Deploy Bokeh app online

Hi,

I was hoping someone could maybe point me in the right direction. I have built a bokeh app and I was hoping of deploying online but I am having trouble getting it to work. Originally I was hoping to use flask to deploy it but haven’t had much luck. Below if the code of my app:

from functions import *
#from bokeh.charts import Bar
from bokeh.plotting import figure
from bokeh.models import HoverTool, ColumnDataSource
from bokeh.layouts import layout, gridplot, widgetbox
from bokeh.models.widgets import Div, RangeSlider, RadioButtonGroup, TextInput, Paragraph, PreText
from bokeh.io import curdoc, set_curdoc
from bokeh.embed import components
from fractions import Fraction
from scipy.signal import savgol_filter
from flask import Flask, render_template
import holoviews as hv
hv.extension(‘bokeh’)

Default distribution and plot details

n=[5]
m=[3]
M=int(7*10**4)
dist = [0,0,1]
pri = choose_dist(dist)
y = power_function(n,m,M)
pf = np.clip(savgol_filter(y[0],11,9),0,1)
y2 = pos_pred_den(pri,pf)
y3 = neg_pred_den(pri,pf)
y4 = pred_func(y2)
y5 = pred_func(y3)

stack = [‘stage 1’,‘passed’]
dat = make_data(P,stack,y[1])
renderer = hv.renderer(‘bokeh’).instance(mode=‘server’)

source = ColumnDataSource(data=dict(
P=P,
pri=pri,
y=pf,
y4=y4,
y5=y5
))
hover0 = HoverTool(tooltips=[
(“TRP”, “@P”),
(“Density”, “@pri”),
],
mode = ‘vline’)
hover1 = HoverTool(tooltips=[
(“TRP”, “@P”),
(“PTS”, “@y”),
],
mode = ‘vline’)
hover2 = HoverTool(tooltips=[
(“TRP”, “@P”),
(“PP”, “@y4”),
],
mode = ‘vline’)
hover3 = HoverTool(tooltips=[
(“TRP”, “@P”),
(“PP”, “@y5”),
],
mode = ‘vline’)
hover4 = [
(‘TRP’, ‘@P’),
(‘Y’, ‘@Pb’),
(‘stage’,’@stage’),
]

Plots

p0 = figure(plot_width = 400, plot_height = 400, title=“Prior Distribution”,
tools=[hover0], x_axis_label = ‘True Response Probability’, y_axis_label = ‘Probability Density’)
p0.line(‘P’, ‘pri’, line_width=2, line_color=‘orange’, source=source)
p1 = figure(plot_width=400, plot_height= 400, title=“Power Function”,
tools=[hover1], x_axis_label=‘True Response Probability’, y_axis_label=‘Probability that trial is successful’)
p1.line(‘P’, ‘y’, line_width=2, line_color=‘green’, source=source)
p2 = figure(plot_width=400, plot_height= 400, title=“Positive Predictive Function”,
tools=[hover2], x_axis_label=‘True Response Probability (\u2265x)’, y_axis_label=‘Posterior probability’)
p2.line(‘P’, ‘y4’, line_width=2, line_color=‘blue’, source=source)
p3 = figure(plot_width=400, plot_height= 400, title=“Negative Predictive Function”,
tools=[hover3], x_axis_label=‘True Response Probability (\u2265x)’, y_axis_label=‘Posterior probability’)
p3.line(‘P’, ‘y5’, line_width=2, line_color=‘red’, source=source)

def BBars(st,p,y):
dat = make_data(p,st,y)
return hv.Bars(dat, kdims = [‘True Response Probability’, ‘stage’], vdims = [‘Proportion’], group = “Distribution of end stages”, xrotation = 90)(plot=dict(tools=[‘hover’], stack_index=‘stage’, height=900, width=1200))

stream = hv.streams.Stream.define(‘stacks’,st = stack, p=P, y=y[1])()
p4 = hv.DynamicMap(BBars, streams=[stream])
bar_plot = renderer.get_plot(p4)

text = Paragraph(text =""“The Number of Participitants field recognizes stages with commas (i.e. 5,6
indicates a first stage of 5 participants and a second of 6). The same goes
for Minimum TOTAL repsonses to pass (i.e. 3,6 indicates that a minimum of 3
must pass the first stage before continuing to the second and a total of at
least 6 must pass for the trial to be successful).”"", width = 400)

