1- import asyncio
21import functools
32import sys
43import re
54import js
65import json
76import 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
108import 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
3511def sync_css ():
3612 style_elem .innerHTML = css_box .value
3713
3814
39- @event_handler
40- @debounced
4115def 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
6134def 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+
6743output = js .document .getElementById ("output" )
6844css_box = js .document .getElementById ("css" )
6945console = js .document .getElementById ("console" )
@@ -98,11 +74,3 @@ def download_svg():
9874css_source = get ("schemascii_example.css" ).read ()
9975style_elem .textContent = css_source
10076css_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