File tree 4 files changed +34
-16
lines changed
4 files changed +34
-16
lines changed Original file line number Diff line number Diff line change 4
4
< meta charset ="UTF-8 " />
5
5
< meta name ="viewport " content ="width=device-width,initial-scale=1.0 ">
6
6
< 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 >
8
11
< script type ="module " src ="https://cdn.jsdelivr.net/npm/polyscript "> </ script >
9
12
< script type ="pyodide " src ="./index.py " config ="./config.toml "> </ script >
10
13
</ head >
Original file line number Diff line number Diff line change 1
1
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__ )
3
5
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 ():
5
21
render (document .body , html (
6
22
"""
7
23
<h3 onclick=${h3_click}>
@@ -10,11 +26,7 @@ def update(count):
10
26
<button onclick=${button_click}>
11
27
Clicks ${count}
12
28
</button>
13
- """ ,
14
- h3_click = lambda event : print (event .type ),
15
- value = "World" ,
16
- button_click = lambda _ : update (count + 1 ),
17
- count = count
29
+ """
18
30
))
19
31
20
- update ( 0 )
32
+ show ( )
Original file line number Diff line number Diff line change 1
1
# A silly idea by Andrea Giammarchi
2
2
from string import Template as _Template
3
+ from sys import modules
3
4
4
5
# The goal of this utility is to create a JS
5
6
# Template Literal Tag like function that accepts
8
9
# If a cache dictionary is passed, it never parses the same
9
10
# template string more than once, improving performance
10
11
# 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 ] )
13
14
14
15
def _create (tpl ):
15
16
i = 0
@@ -36,7 +37,7 @@ def _create(tpl):
36
37
i += 1
37
38
# make the template immutable
38
39
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 ])
40
41
41
42
# given a template string, maps all non interpolated
42
43
# parts as tuple and orchestrate ordered values to send
Original file line number Diff line number Diff line change @@ -27,7 +27,9 @@ def hole(tpl, *values):
27
27
return lambda : uhtml (_arrays [i ], * [_to_js (v ) for v in values ])
28
28
return hole
29
29
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
+ )
You can’t perform that action at this time.
0 commit comments