From 6757c2e882e22db54534430abe97f805d7b7b950 Mon Sep 17 00:00:00 2001
From: dragoncoder047 <101021094+dragoncoder047@users.noreply.github.com>
Date: Fri, 11 Aug 2023 18:35:20 +0000
Subject: [PATCH 01/13] fix version finding bug
---
scripts/web_startup.py | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/scripts/web_startup.py b/scripts/web_startup.py
index 9e24293..1977a97 100644
--- a/scripts/web_startup.py
+++ b/scripts/web_startup.py
@@ -6,7 +6,6 @@
import re
import js
import json
-from operator import itemgetter
import warnings
import micropip
@@ -68,12 +67,13 @@ async def switch_version():
print("Loading all versions... ", end="")
-versions_all = json.load(
+foo = json.load(
get("https://api.github.com/repos/dragoncoder047/schemascii/contents/dist"))
+foo = filter(lambda x: x["name"].endswith(".whl"), foo)
+foo = map(lambda x: x["path"], foo)
versions_to_wheel_map = dict(
- zip(map(itemgetter("name"), versions_all), map(itemgetter("path"), versions_all)))
+ zip(map(lambda x: re.search(r"""/schemascii-([\d.]+)-""", x).group(1), foo), foo))
all_versions = list(versions_to_wheel_map.keys())
-all_versions.append("DEV")
latest_version = re.search(
r'''version = "([\d.]+)"''', get("pyproject.toml").read()).group(1)
print(all_versions, "latest =", latest_version)
From c625572397b7a8ac17b574389175bd3e84e04c9b Mon Sep 17 00:00:00 2001
From: dragoncoder047 <101021094+dragoncoder047@users.noreply.github.com>
Date: Fri, 11 Aug 2023 18:41:39 +0000
Subject: [PATCH 02/13] mixed up lazy iteration
---
scripts/web_startup.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/scripts/web_startup.py b/scripts/web_startup.py
index 1977a97..b4b8ac0 100644
--- a/scripts/web_startup.py
+++ b/scripts/web_startup.py
@@ -70,7 +70,7 @@ async def switch_version():
foo = json.load(
get("https://api.github.com/repos/dragoncoder047/schemascii/contents/dist"))
foo = filter(lambda x: x["name"].endswith(".whl"), foo)
-foo = map(lambda x: x["path"], foo)
+foo = list(map(lambda x: x["path"], foo))
versions_to_wheel_map = dict(
zip(map(lambda x: re.search(r"""/schemascii-([\d.]+)-""", x).group(1), foo), foo))
all_versions = list(versions_to_wheel_map.keys())
From f44028cff791a95d228164ee60523895e4f0372d Mon Sep 17 00:00:00 2001
From: dragoncoder047 <101021094+dragoncoder047@users.noreply.github.com>
Date: Fri, 11 Aug 2023 18:55:18 +0000
Subject: [PATCH 03/13] add download button
---
index.html | 2 +-
scripts/web_startup.py | 13 +++++++++++--
2 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/index.html b/index.html
index 098f57c..eafbbbb 100644
--- a/index.html
+++ b/index.html
@@ -61,7 +61,7 @@
Schemascii Source
CSS
- Result
+ Result
Messages
diff --git a/scripts/web_startup.py b/scripts/web_startup.py
index b4b8ac0..8a3b93a 100644
--- a/scripts/web_startup.py
+++ b/scripts/web_startup.py
@@ -1,5 +1,3 @@
-from pyodide.http import pyfetch as fetch, open_url as get
-from pyodide.ffi import create_proxy as event_handler
import asyncio
import functools
import sys
@@ -7,6 +5,8 @@
import js
import json
import warnings
+from pyodide.http import pyfetch as fetch, open_url as get
+from pyodide.ffi import create_proxy as event_handler
import micropip
@@ -57,6 +57,13 @@ async def switch_version():
version = ver_switcher.value
await micropip.install(versions_to_wheel_map[version])
+@event_handler
+def download_svg():
+ a = js.document.createElement("a")
+ a.setAttribute("href", js.URL.createObjectURL(js.Blob.new([output.innerHTML], {"type": "application/svg+xml"})))
+ a.setAttribute("download", f"schemascii_playground_{js.Date.new.toISOString()}_no_css.svg")
+ a.click()
+
output = js.document.getElementById("output")
css_box = js.document.getElementById("css")
console = js.document.getElementById("console")
@@ -64,6 +71,7 @@ async def switch_version():
style_elem = js.document.getElementById("custom-css")
errors = js.document.getElementById("errors")
ver_switcher = js.document.getElementById("version")
+download_button = js.document.getElementById("download")
print("Loading all versions... ", end="")
@@ -93,6 +101,7 @@ async def switch_version():
css_box.addEventListener("input", sync_css)
source.addEventListener("input", render_catch_warnings)
+download_button.addEventListener("click", download_svg)
source.removeAttribute("disabled")
css_box.removeAttribute("disabled")
From b323c9a295ad9a3d4d8e071d928b2e89ef7ec598 Mon Sep 17 00:00:00 2001
From: dragoncoder047 <101021094+dragoncoder047@users.noreply.github.com>
Date: Fri, 11 Aug 2023 19:29:51 +0000
Subject: [PATCH 04/13] kludges on event handlers
---
index.html | 20 +++++++++++++++-
scripts/web_startup.py | 52 ++++++++----------------------------------
2 files changed, 29 insertions(+), 43 deletions(-)
diff --git a/index.html b/index.html
index eafbbbb..7847b02 100644
--- a/index.html
+++ b/index.html
@@ -75,7 +75,11 @@ More Information
// cSpell:ignore pyodide pyproject
var pyodide;
var console = document.getElementById("console");
- var errors = document.getElementById("errors")
+ var errors = document.getElementById("errors");
+ var css_box = document.getElementById("css");
+ var source = document.getElementById("schemascii");
+ var download_button = document.getElementById("download");
+ var ver_switcher = document.getElementById("version");
async function main() {
try {
@@ -85,6 +89,13 @@ More Information
await pyodide.loadPackage("micropip", { errorCallback: error, messageCallback: () => { } });
info("done\n");
await pyodide.runPythonAsync(await fetch("scripts/web_startup.py").then(r => r.text()));
+ css_box.addEventListener("input", debounce(pyodide.globals.get("sync_css")));
+ source.addEventListener("input", debounce(pyodide.globals.get("render_catch_warnings")));
+ download_button.addEventListener("click", pyodide.globals.get("download_svg"));
+ ver_switcher.addEventListener("change", pyodide.globals.get("switch_version"));
+ source.removeAttribute("disabled");
+ css_box.removeAttribute("disabled");
+ console.textContent = "Ready";
} catch (e) {
error(`\nFATAL ERROR:\n${e.stack}\n`);
throw e;
@@ -96,6 +107,13 @@ More Information
function error(text) {
errors.textContent += text;
}
+ function debounce(fun) {
+ var timeout;
+ return function() {
+ if (timeout) clearTimeout(timeout);
+ timeout = setTimeout(fun, 100);
+ }
+ }
main();
diff --git a/scripts/web_startup.py b/scripts/web_startup.py
index 8a3b93a..360902b 100644
--- a/scripts/web_startup.py
+++ b/scripts/web_startup.py
@@ -1,43 +1,17 @@
-import asyncio
import functools
import sys
import re
import js
import json
import warnings
-from pyodide.http import pyfetch as fetch, open_url as get
-from pyodide.ffi import create_proxy as event_handler
+from pyodide.http import open_url as get
import micropip
-def debounced(fun):
- timeout = None
-
- @functools.wraps(fun)
- def inner():
- nonlocal timeout
- if timeout:
- js.clearTimeout(timeout)
- timeout = js.setTimeout(fun, 100)
-
- return inner
-
-
-def syncify(fun):
- @functools.wraps(fun)
- def inner(e):
- return asyncio.ensure_future(fun(e))
- return inner
-
-
-@event_handler
-@debounced
def sync_css():
style_elem.innerHTML = css_box.value
-@event_handler
-@debounced
def render_catch_warnings(*args, **kwargs):
import schemascii
console.textContent = ""
@@ -49,21 +23,23 @@ def render_catch_warnings(*args, **kwargs):
return out
-@event_handler
-@syncify
-async def switch_version():
+def switch_version():
if "schemascii" in sys.modules:
del sys.modules["schemascii"] # Invalidate cache
version = ver_switcher.value
- await micropip.install(versions_to_wheel_map[version])
+ js.eval(
+ f'''(async () => await pyodide.globals.get("micropip").install({versions_to_wheel_map[version]!r}))();''')
+
-@event_handler
def download_svg():
a = js.document.createElement("a")
- a.setAttribute("href", js.URL.createObjectURL(js.Blob.new([output.innerHTML], {"type": "application/svg+xml"})))
- a.setAttribute("download", f"schemascii_playground_{js.Date.new.toISOString()}_no_css.svg")
+ a.setAttribute("href", js.URL.createObjectURL(js.Blob.new(
+ [output.innerHTML], {"type": "application/svg+xml"})))
+ a.setAttribute(
+ "download", f"schemascii_playground_{js.Date.new.toISOString()}_no_css.svg")
a.click()
+
output = js.document.getElementById("output")
css_box = js.document.getElementById("css")
console = js.document.getElementById("console")
@@ -98,11 +74,3 @@ def download_svg():
css_source = get("schemascii_example.css").read()
style_elem.textContent = css_source
css_box.value = css_source
-
-css_box.addEventListener("input", sync_css)
-source.addEventListener("input", render_catch_warnings)
-download_button.addEventListener("click", download_svg)
-
-source.removeAttribute("disabled")
-css_box.removeAttribute("disabled")
-console.textContent = "ready\n"
From cf32b6cf08dd10ff6cbf320f6a22e3de897cb41d Mon Sep 17 00:00:00 2001
From: dragoncoder047 <101021094+dragoncoder047@users.noreply.github.com>
Date: Fri, 11 Aug 2023 20:46:36 +0000
Subject: [PATCH 05/13] move to js because pyodide is being annoying
---
index.html | 46 +----------------
scripts/monkeypatch.py | 17 +++++++
scripts/web_startup.js | 110 +++++++++++++++++++++++++++++++++++++++++
scripts/web_startup.py | 76 ----------------------------
4 files changed, 128 insertions(+), 121 deletions(-)
create mode 100644 scripts/monkeypatch.py
create mode 100644 scripts/web_startup.js
delete mode 100644 scripts/web_startup.py
diff --git a/index.html b/index.html
index 7847b02..6f750b2 100644
--- a/index.html
+++ b/index.html
@@ -71,50 +71,6 @@ More Information
https://github.com/dragoncoder047/schemascii/