-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
autoload_static.py
71 lines (57 loc) · 2 KB
/
autoload_static.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from jinja2 import Template
from tornado.ioloop import IOLoop
from tornado.web import Application, RequestHandler
from bokeh.embed import autoload_static
from bokeh.plotting import figure
from bokeh.resources import CDN
from bokeh.sampledata.iris import flowers
from bokeh.util.browser import view
template = Template("""
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div>
The plot embedded below is a standalone plot that was embedded using
<fixed>autoload_static</fixed>. For more information see the
<a target="_blank" href="https://docs.bokeh.org/en/latest/docs/user_guide/embed.html#autoload-scripts">
documentation</a>.
</div>
{{ script|safe }}
</body>
</html>
""")
class IndexHandler(RequestHandler):
def initialize(self, script):
self.script = script
def get(self):
self.write(template.render(script=self.script))
# Normally, you might save the .js files to some location on disk, and serve
# them from there. Here we us this request handler, just to make the example
# completely self-contained.
class JSHandler(RequestHandler):
def initialize(self, js):
self.js = js
def get(self): self.write(self.js)
def make_plot():
colormap = {'setosa': 'red', 'versicolor': 'green', 'virginica': 'blue'}
colors = [colormap[x] for x in flowers['species']]
p = figure(title = "Iris Morphology")
p.xaxis.axis_label = 'Petal Length'
p.yaxis.axis_label = 'Petal Width'
p.circle(flowers["petal_length"], flowers["petal_width"],
color=colors, fill_alpha=0.2, size=10)
return p
if __name__ == '__main__':
print('Opening Tornado app with embedded Bokeh plot on http://localhost:8080/')
js, script = autoload_static(make_plot(), CDN, "embed.js")
app = Application([
(r"/", IndexHandler, dict(script=script)),
(r"/embed.js", JSHandler, dict(js=js)),
])
app.listen(8080)
io_loop = IOLoop.current()
io_loop.add_callback(view, "http://localhost:8080/")
io_loop.start()