div = Div(text=""“

Sequential Trial Simulator

Application for simulating drug trials


Sequential studies are important in testing drug safety. Trials usaully consist of a n of stages,
in each stage mi participants are tested (note the number of participants can vary
between stages) and a minimum number of participants that must have a positive response before
moving onto the next stage. Note that these minimum number of participants are summed across
stages. The below app allows a pratictioner to design their own experiment and specify any prior
knowledge they may have. The resulting graphs depict the probability that the trial is successful
given a true response probability (Power Function), the probability that the true response rate
is greater than some value (x-axis) given the trial succeeded (Positive Predictive Function), the
probability that the true response rate is greater than or equal to some value (x-axis) given the
trial failed (Negative Predictive Function) and the final bar chart depicts the probability that a
trial is stopped in a certain stage (Distribution of end stages).

“””, width = 800)

widgets

button_group = RadioButtonGroup(labels=[“Uniform”, “Beta”, “Logit”], active = 0)
alpha = TextInput(title=“Min, \u03B1, \u03BC”, value = ‘0’, width=400)
betta = TextInput(title=“Max, \u03B2, \u03C3”, value = ‘1’, width=400)
num_participants = TextInput(title=“Number of Participants”, value = str(n[0]), width=400)
num_min = TextInput(title=“Mininum TOTAL responses to pass”, value = str(m[0]), width=400)

upper_widgets = widgetbox(button_group,alpha,betta)
lower_widgets = widgetbox(num_participants,num_min)

def update_lower_graphs(pri):
y = source.data[‘y’]
y2 = pos_pred_den(pri,y)
y3 = neg_pred_den(pri,y)
y4 = pred_func(y2)
y5 = pred_func(y3)
source.data[‘y4’] = y4
source.data[‘y5’] = y5

update functions

def update_den(attrname, old, new):
param1 = float(Fraction(alpha.value))
param2 = float(Fraction(betta.value))
den = button_group.active
if den == 0 :
pri = unif(P, param1, param2)
elif den == 1 :
pri = beta.pdf(P, param1, param2)
else:
pri = logit(P, param1, param2)
y2 = pos_pred_den(pri,pf)
y3 = neg_pred_den(pri,pf)
y4 = pred_func(y2)
y5 = pred_func(y3)
source.data[‘pri’] = pri
update_lower_graphs(pri)

def update_dist(attrname, old, new):
den = button_group.active
li = doc.roots[0].children[0].children
if den == 0:
alpha.value = str(0)
betta.value = str(1)
start = float(alpha.value)
end = float(betta.value)
pri = unif(P, start, end)
source.data[‘pri’] = pri
update_lower_graphs(pri)
elif den == 1:
alpha.value = str(2)
betta.value = str(2)
alf = float(alpha.value)
bet = float(betta.value)
pri = beta.pdf(P,alf,bet)
source.data[‘pri’]=pri
update_lower_graphs(pri)
else:
alpha.value = str(0)
betta.value = str(0.5)
mu = float(alpha.value)
sig = float(betta.value)
pri = logit(P, mu, sig)
source.data[‘pri’]=pri
update_lower_graphs(pri)

def update_preds(attrname, old, new):
pri = source.data[‘pri’]
y2 = pos_pred_den(pri,pf)
y3 = neg_pred_den(pri,pf)
y4 = pred_func(y2)
y5 = pred_func(y3)
source.data[‘y4’] = y4
source.data[‘y5’] = y5

def update_participants(attrname, old, new):
n = list(map(int,num_participants.value.split(’,’)))
m = list(map(int,num_min.value.split(’,’)))
if len(n) == len(m):
stck =
for _ in range(len(n)):
st = _+1
strr = ‘stage %d’ % st
stck.append(strr)
stck.append(‘passed’)
pri = source.data[‘pri’]
y = power_function(n,m,M)
pf = np.clip(savgol_filter(y[0],11,9),0,1)
y2 = pos_pred_den(pri,pf)
y3 = neg_pred_den(pri,pf)
y4 = pred_func(y2)
y5 = pred_func(y3)
stream.event(st=stck, p=P, y=y[1])
source.data[‘y’] = pf
source.data[‘y4’] = y4
source.data[‘y5’] = y5

button_group.on_change(‘active’, update_dist)
alpha.on_change(‘value’, update_den)
betta.on_change(‘value’, update_den)
num_min.on_change(‘value’, update_participants)
num_participants.on_change(‘value’, update_participants)

model = layout([div],[upper_widgets, p0], [lower_widgets, text], [p1,p2,p3], [bar_plot.state])
doc = curdoc()
doc.add_root(model)
doc.title = “Sequential Trial Simulator”

script, divy = components(doc)

Bokeh server apps cannot be embedded using components. To share an app from a bokeh server you can use server_document or server_session. Not that the Bokeh server has to always be running as well, it is the thing that runs the Python callbacks in your app.

