-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbokeh_basic.py
145 lines (109 loc) · 3.81 KB
/
bokeh_basic.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# Run this example easily with "nitro run URL".
# Get the nitro CLI: https://nitro.h2o.ai/cli/
#
# Like Nitro? Please star us on Github: https://github.com/h2oai/nitro
#
# ===
# About: How to use Bokeh in Nitro apps
# Author: Prithvi Prabhu <[email protected]>
# License: Apache-2.0
# Source: https://github.com/h2oai/nitro-bokeh/examples
# Keywords: [visualization]
#
# Setup:
# FILE requirements.txt EOF
# bokeh
# numpy
# pandas
# Flask>=2
# simple-websocket>=0.5
# h2o-nitro[web]
# h2o-nitro-bokeh>=0.1
# EOF
# RUN python -m pip install -r requirements.txt
# ENV FLASK_APP bokeh_basic.py
# ENV FLASK_ENV development
# START python -m flask run
# ===
import numpy as np
import pandas as pd
import simple_websocket
from bokeh.models import HoverTool
from bokeh.palettes import brewer
from bokeh.plotting import figure
from bokeh.sampledata.penguins import data
from bokeh.transform import factor_cmap, factor_mark
from flask import Flask, request, send_from_directory
# ----- Nitro app -----
from h2o_nitro import View
from h2o_nitro_web import web_directory
from h2o_nitro_bokeh import bokeh_plugin, bokeh_box
# Entry point
def main(view: View):
# Show plots one by one.
view(bokeh_box(make_bokeh_scatterplot()))
view(bokeh_box(make_bokeh_hexbin_plot()))
view(bokeh_box(make_bokeh_stacked_area()))
# Nitro instance
nitro = View(
main,
title='Nitro + Bokeh',
caption='A minimal example',
plugins=[bokeh_plugin()], # Include the Bokeh plugin
)
# ----- Bokeh plotting routines -----
# Source: http://docs.bokeh.org/en/latest/docs/gallery/marker_map.html
def make_bokeh_scatterplot():
SPECIES = sorted(data.species.unique())
MARKERS = ['hex', 'circle_x', 'triangle']
p = figure(title="Penguin size", background_fill_color="#fafafa")
p.xaxis.axis_label = 'Flipper Length (mm)'
p.yaxis.axis_label = 'Body Mass (g)'
p.scatter("flipper_length_mm", "body_mass_g", source=data,
legend_group="species", fill_alpha=0.4, size=12,
marker=factor_mark('species', MARKERS, SPECIES),
color=factor_cmap('species', 'Category10_3', SPECIES))
p.legend.location = "top_left"
p.legend.title = "Species"
return p
# Source: http://docs.bokeh.org/en/latest/docs/gallery/hexbin.html
def make_bokeh_hexbin_plot():
n = 500
x = 2 + 2 * np.random.standard_normal(n)
y = 2 + 2 * np.random.standard_normal(n)
p = figure(title="Hexbin for 500 points", match_aspect=True,
tools="wheel_zoom,reset", background_fill_color='#440154')
p.grid.visible = False
r, bins = p.hexbin(x, y, size=0.5, hover_color="pink", hover_alpha=0.8)
p.circle(x, y, color="white", size=1)
p.add_tools(HoverTool(
tooltips=[("count", "@c"), ("(q,r)", "(@q, @r)")],
mode="mouse", point_policy="follow_mouse", renderers=[r]
))
return p
# Source: http://docs.bokeh.org/en/latest/docs/gallery/stacked_area.html
def make_bokeh_stacked_area():
n = 10
df = pd.DataFrame(np.random.randint(10, 100, size=(15, n))).add_prefix('y')
p = figure(x_range=(0, len(df) - 1), y_range=(0, 800))
p.grid.minor_grid_line_color = '#eeeeee'
names = [f"y{i}" for i in range(n)]
p.varea_stack(stackers=names, x='index', color=brewer['Spectral'][n], legend_label=names, source=df)
p.legend.orientation = "horizontal"
p.legend.background_fill_color = "#fafafa"
return p
# ----- Flask boilerplate -----
app = Flask(__name__, static_folder=web_directory, static_url_path='')
@app.route('/')
def home_page():
return send_from_directory(web_directory, 'index.html')
@app.route('/nitro', websocket=True)
def socket():
ws = simple_websocket.Server(request.environ)
try:
nitro.serve(ws.send, ws.receive)
except simple_websocket.ConnectionClosed:
pass
return ''
if __name__ == '__main__':
app.run()