Skip to content

Commit

Permalink
version 0.3.2 -> bug fix with no css
Browse files Browse the repository at this point in the history
  • Loading branch information
dragoncoder047 authored Aug 11, 2023
1 parent b44080f commit 800f1f1
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 63 deletions.
Binary file added dist/schemascii-0.3.2-py3-none-any.whl
Binary file not shown.
Binary file added dist/schemascii-0.3.2.tar.gz
Binary file not shown.
79 changes: 19 additions & 60 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@
color: red;
}

a, a:visited {
a,
a:visited {
color: blue;
}

@media screen and (prefers-color-scheme: dark) {
body, textarea {

body,
textarea {
color: white;
background: black;
}
a, a:visited {

a,
a:visited {
color: magenta;
}
}
Expand All @@ -47,7 +52,7 @@

<body>
<h1>Schemascii Playground</h1>
<h2 id="version">Loading version...</h2>
<h2>using schemascii version <select id="version"></select></h2>
<div class="flex row">
<div class="flex column">
<h2>Schemascii Source</h2><textarea id="schemascii" disabled></textarea>
Expand All @@ -63,46 +68,25 @@ <h2>Messages</h2>
<h2>Errors</h2>
<pre id="errors"></pre>
<h2>More Information</h2>
<p><a href="https://github.com/dragoncoder047/schemascii/" target="_blank">https://github.com/dragoncoder047/schemascii/</a></p>
<p><a href="https://github.com/dragoncoder047/schemascii/"
target="_blank">https://github.com/dragoncoder047/schemascii/</a></p>
</body>
<script>
// cSpell:ignore pyodide pyproject pyimport
// cSpell:ignore pyodide pyproject
var pyodide;
var output = document.getElementById("output");
var css = document.getElementById("css");
var console = document.getElementById("console");
var source = document.getElementById("schemascii");
var style_elem = document.getElementById("custom-css");
var schemascii_render;
var errors = document.getElementById("errors")

async function main() {
try {
info("Loading Python... ");
pyodide = await loadPyodide({ stdout: info, stderr: error });
info("done\nInstalling micropip...");
await pyodide.loadPackage("micropip", { errorCallback: error, messageCallback: () => {} });
info("done\nFetching current Schemascii version... ");
var pyproject_toml = await fetch("pyproject.toml").then(x => x.text());
var ver = /version = "([\d.]+)"/.exec(pyproject_toml)[1];
document.getElementById("version").textContent = `using Schemascii version ${ver}`;
info(`${ver}\nInstalling schemascii-${ver} ... `);
await pyodide.pyimport("micropip", { errorCallback: error, messageCallback: info })
.install(`https://dragoncoder047.github.io/schemascii/dist/schemascii-${ver}-py3-none-any.whl`);
schemascii_render = await pyodide.runPythonAsync(`
import warnings
import schemascii
def foo(*args, **kwargs):
with warnings.catch_warnings(record=True) as captured_warnings:
out = schemascii.render(*args, **kwargs)
for warn in captured_warnings:
print("warning:", warn.message)
return out
foo
`);
await setup();
console.textContent = "ready\n";
await pyodide.loadPackage("micropip", { errorCallback: error, messageCallback: () => { } });
info("done\n");
await pyodide.runPythonAsync(await fetch("scripts/web_startup.py").then(r => r.text()));
} catch (e) {
error(`\n${e.stack}\n`);
error(`\nFATAL ERROR:\n${e.stack}\n`);
throw e;
}
}
Expand All @@ -112,32 +96,7 @@ <h2>More Information</h2>
function error(text) {
errors.textContent += text;
}
async function setup() {
css.value = await fetch("schemascii_example.css").then(x => x.text());
style_elem.innerHTML = css.value;
css.addEventListener("input", () => {
style_elem.innerHTML = css.value;
});
var timeout = null;
source.addEventListener("input", () => {
if (timeout) clearTimeout(timeout);
// Debouncing
timeout = setTimeout(async () => {
console.textContent = "";
errors.textContent = "";
try {
output.innerHTML = await schemascii_render("playground", source.value);
} catch (e) {
error(`\n${e.stack}\n`);
output.innerHTML = "";
throw e;
}
}, 100);
});
source.removeAttribute("disabled");
css.removeAttribute("disabled");
}
main();
</script>

</html>
</html>
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "schemascii"
version = "0.3.1"
version = "0.3.2"
description = "Render ASCII-art schematics to SVG"
readme = "README.md"
authors = [{ name = "dragoncoder047", email = "[email protected]" }]
Expand Down
2 changes: 1 addition & 1 deletion schemascii/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .utils import XML
from .errors import *

__version__ = "0.3.1"
__version__ = "0.3.2"


def render(filename: str, text: str = None, **options) -> str:
Expand Down
99 changes: 99 additions & 0 deletions scripts/web_startup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
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
import re
import js
import json
from operator import itemgetter
import warnings
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 = ""
errors.textContent = ""
with warnings.catch_warnings(record=True) as captured_warnings:
out = schemascii.render(*args, **kwargs)
for warn in captured_warnings:
print("warning:", warn.message)
return out


@event_handler
@syncify
async 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])

output = js.document.getElementById("output")
css_box = js.document.getElementById("css")
console = js.document.getElementById("console")
source = js.document.getElementById("schemascii")
style_elem = js.document.getElementById("custom-css")
errors = js.document.getElementById("errors")
ver_switcher = js.document.getElementById("version")


print("Loading all versions... ", end="")
versions_all = json.load(
get("https://api.github.com/repos/dragoncoder047/schemascii/contents/dist"))
versions_to_wheel_map = dict(
zip(map(itemgetter("name"), versions_all), map(itemgetter("path"), versions_all)))
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)

for ver in all_versions:
opt = js.document.createElement("option")
opt.value = opt.textContent = ver
ver_switcher.append(opt)

ver_switcher.value = latest_version
await micropip.install(versions_to_wheel_map[latest_version])


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)

source.removeAttribute("disabled")
css_box.removeAttribute("disabled")
console.textContent = "ready\n"
2 changes: 1 addition & 1 deletion test_data/test_charge_pump.txt.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 800f1f1

Please sign in to comment.