Skip to content

Commit cf32b6c

Browse files
move to js because pyodide is being annoying
1 parent b323c9a commit cf32b6c

File tree

4 files changed

+128
-121
lines changed

4 files changed

+128
-121
lines changed

index.html

+1-45
Original file line numberDiff line numberDiff line change
@@ -71,50 +71,6 @@ <h2>More Information</h2>
7171
<p><a href="https://github.com/dragoncoder047/schemascii/"
7272
target="_blank">https://github.com/dragoncoder047/schemascii/</a></p>
7373
</body>
74-
<script>
75-
// cSpell:ignore pyodide pyproject
76-
var pyodide;
77-
var console = document.getElementById("console");
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");
83-
84-
async function main() {
85-
try {
86-
info("Loading Python... ");
87-
pyodide = await loadPyodide({ stdout: info, stderr: error });
88-
info("done\nInstalling micropip...");
89-
await pyodide.loadPackage("micropip", { errorCallback: error, messageCallback: () => { } });
90-
info("done\n");
91-
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";
99-
} catch (e) {
100-
error(`\nFATAL ERROR:\n${e.stack}\n`);
101-
throw e;
102-
}
103-
}
104-
function info(line) {
105-
console.textContent += line;
106-
}
107-
function error(text) {
108-
errors.textContent += text;
109-
}
110-
function debounce(fun) {
111-
var timeout;
112-
return function() {
113-
if (timeout) clearTimeout(timeout);
114-
timeout = setTimeout(fun, 100);
115-
}
116-
}
117-
main();
118-
</script>
74+
<script src="scripts/web_startup.js"></script>
11975

12076
</html>

scripts/monkeypatch.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import sys
2+
if "schemascii" in sys.modules:
3+
del sys.modules["schemascii"]
4+
5+
import warnings
6+
import schemascii
7+
8+
print("monkeypatching... ", end="")
9+
10+
def patched(src):
11+
with warnings.catch_warnings(record=True) as captured_warnings:
12+
out = schemascii.render("<playground>", src)
13+
for warn in captured_warnings:
14+
print("warning:", warn.message)
15+
return out
16+
17+
schemascii.patched_render = patched

scripts/web_startup.js

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// cSpell:ignore pyodide pyproject
2+
var pyodide;
3+
var console = document.getElementById("console");
4+
var errors = document.getElementById("errors");
5+
var css_box = document.getElementById("css");
6+
var source = document.getElementById("schemascii");
7+
var download_button = document.getElementById("download");
8+
var ver_switcher = document.getElementById("version");
9+
10+
var schemascii;
11+
var monkeysrc;
12+
13+
var ver_map;
14+
15+
async function main() {
16+
try {
17+
info("Loading Python... ");
18+
pyodide = await loadPyodide({ stdout: info, stderr: error });
19+
info("done\nInstalling micropip...");
20+
await pyodide.loadPackage("micropip", { errorCallback: error, messageCallback: () => { } });
21+
info("done\nFetching versions... ");
22+
monkeysrc = await fetch("scripts/monkeypatch.py").then(r => r.text());
23+
var foo = await fetch("https://api.github.com/repos/dragoncoder047/schemascii/contents/dist").then(r => r.json());
24+
foo = foo.filter(x => x.name.endsWith(".whl")).map(x => x.path);
25+
ver_map = Object.fromEntries(foo.map(x => [/\/schemascii-([\d.]+)-/.exec(x)[1], x]))
26+
var all_versions = Object.keys(ver_map);
27+
//all_versions.push("DEV");
28+
for (var v of all_versions) {
29+
var o = document.createElement("option");
30+
o.textContent = o.value = v;
31+
ver_switcher.append(o);
32+
}
33+
var latest_version = await fetch("pyproject.toml").then(r => r.text()).then(r => /version = "([\d.]+)"/.exec(r)[0]);
34+
ver_switcher.value = latest_version;
35+
info(`["${all_versions.join('", "')}"]\nlatest=${latest_version}\n`);
36+
await switch_version();
37+
css_box.addEventListener("input", debounce(sync_css));
38+
source.addEventListener("input", debounce(catched(render)));
39+
download_button.addEventListener("click", download);
40+
ver_switcher.addEventListener("change", acatched(switch_version));
41+
42+
source.removeAttribute("disabled");
43+
css_box.removeAttribute("disabled");
44+
console.textContent = "Ready";
45+
} catch (e) {
46+
error(`\nFATAL ERROR:\n${e.stack}\n`);
47+
throw e;
48+
}
49+
}
50+
function monkeypatch() {
51+
pyodide.runPython(monkeysrc);
52+
}
53+
function info(line) {
54+
console.textContent += line;
55+
}
56+
function error(text) {
57+
errors.textContent += text;
58+
}
59+
function debounce(fun) {
60+
var timeout;
61+
return function () {
62+
if (timeout) clearTimeout(timeout);
63+
timeout = setTimeout(fun.bind(this, arguments), 100);
64+
};
65+
}
66+
function catched(fun) {
67+
return function () {
68+
try {
69+
fun.call(this, arguments);
70+
} catch (e) {
71+
error(e.stack);
72+
}
73+
};
74+
}
75+
async function acatched(fun) {
76+
return async function() {
77+
try {
78+
await fun.call(this, arguments);
79+
} catch (e) {
80+
error(e.stack);
81+
}
82+
};
83+
}
84+
function sync_css() {
85+
style_elem.innerHTML = css_box.value;
86+
}
87+
function render() {
88+
console.textContent = "";
89+
errors.textContent = "";
90+
output.textContent = schemascii.patched_render(source.value);
91+
}
92+
93+
async function switch_version() {
94+
info("Installing Schemascii version " + ver_switcher.value + "... ")
95+
await pyodide.pyimport("micropip").install(ver_map[ver_switcher.value]);
96+
monkeypatch();
97+
schemascii = pyodide.runPython("import schemascii; schemascii");
98+
info("done\n");
99+
}
100+
101+
function download() {
102+
var a = document.createElement("a");
103+
a.setAttribute("href", URL.createObjectURL(new Blob([output.innerHTML], {"type": "application/svg+xml"})));
104+
a.setAttribute("download", `schemascii_playground_${new Date().toISOString()}_no_css.svg`);
105+
a.click();
106+
}
107+
108+
main();
109+
110+
// fetch("https://github.com/dragoncoder047/schemascii/zipball/main/").then(r => r.arrayBuffer()).then(b => pyodide.unpackArchive(b, "zip"));

scripts/web_startup.py

-76
This file was deleted.

0 commit comments

Comments
 (0)