1
- import asyncio
2
1
import functools
3
2
import sys
4
3
import re
5
4
import js
6
5
import json
7
6
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
10
8
import micropip
11
9
12
10
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
35
11
def sync_css ():
36
12
style_elem .innerHTML = css_box .value
37
13
38
14
39
- @event_handler
40
- @debounced
41
15
def render_catch_warnings (* args , ** kwargs ):
42
16
import schemascii
43
17
console .textContent = ""
@@ -49,21 +23,23 @@ def render_catch_warnings(*args, **kwargs):
49
23
return out
50
24
51
25
52
- @event_handler
53
- @syncify
54
- async def switch_version ():
26
+ def switch_version ():
55
27
if "schemascii" in sys .modules :
56
28
del sys .modules ["schemascii" ] # Invalidate cache
57
29
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
+
59
33
60
- @event_handler
61
34
def download_svg ():
62
35
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" )
65
40
a .click ()
66
41
42
+
67
43
output = js .document .getElementById ("output" )
68
44
css_box = js .document .getElementById ("css" )
69
45
console = js .document .getElementById ("console" )
@@ -98,11 +74,3 @@ def download_svg():
98
74
css_source = get ("schemascii_example.css" ).read ()
99
75
style_elem .textContent = css_source
100
76
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