Skip to content

Commit e7c6317

Browse files
committed
Updated/changed the uhtml demo
1 parent d3a42e4 commit e7c6317

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

test/uhtml/index.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width,initial-scale=1.0">
66
<title>Template</title>
7-
<script defer src="https://cdn.jsdelivr.net/npm/uhtml/es.js"></script>
7+
<script type="module">
8+
import * as uhtml from "https://cdn.jsdelivr.net/npm/uhtml/index.js";
9+
globalThis.uhtml = uhtml;
10+
</script>
811
<script type="module" src="https://cdn.jsdelivr.net/npm/polyscript"></script>
912
<script type="pyodide" src="./index.py" config="./config.toml"></script>
1013
</head>

test/uhtml/index.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
from js import document
2-
from uhtml import render, html
2+
from uhtml import local
3+
# grab utilities passing current module name
4+
render, html, svg = local(__name__)
35

4-
def update(count):
6+
# define handlers and whatnot
7+
def h3_click(event):
8+
print(event.type)
9+
10+
def button_click(event):
11+
global count, value
12+
count = count + 1
13+
value = f"World {count}"
14+
show()
15+
16+
value = "World"
17+
count = 0
18+
19+
# define the driver
20+
def show():
521
render(document.body, html(
622
"""
723
<h3 onclick=${h3_click}>
@@ -10,11 +26,7 @@ def update(count):
1026
<button onclick=${button_click}>
1127
Clicks ${count}
1228
</button>
13-
""",
14-
h3_click=lambda event: print(event.type),
15-
value="World",
16-
button_click=lambda _: update(count + 1),
17-
count=count
29+
"""
1830
))
1931

20-
update(0)
32+
show()

test/uhtml/js_template.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# A silly idea by Andrea Giammarchi
22
from string import Template as _Template
3+
from sys import modules
34

45
# The goal of this utility is to create a JS
56
# Template Literal Tag like function that accepts
@@ -8,8 +9,8 @@
89
# If a cache dictionary is passed, it never parses the same
910
# template string more than once, improving performance
1011
# for more complex scenarios / use cases.
11-
def tag(fn, cache=None):
12-
return lambda tpl, **kw: _tag(tpl, cache)(fn, **kw)
12+
def tag(name, fn, cache=None):
13+
return lambda tpl: _tag(tpl, cache)(fn, modules[name])
1314

1415
def _create(tpl):
1516
i = 0
@@ -36,7 +37,7 @@ def _create(tpl):
3637
i += 1
3738
# make the template immutable
3839
t = tuple(a)
39-
return lambda fn, **kw: fn(t, *[kw[k] for k in keys])
40+
return lambda fn, kw: fn(t, *[kw.__dict__[k] for k in keys])
4041

4142
# given a template string, maps all non interpolated
4243
# parts as tuple and orchestrate ordered values to send

test/uhtml/uhtml.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ def hole(tpl, *values):
2727
return lambda: uhtml(_arrays[i], *[_to_js(v) for v in values])
2828
return hole
2929

30-
# export uhtml API as Python
31-
html = _tag(_kind(_uhtml.html), cache=_cache)
32-
svg = _tag(_kind(_uhtml.svg), cache=_cache)
33-
render = _uhtml.render
30+
def local(name):
31+
return (
32+
_uhtml.render,
33+
_tag(name, _kind(_uhtml.html), cache=_cache),
34+
_tag(name, _kind(_uhtml.svg), cache=_cache)
35+
)

0 commit comments

Comments
 (0)