Skip to content

Commit b323c9a

Browse files
kludges on event handlers
1 parent f44028c commit b323c9a

File tree

2 files changed

+29
-43
lines changed

2 files changed

+29
-43
lines changed

index.html

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ <h2>More Information</h2>
7575
// cSpell:ignore pyodide pyproject
7676
var pyodide;
7777
var console = document.getElementById("console");
78-
var errors = document.getElementById("errors")
78+
var errors = document.getElementById("errors");
79+
var css_box = document.getElementById("css");
80+
var source = document.getElementById("schemascii");
81+
var download_button = document.getElementById("download");
82+
var ver_switcher = document.getElementById("version");
7983

8084
async function main() {
8185
try {
@@ -85,6 +89,13 @@ <h2>More Information</h2>
8589
await pyodide.loadPackage("micropip", { errorCallback: error, messageCallback: () => { } });
8690
info("done\n");
8791
await pyodide.runPythonAsync(await fetch("scripts/web_startup.py").then(r => r.text()));
92+
css_box.addEventListener("input", debounce(pyodide.globals.get("sync_css")));
93+
source.addEventListener("input", debounce(pyodide.globals.get("render_catch_warnings")));
94+
download_button.addEventListener("click", pyodide.globals.get("download_svg"));
95+
ver_switcher.addEventListener("change", pyodide.globals.get("switch_version"));
96+
source.removeAttribute("disabled");
97+
css_box.removeAttribute("disabled");
98+
console.textContent = "Ready";
8899
} catch (e) {
89100
error(`\nFATAL ERROR:\n${e.stack}\n`);
90101
throw e;
@@ -96,6 +107,13 @@ <h2>More Information</h2>
96107
function error(text) {
97108
errors.textContent += text;
98109
}
110+
function debounce(fun) {
111+
var timeout;
112+
return function() {
113+
if (timeout) clearTimeout(timeout);
114+
timeout = setTimeout(fun, 100);
115+
}
116+
}
99117
main();
100118
</script>
101119

scripts/web_startup.py

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,17 @@
1-
import asyncio
21
import functools
32
import sys
43
import re
54
import js
65
import json
76
import warnings
8-
from pyodide.http import pyfetch as fetch, open_url as get
9-
from pyodide.ffi import create_proxy as event_handler
7+
from pyodide.http import open_url as get
108
import micropip
119

1210

13-
def debounced(fun):
14-
timeout = None
15-
16-
@functools.wraps(fun)
17-
def inner():
18-
nonlocal timeout
19-
if timeout:
20-
js.clearTimeout(timeout)
21-
timeout = js.setTimeout(fun, 100)
22-
23-
return inner
24-
25-
26-
def syncify(fun):
27-
@functools.wraps(fun)
28-
def inner(e):
29-
return asyncio.ensure_future(fun(e))
30-
return inner
31-
32-
33-
@event_handler
34-
@debounced
3511
def sync_css():
3612
style_elem.innerHTML = css_box.value
3713

3814

39-
@event_handler
40-
@debounced
4115
def render_catch_warnings(*args, **kwargs):
4216
import schemascii
4317
console.textContent = ""
@@ -49,21 +23,23 @@ def render_catch_warnings(*args, **kwargs):
4923
return out
5024

5125

52-
@event_handler
53-
@syncify
54-
async def switch_version():
26+
def switch_version():
5527
if "schemascii" in sys.modules:
5628
del sys.modules["schemascii"] # Invalidate cache
5729
version = ver_switcher.value
58-
await micropip.install(versions_to_wheel_map[version])
30+
js.eval(
31+
f'''(async () => await pyodide.globals.get("micropip").install({versions_to_wheel_map[version]!r}))();''')
32+
5933

60-
@event_handler
6134
def download_svg():
6235
a = js.document.createElement("a")
63-
a.setAttribute("href", js.URL.createObjectURL(js.Blob.new([output.innerHTML], {"type": "application/svg+xml"})))
64-
a.setAttribute("download", f"schemascii_playground_{js.Date.new.toISOString()}_no_css.svg")
36+
a.setAttribute("href", js.URL.createObjectURL(js.Blob.new(
37+
[output.innerHTML], {"type": "application/svg+xml"})))
38+
a.setAttribute(
39+
"download", f"schemascii_playground_{js.Date.new.toISOString()}_no_css.svg")
6540
a.click()
6641

42+
6743
output = js.document.getElementById("output")
6844
css_box = js.document.getElementById("css")
6945
console = js.document.getElementById("console")
@@ -98,11 +74,3 @@ def download_svg():
9874
css_source = get("schemascii_example.css").read()
9975
style_elem.textContent = css_source
10076
css_box.value = css_source
101-
102-
css_box.addEventListener("input", sync_css)
103-
source.addEventListener("input", render_catch_warnings)
104-
download_button.addEventListener("click", download_svg)
105-
106-
source.removeAttribute("disabled")
107-
css_box.removeAttribute("disabled")
108-
console.textContent = "ready\n"

0 commit comments

Comments
 (0)