mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-20 02:37:41 -05:00
* updated MicroPython to latest in order to have `globals` API available * reduced code around helpers for both MicroPython and Pyodide as now these are more aligned * updated all dependencies and brought in latest [coincident/window](https://github.com/WebReflection/coincident#coincidentwindow) goodness to any `xworker`, preserving the `sync` previous behavior * using [@ungap/structured-clone/json](https://github.com/ungap/structured-clone#tojson) as *coincident* default `parse` and `stringify` utility to allow recursive and more complex data to travel back from the *Worker* (forward data is still fully [structured clone algorithm compatible](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)) * renamed all *plugin/s* references to *custom/s* as plugin as a word was too misleading * changed *custom types* helpers logic to allow any single node to have its own version of the interpreter wrapper, and all the extra fields it carries with it, including a way to augment every interpreter execution, among as every worker code execution * created a `custom` folder where I've landed the very first `pyscript.js` custom type * created an exhaustive test page to demonstrate the current abilities of *PyScript Next* among its ability to expose utilities that can be used to create *PyScript* plugins
72 lines
2.1 KiB
Python
72 lines
2.1 KiB
Python
import js
|
|
import matplotlib
|
|
|
|
try:
|
|
js.document
|
|
except AttributeError:
|
|
matplotlib.use("agg")
|
|
|
|
import base64
|
|
import io
|
|
|
|
import matplotlib.pyplot as plt
|
|
import matplotlib.tri as tri
|
|
import numpy as np
|
|
|
|
# First create the x and y coordinates of the points.
|
|
n_angles = 36
|
|
n_radii = 8
|
|
min_radius = 0.25
|
|
radii = np.linspace(min_radius, 0.95, n_radii)
|
|
|
|
angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)
|
|
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
|
|
angles[:, 1::2] += np.pi / n_angles
|
|
|
|
x = (radii * np.cos(angles)).flatten()
|
|
y = (radii * np.sin(angles)).flatten()
|
|
z = (np.cos(radii) * np.cos(3 * angles)).flatten()
|
|
|
|
# Create the Triangulation; no triangles so Delaunay triangulation created.
|
|
triang = tri.Triangulation(x, y)
|
|
|
|
# Mask off unwanted triangles.
|
|
triang.set_mask(
|
|
np.hypot(x[triang.triangles].mean(axis=1), y[triang.triangles].mean(axis=1))
|
|
< min_radius
|
|
)
|
|
|
|
fig1, ax1 = plt.subplots()
|
|
ax1.set_aspect("equal")
|
|
tpc = ax1.tripcolor(triang, z, shading="flat")
|
|
fig1.colorbar(tpc)
|
|
ax1.set_title("tripcolor of Delaunay triangulation, flat shading")
|
|
|
|
buf = io.BytesIO()
|
|
plt.savefig(buf, format="png")
|
|
buf.seek(0)
|
|
|
|
# how it was (including main thread counter part)
|
|
# js.xworker.postMessage(
|
|
# "data:image/png;base64," + base64.b64encode(buf.read()).decode("UTF-8")
|
|
# )
|
|
|
|
# how it is now via structured coincident/window
|
|
document = xworker.window.document
|
|
img = document.createElement("img")
|
|
img.style.transform = "scale(.5)"
|
|
img.src = "data:image/png;base64," + base64.b64encode(buf.read()).decode("UTF-8")
|
|
|
|
# document.body.append(img) fails for some reason I don't understand
|
|
# same would be for document.querySelector("#image").append(img)
|
|
# those would work in any JS counterpart though ... but for demo sake:
|
|
document.querySelector("#image").innerHTML = img.outerHTML
|
|
|
|
# about pyodide issue
|
|
print(xworker.window.Array(1, 2)) # this works
|
|
xworker.window.console.log(1, 2) # this works too
|
|
# xworker.window.console.log(xworker.window.Array(1, 2)) # this doesn't
|
|
# xworker.window.console.log([1, 2]) # also this doesn't
|
|
|
|
# xworker.window.console.log(to_js({})) # this doesn't neither
|