Compare commits

...

4 Commits

Author SHA1 Message Date
pre-commit-ci[bot]
a9e1abf6d1 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2023-11-13 16:34:53 +00:00
Andrea Giammarchi
24da768959 Merge branch 'main' into fpliger/example-panel-worker 2023-11-13 17:31:14 +01:00
Fabio Pliger
72955bced9 add panel.py logic 2023-08-10 16:37:43 -05:00
Fabio Pliger
c63c4043d3 add html and toml files 2023-08-10 16:32:48 -05:00
3 changed files with 154 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
import json
import js
import panel as pn
from js import JSON
from panel.io.pyodide import init_doc, write_doc
from polyscript import xworker
from pyodide.ffi import create_once_callable, create_proxy, to_js
from pyscript import document, window
js.document = document
init_doc()
print("Hello from panel.py")
slider = pn.widgets.FloatSlider(start=0, end=10, name="Amplitude")
def callback(new):
print(f"Amplitude is: {new}")
return f"Amplitude is: {new}"
print("made this far...")
pn.Row(slider, pn.bind(callback, slider)).servable(target="simple_app")
# ------ END OF PANEL CODE ------
docs_json_str, render_items_str, root_ids_str = await write_doc()
docs_json = JSON.parse(docs_json_str) # .as_object_map()
# render_items = to_js(JSON.parse(render_items_str), depth=-1, pyproxies=None, create_pyproxies=False, dict_converter=js.Object.fromEntries)#.as_object_map()
root_ids = JSON.parse(root_ids_str) # .as_object_map()
# docs_json = json.loads(docs_json_str)
render_items = json.loads(render_items_str)
# root_ids = json.loads(root_ids_str)
print(type(render_items))
root_elements = document.querySelectorAll("[data-root-id]")
data_roots = []
for el in root_elements:
el.innerHTML = ""
data_roots.append([el.getAttribute("data-root-id"), el.id])
roots = {root_ids[i]: root for i, root in enumerate(data_roots)}
print("Quick check")
print(roots)
print(root_ids)
print(render_items)
# render_items[0]['roots'] = roots
# render_items[0]['root_ids'] = root_ids
# render_items[0].roots = to_js(roots)
# render_items[0].root_ids = to_js(root_ids)
# print(roots)
# print(data_roots)
print(docs_json)
print(render_items)
print("here....")
# views = await window.Bokeh.embed.embed_items(to_js(docs_json), to_js(render_items))
views = await window.Bokeh.embed.embed_items(
docs_json, # to_js(docs_json, depth=-1, pyproxies=None, create_pyproxies=False, dict_converter=js.Object.fromEntries),
to_js(
render_items,
depth=-1,
pyproxies=None,
create_pyproxies=False,
dict_converter=js.Object.fromEntries,
),
)
# Experiments back to main thread
# await xworker.sync.render_full(docs_json_str, render_items_str, root_ids_str)
print("made it to the end")
print(docs_json)

View File

@@ -0,0 +1,5 @@
packages = [
"https://cdn.holoviz.org/panel/0.14.3/dist/wheels/bokeh-2.4.3-py3-none-any.whl",
"numpy",
"panel==0.14.1"
]

View File

@@ -0,0 +1,71 @@
<html>
<head>
<title>Panel Example</title>
<meta charset="iso-8859-1" />
<link rel="icon" type="image/x-icon" href="./favicon.png" />
</head>
<body>
<section class="pyscript">
<div id="simple_app"></div>
<py-tutor>
<script defer src="https://cdn.bokeh.org/bokeh/release/bokeh-2.4.3.js"></script>
<script defer src="https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.4.3.min.js"></script>
<script defer src="https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.4.3.min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/@holoviz/panel@0.14.1/dist/panel.min.js"></script>
<script type="module" src="../../core.js"></script>
<!-- <script
type="module"
src="https://esm.sh/@pyscript/core@latest/core.js"
></script> -->
<script type="py" worker="./panel.py" config="./panel.toml" async></script>
<!-- Code below borrowed for reference from example working on polyscript directly -->
<!-- <script type="micropython">
from pyscript import PyWorker
import json
import js
w = PyWorker('./panel.py', **{'type': 'pyodide', 'async': True, 'config': './panel.toml'})
# xworker.window made the following completely unnecessary
document = js.document
async def render_full(docs_json_str, render_items_str, root_ids_str):
docs_json = json.loads(docs_json_str)
render_items = json.loads(render_items_str)
root_ids = json.loads(root_ids_str)
print(type(render_items))
root_elements = document.querySelectorAll('[data-root-id]')
data_roots = []
for el in root_elements:
el.innerHTML = ''
data_roots.append([el.getAttribute('data-root-id'), el.id])
roots = {root_ids[i]: root for i, root in enumerate(data_roots)}
print("Quick check")
render_items[0]['roots'] = roots
render_items[0]['root_ids'] = root_ids
print("here....")
views = await js.Bokeh.embed.embed_items(docs_json, render_items)
def render(docs_json, render_items):
print(f"GOT DATA: {docs_json}")
print(f"GOT ITEMS: {render_items}")
await js.Bokeh.embed.embed_items(json.loads(docs_json), json.loads(render_items))
# views = await xworker.window.Bokeh.embed.embed_items(create_proxy(docs_json), create_proxy(render_items))
w.sync.render = render
w.sync.render_full = render_full
</script> -->
</py-tutor>
</section>
</body>
</html>