Skip to content

Commit bb0ae0b

Browse files
committed
Even better uhtml integration with Signals
1 parent e7c6317 commit bb0ae0b

16 files changed

+1228
-131
lines changed

docs/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@ Please note that if a worker is created explicitly, there won't be any element,
725725
726726
* [multi-pompom](./examples/multi-pompom/) - draw 4 pompom via turtle out of 4 different workers
727727
* [non-blocking input](./examples/worker-input/) - ask a question from a worker and log results in a sync-like, yet non-blocking, style
728+
* [reactive UI](./examples/uhtml/) - a different approach to render safely HTML content and use [Preact Signals](https://preactjs.com/guide/v10/signals/) to react to changes without needing to orchestrate updates manually
728729
729730
730731
## Interpreter Features

docs/examples/uhtml/index.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width,initial-scale=1.0">
6+
<title>uhtml/preactive in Python</title>
7+
<script type="module" src="https://cdn.jsdelivr.net/npm/polyscript"></script>
8+
<script type="pyodide" src="./index.py" config="./py-config.toml"></script>
9+
</head>
10+
<body></body>
11+
</html>

docs/examples/uhtml/index.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from uhtml import local
2+
from js import document
3+
4+
# grab utilities passing current module name
5+
ui = local(__name__)
6+
7+
# define handlers and whatnot
8+
def h3_click(event):
9+
print(event.type)
10+
11+
def button_click(event):
12+
count.value = count.value + 1
13+
14+
# define signals and/or computed
15+
count = ui.signal(0)
16+
who = ui.computed(lambda: f"World {count.value}!")
17+
18+
# render via a callback that can react to changes
19+
ui.render(document.body, lambda: ui.html(
20+
"""
21+
<h3 onclick=${h3_click}>
22+
Hello ${who}
23+
</h3>
24+
<button onclick=${button_click}>
25+
Clicks ${count}
26+
</button>
27+
"""
28+
))

docs/examples/uhtml/mpy-config.toml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[js_modules.main]
2+
"https://cdn.jsdelivr.net/npm/uhtml/preactive.js" = "uhtml"
3+
4+
[[fetch]]
5+
files = ["./template.py", "./uhtml.py"]

docs/examples/uhtml/py-config.toml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[js_modules.main]
2+
"https://cdn.jsdelivr.net/npm/uhtml/preactive.js" = "uhtml"
3+
4+
[[fetch]]
5+
files = ["./uhtml.py"]

0 commit comments

Comments
 (0)