Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 079d33e

Browse files
committed
Add interactive bokeh example
1 parent 60232bf commit 079d33e

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<html><head>
2+
<title>Bokeh Example</title>
3+
<meta charset="iso-8859-1">
4+
<script type="text/javascript" src="https://cdn.bokeh.org/bokeh/release/bokeh-2.4.2.js"></script>
5+
<script type="text/javascript" src="https://cdn.bokeh.org/bokeh/release/bokeh-gl-2.4.2.min.js"></script>
6+
<script type="text/javascript" src="https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.4.2.min.js"></script>
7+
<script type="text/javascript" src="https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.4.2.min.js"></script>
8+
<script type="text/javascript" src="https://cdn.bokeh.org/bokeh/release/bokeh-mathjax-2.4.2.min.js"></script>
9+
10+
<script type="text/javascript">
11+
Bokeh.set_log_level("info");
12+
</script>
13+
<link rel="stylesheet" href="build/pyscript.css" />
14+
15+
<script defer src="build/pyscript.js"></script>
16+
17+
</head>
18+
<body>
19+
<py-env>
20+
- bokeh
21+
- numpy
22+
</py-env>
23+
<h1>Bokeh Example</h1>
24+
<div id="myplot"></div>
25+
26+
<py-script>
27+
import json
28+
import pyodide
29+
30+
from js import Bokeh, console, JSON
31+
32+
import bokeh
33+
34+
from bokeh.document import Document
35+
from bokeh.embed.util import OutputDocumentFor, standalone_docs_json
36+
from bokeh.models import Slider, Div
37+
from bokeh.layouts import Row
38+
from bokeh.protocol.messages.patch_doc import process_document_events
39+
40+
# create a new plot with default tools, using figure
41+
p = Slider(start=0.1, end=10, value=1, step=.1, title="Amplitude")
42+
div = Div(text=f'Amplitude is: {p.value}')
43+
44+
def callback(attr, old, new):
45+
div.text = f'Amplitude is: {new}'
46+
47+
p.on_change('value', callback)
48+
49+
row = Row(children=[p, div])
50+
51+
print("about to embed")
52+
53+
def doc_json(model, target):
54+
with OutputDocumentFor([model]) as doc:
55+
doc.title = ""
56+
docs_json = standalone_docs_json([model])
57+
58+
doc_json = list(docs_json.values())[0]
59+
root_id = doc_json['roots']['root_ids'][0]
60+
61+
return doc, json.dumps(dict(
62+
target_id = target,
63+
root_id = root_id,
64+
doc = doc_json,
65+
version = bokeh.__version__,
66+
))
67+
68+
def link_docs(pydoc, jsdoc):
69+
70+
def jssync(event):
71+
if (event.setter_id is not None):
72+
return
73+
events = [event]
74+
json_patch = jsdoc.create_json_patch_string(pyodide.to_js(events))
75+
pydoc.apply_json_patch(json.loads(json_patch))
76+
77+
jsdoc.on_change(pyodide.create_proxy(jssync), pyodide.to_js(False))
78+
79+
def pysync(event):
80+
json_patch = process_document_events([event], use_buffers=False)[0]
81+
82+
jsdoc.apply_json_patch(JSON.parse(json_patch), {}, setter_id='js')
83+
84+
pydoc.on_change(pysync)
85+
86+
async def show(plot, target):
87+
pydoc, model_json = doc_json(plot, target)
88+
views = await Bokeh.embed.embed_item(JSON.parse(model_json))
89+
print("Done embedding...")
90+
jsdoc = views[0].model.document
91+
link_docs(pydoc, jsdoc)
92+
93+
pyscript.run_until_complete(show(row, 'myplot'))
94+
95+
</py-script>
96+
97+
</body>
98+
</html>

0 commit comments

Comments
 (0)