Bryan

···

On Sep 27, 2017, at 17:43, [email protected] wrote:

Hi,

I was hoping someone could maybe point me in the right direction. I have built a bokeh app and I was hoping of deploying online but I am having trouble getting it to work. Originally I was hoping to use flask to deploy it but haven’t had much luck. Below if the code of my app:

from functions import *
#from bokeh.charts import Bar
from bokeh.plotting import figure
from bokeh.models import HoverTool, ColumnDataSource
from bokeh.layouts import layout, gridplot, widgetbox
from bokeh.models.widgets import Div, RangeSlider, RadioButtonGroup, TextInput, Paragraph, PreText
from bokeh.io import curdoc, set_curdoc
from bokeh.embed import components
from fractions import Fraction
from scipy.signal import savgol_filter
from flask import Flask, render_template
import holoviews as hv
hv.extension(‘bokeh’)

Default distribution and plot details

n=[5]
m=[3]
M=int(7*10**4)
dist = [0,0,1]
pri = choose_dist(dist)
y = power_function(n,m,M)
pf = np.clip(savgol_filter(y[0],11,9),0,1)
y2 = pos_pred_den(pri,pf)
y3 = neg_pred_den(pri,pf)
y4 = pred_func(y2)
y5 = pred_func(y3)

stack = [‘stage 1’,‘passed’]
dat = make_data(P,stack,y[1])
renderer = hv.renderer(‘bokeh’).instance(mode=‘server’)

source = ColumnDataSource(data=dict(
P=P,
pri=pri,
y=pf,
y4=y4,
y5=y5
))
hover0 = HoverTool(tooltips=[
(“TRP”, “@P”),
(“Density”, “@pri”),
],
mode = ‘vline’)
hover1 = HoverTool(tooltips=[
(“TRP”, “@P”),
(“PTS”, “@y”),
],
mode = ‘vline’)
hover2 = HoverTool(tooltips=[
(“TRP”, “@P”),
(“PP”, “@y4”),
],
mode = ‘vline’)
hover3 = HoverTool(tooltips=[
(“TRP”, “@P”),
(“PP”, “@y5”),
],
mode = ‘vline’)
hover4 = [
(‘TRP’, ‘@P’),
(‘Y’, ‘@Pb’),
(‘stage’,’@stage’),
]

Plots

p0 = figure(plot_width = 400, plot_height = 400, title=“Prior Distribution”,
tools=[hover0], x_axis_label = ‘True Response Probability’, y_axis_label = ‘Probability Density’)
p0.line(‘P’, ‘pri’, line_width=2, line_color=‘orange’, source=source)
p1 = figure(plot_width=400, plot_height= 400, title=“Power Function”,
tools=[hover1], x_axis_label=‘True Response Probability’, y_axis_label=‘Probability that trial is successful’)
p1.line(‘P’, ‘y’, line_width=2, line_color=‘green’, source=source)
p2 = figure(plot_width=400, plot_height= 400, title=“Positive Predictive Function”,
tools=[hover2], x_axis_label=‘True Response Probability (\u2265x)’, y_axis_label=‘Posterior probability’)
p2.line(‘P’, ‘y4’, line_width=2, line_color=‘blue’, source=source)
p3 = figure(plot_width=400, plot_height= 400, title=“Negative Predictive Function”,
tools=[hover3], x_axis_label=‘True Response Probability (\u2265x)’, y_axis_label=‘Posterior probability’)
p3.line(‘P’, ‘y5’, line_width=2, line_color=‘red’, source=source)

def BBars(st,p,y):
dat = make_data(p,st,y)
return hv.Bars(dat, kdims = [‘True Response Probability’, ‘stage’], vdims = [‘Proportion’], group = “Distribution of end stages”, xrotation = 90)(plot=dict(tools=[‘hover’], stack_index=‘stage’, height=900, width=1200))

stream = hv.streams.Stream.define(‘stacks’,st = stack, p=P, y=y[1])()
p4 = hv.DynamicMap(BBars, streams=[stream])
bar_plot = renderer.get_plot(p4)

text = Paragraph(text =""“The Number of Participitants field recognizes stages with commas (i.e. 5,6
indicates a first stage of 5 participants and a second of 6). The same goes
for Minimum TOTAL repsonses to pass (i.e. 3,6 indicates that a minimum of 3
must pass the first stage before continuing to the second and a total of at
least 6 must pass for the trial to be successful).”"", width = 400)

