-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_html.py
104 lines (80 loc) · 2.6 KB
/
test_html.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import sys, platform
js = type(platform)("js")
js.document = platform.document
sys.modules["js"] = js
def jsid(elemid):
import js
# Element(str(elemid)).element
return js.document.getElementById(str(elemid))
def jsattr(query, attr):
if isinstance(query, str):
return js.document.getElementById(str(query)).getAttribute(attr)
return query.getAttribute(attr)
def new(oclass, *argv):
return getattr(oclass, "new")(*argv)
# Read data from Emscripten Filesystem
ObjectURLs = {}
def File(path):
global ObjectURLs
from js import window
import pyodide.ffi
filename = str(path)
bloburl = ObjectURLs.get(filename, None)
if bloburl is None:
with open(filename, "rb") as fp:
data_from_FS = fp.read()
jsfile = window.File.new([pyodide.ffi.to_js(data_from_FS)], filename)
bloburl = window.URL.createObjectURL(jsfile)
ObjectURLs[filename] = bloburl
return bloburl
class HTML:
def __init__(self, file="html"):
self.file = file
self.buffer = []
self.ctx = 0
def __call__(self, *argv, **kw):
sys.__stderr__.write(f"HTML {argv}")
kw.setdefault("end", "\n")
kw.setdefault("pre", " " * self.ctx)
for pos, arg in enumerate(argv):
pre = ""
suf = ""
if not pos:
pre
else:
pre = ""
arg = arg.replace("py-click=", 'onclick="route(event)" py-click=')
arg = arg.replace('{','{i18n.')
o_len = len(arg)
arg = arg.rstrip("'")
arg = eval(f"f'''{arg}'''") + "'" * (len(arg) - o_len)
self.buffer.append(kw["pre"] + arg + kw["end"])
if not self.ctx:
self.flush()
def read(self):
return str().join(self.buffer)
def flush(self):
import js
extend = js.document.createElement("div")
extend.innerHTML = self.read()
jsid(self.file).appendChild(extend)
self.buffer.clear()
def __enter__(self):
self.ctx += 1
return self
def __exit__(self, *_):
self.ctx -= 1
if not self.ctx:
self.flush()
class i18n:
world = "mundo"
def clicked(*argv):
print("button has be clicked")
async def main():
html = HTML("html")
with html as printf:
printf("Hello {world}")
printf('<button id=pushme onclick=py.clicked()>Say Hello {world}</button>')
#printf('<button id=pushme>Say Hello {world}</button>')
# todo use a router closure jsid("pushme").addEventListener("click", "py.clicked")
asyncio.run(main())