Home
Napolitan is a lightweight, user-friendly library for creating custom elements with a simple API for PyScript. It uses a declarative approach for easy component structure definition and updates only changed parts of the DOM for efficiency. It works with both Pyodide and MicroPython, using only the language features and built-in libraries supported by both, intentionally avoiding certain modules like typing and metaclasses. Based on Web Components, probably, Napolitan works with any PyScript, JavaScript, or TypeScript framework.
Quick Look¶
Example: Counter (Not Styled)¶
from napolitan.core import Component, Attr, customElement
from napolitan.livingstandard import div, button
@customElement("my-counter-not-styled")
class Counter(Component):
count = Attr(int, 0)
def render(self):
return div()(
button(onclick=self.increment)("Increment"),
self.count,
button(onclick=self.decrement)("Decrement"),
)
def increment(self, _):
self.count += 1
def decrement(self, _):
self.count -= 1
Example: Counter (Styled using Shoelace)¶
Info
"+" annotations in this example explain each part of the code well, so could please click "+" if you want to know the details.
from napolitan.core import Component, Attr, customElement
from napolitan.livingstandard import div, span
from napolitan.shoelace import button, button_group, icon, card
@customElement("my-counter") #
class Counter(Component): #
count = Attr(int, 0) #
@staticmethod
def style(): #
return """
.counter {
--padding: 1rem;
--border-color: #1E2129;
}
.count {
font-size: 3rem;
font-weight: bold;
color: #1E2129;
display: flex;
justify-content: center;
}
"""
def render(self): #
return card(cls="counter")(
span(cls="count")(self.count),
div(slot="footer", cls="footer")(
button_group(label="btns")(
button(variant="primary", onclick=self.increment)( #
icon(name="plus-lg")
),
button(variant="primary", onclick=self.decrement)( #
icon(name="dash-lg")
),
)
),
)
def increment(self, e): #
self.count += 1
def decrement(self, e): #
self.count -= 1
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.15.0/cdn/themes/light.css" />
<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.15.0/cdn/shoelace.js"></script>
<script type="mpy" src="examples/counter.py" config="examples/pyscript.toml"></script>
<my-counter count="10"></my-counter>
Example: Counter (Used listen
decorator for event handling)¶
from napolitan.core import Component, Attr, customElement, track_method_calls, listen
from napolitan.livingstandard import div, span
from napolitan.shoelace import sl_button, sl_button_group, sl_icon, sl_card
@customElement("my-counter-use-listen")
class Counter(Component):
count = Attr(int, 0)
@staticmethod
def style():
return """
:host {
display: flex;
width: fit-content;
height: fit-content;
}
.counter {
--padding: 1rem;
--border-color: #1E2129;
}
.count {
font-size: 3rem;
font-weight: bold;
color: #1E2129;
display: flex;
justify-content: center;
}
.counter [slot='footer'] {
display: flex;
justify-content: center;
}
"""
def render(self):
return sl_card(cls="counter")(
span(cls="count")(self.count),
div(slot="footer")(
sl_button_group(label="btns")(
sl_button(cls="inc", variant="primary")(
sl_icon(name="plus-lg"),
),
sl_button(cls="dec", variant="primary")(
sl_icon(name="dash-lg")
),
)
),
)
@listen("click", ".inc")
def increment(self, e):
self.count += 1
@listen("click", ".dec")
def decrement(self, e):
self.count -= 1