div = Div(text=""“

Sequential Trial Simulator

Application for simulating drug trials


Sequential studies are important in testing drug safety. Trials usaully consist of a n of stages,
in each stage mi participants are tested (note the number of participants can vary
between stages) and a minimum number of participants that must have a positive response before
moving onto the next stage. Note that these minimum number of participants are summed across
stages. The below app allows a pratictioner to design their own experiment and specify any prior
knowledge they may have. The resulting graphs depict the probability that the trial is successful
given a true response probability (Power Function), the probability that the true response rate
is greater than some value (x-axis) given the trial succeeded (Positive Predictive Function), the
probability that the true response rate is greater than or equal to some value (x-axis) given the
trial failed (Negative Predictive Function) and the final bar chart depicts the probability that a
trial is stopped in a certain stage (Distribution of end stages).

“””, width = 800)

widgets

button_group = RadioButtonGroup(labels=[“Uniform”, “Beta”, “Logit”], active = 0)
alpha = TextInput(title=“Min, \u03B1, \u03BC”, value = ‘0’, width=400)
betta = TextInput(title=“Max, \u03B2, \u03C3”, value = ‘1’, width=400)
num_participants = TextInput(title=“Number of Participants”, value = str(n[0]), width=400)
num_min = TextInput(title=“Mininum TOTAL responses to pass”, value = str(m[0]), width=400)

upper_widgets = widgetbox(button_group,alpha,betta)
lower_widgets = widgetbox(num_participants,num_min)

def update_lower_graphs(pri):
y = source.data[‘y’]
y2 = pos_pred_den(pri,y)
y3 = neg_pred_den(pri,y)
y4 = pred_func(y2)
y5 = pred_func(y3)
source.data[‘y4’] = y4
source.data[‘y5’] = y5

update functions

def update_den(attrname, old, new):
param1 = float(Fraction(alpha.value))
param2 = float(Fraction(betta.value))
den = button_group.active
if den == 0 :
pri = unif(P, param1, param2)
elif den == 1 :
pri = beta.pdf(P, param1, param2)
else:
pri = logit(P, param1, param2)
y2 = pos_pred_den(pri,pf)
y3 = neg_pred_den(pri,pf)
y4 = pred_func(y2)
y5 = pred_func(y3)
source.data[‘pri’] = pri
update_lower_graphs(pri)

def update_dist(attrname, old, new):
den = button_group.active
li = doc.roots[0].children[0].children
if den == 0:
alpha.value = str(0)
betta.value = str(1)
start = float(alpha.value)
end = float(betta.value)
pri = unif(P, start, end)
source.data[‘pri’] = pri
update_lower_graphs(pri)
elif den == 1:
alpha.value = str(2)
betta.value = str(2)
alf = float(alpha.value)
bet = float(betta.value)
pri = beta.pdf(P,alf,bet)
source.data[‘pri’]=pri
update_lower_graphs(pri)
else:
alpha.value = str(0)
betta.value = str(0.5)
mu = float(alpha.value)
sig = float(betta.value)
pri = logit(P, mu, sig)
source.data[‘pri’]=pri
update_lower_graphs(pri)

def update_preds(attrname, old, new):
pri = source.data[‘pri’]
y2 = pos_pred_den(pri,pf)
y3 = neg_pred_den(pri,pf)
y4 = pred_func(y2)
y5 = pred_func(y3)
source.data[‘y4’] = y4
source.data[‘y5’] = y5

def update_participants(attrname, old, new):
n = list(map(int,num_participants.value.split(’,’)))
m = list(map(int,num_min.value.split(’,’)))
if len(n) == len(m):
stck =
for _ in range(len(n)):
st = _+1
strr = ‘stage %d’ % st
stck.append(strr)
stck.append(‘passed’)
pri = source.data[‘pri’]
y = power_function(n,m,M)
pf = np.clip(savgol_filter(y[0],11,9),0,1)
y2 = pos_pred_den(pri,pf)
y3 = neg_pred_den(pri,pf)
y4 = pred_func(y2)
y5 = pred_func(y3)
stream.event(st=stck, p=P, y=y[1])
source.data[‘y’] = pf
source.data[‘y4’] = y4
source.data[‘y5’] = y5

button_group.on_change(‘active’, update_dist)
alpha.on_change(‘value’, update_den)
betta.on_change(‘value’, update_den)
num_min.on_change(‘value’, update_participants)
num_participants.on_change(‘value’, update_participants)

model = layout([div],[upper_widgets, p0], [lower_widgets, text], [p1,p2,p3], [bar_plot.state])
doc = curdoc()
doc.add_root(model)
doc.title = “Sequential Trial Simulator”

script, divy = components(doc)

You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/6b3f8b8b-5da4-431a-8c04-f6085e9df33f%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.