Hi guys I was trying to do the same, but on my Jupyter notebook.
My solution based on this thread and this GitHub Example is not working.
I posted this question on StackOverflow, with a Minimal Example, the first answer was helpful to clear my ideas but not completely.
For example, one helpful suggestion from the author in the Stack Overflow question is:
push_notebook
``
“You should not make a new plot every time! The entire purpose of both
and the Bokeh server is that updates can happen efficiently without making a whole new plot every time. You should be updating the data and attributes of the existing plot.”
Furthermore, what is the first plot that will be plotted in this example from the dataframe, if nothing is pre-selcted?
I will attach here I small piece of code on what I am trying to do:
import pandas as pd
dct={‘Date’ : [“2018-01-07”, “2018-01-12”, “2018-01-13”, “2018-01-14”, “2018-01-20”, “2018-01-24”],‘Activity’ : [‘A’,‘B’,‘A’,‘B’,‘A’,‘B’],‘Count’ : [1, 2, 5, 3, 7, 1]}
df=pd.DataFrame(dct)
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, Select
from bokeh.plotting import figure,curdoc
from bokeh.io import show,push_notebook, output_notebook,output_file
activity_list=df[‘Activity’].unique().tolist().copy()
activity_selected=‘A’
def modify_doc(doc):
def make_plot(cdf):
plot = figure()
plot.vbar(x=cdf.Date, top=cdf.Count, width=0.9)
push_notebook()
show(plot, notebook_handle = True)
return plot
def update_plot(attr, old, new):
activity = select.value
sdf = df.copy()
sdf = sdf[sdf[‘Activity’] == activity]
layout.children[0] = make_plot(sdf)
select = Select(title=‘Select Activity’, value=activity_selected, options=activity_list)
select.on_change(‘value’, update_plot)
p=make_plot(df)
layout=column(select, p)
doc.add_root(layout)
show(modify_doc)
output_notebook()
``
···
On Thursday, 29 June 2017 20:39:10 UTC+2, Tyler Nickerson wrote:
Hey Aadesh,
The error appears to be caused by this line within update_plot
:
sdf = sdf[cdf['State'] == state]
sdf
is not assigned anywhere prior to this and cdf
only exists in the scope of make_plot
. A possible solution is creating a copy of df
within update_plot
and using that instead. Here’s an example of what I mean:
def update_plot(attr, old, new):
state = select.value
sdf = df.copy()
sdf = sdf[sdf['State' ] == state]
make_plot(sdf)
Depending on your use case you may still need to tweak this more.
On Thu, Jun 29, 2017 at 10:22 AM, [email protected] wrote:
We are trying to the same thing and we are stucked on this issue: Tyler and NSR Murthy if you can help us it would be great.
import pandas as pd
from bokeh.io import push_notebook, show, output_notebook, output_file
from bokeh.charts import Bar
from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Button, TextInput, Slider, Dropdown, Select
from bokeh.layouts import widgetbox ,row, column
df = pd.read_csv(‘data.csv’)
df.head(19);
function to make a plot
def make_plot(cdf):
plot = Bar(cdf, label = ‘City’, values =‘Metric1’, title = ‘BarPlot’, legend=False, width=800, height=800)
return plot
function to update data
def update_plot(attr, old, new):
state = select.value
sdf = sdf[cdf[‘State’] == state]
layout.children[0] = make_plot(sdf)
In[13]:
create a widget
instead of ipywidget can we use bokeh widget
create a function to update the plot based on the state selected
create a lists to assign to plot
stateslist = df[“State”].unique().tolist();
select = Select(title=“Select Name:”, options= stateslist)
In[15]:
plot it
plot = make_plot(df)
update values usinsg widgets
select.on_change(‘value’,update_plot)
picture the layout of plot and controls on ui
controls = column(select)
create a layout instance containing the complete app layout
layout = row(plot,controls)
curdoc().add_root(layout)
``
The error message we are getting is:
error handling message Message ‘PATCH-DOC’ (revision 1): UnboundLocalError(“local variable ‘sdf’ referenced before assignment”,)
``
Any thought or suggestion to fix this error or do you have a working code we can look for?
On Thursday, March 9, 2017 at 5:13:28 PM UTC-5, NSR Murthy wrote:
Tyler
The layout trick did it for me. Thank you so much . I also had to make minor changes to comment all the notebook commands.
It is tricky to see that output_notebook, show etc are only relevant to jupyer notebook and u have to comment that while using Bokeh Serve.
curdoc is the most abstract function i saw in Bokeh. I come from non development background and so could be my issue to pick up things.
On Monday, March 6, 2017 at 3:36:55 PM UTC-5, NSR Murthy wrote:
I am not able to figure how do i update the data frame based on a widget Selection.
I would like user to select a value from select list and then based on that value update the bar chart.
To do so i need to pass the sliced dataframe to the bar chart.
so step1 : show the default plot and select box.
step2: User selects a state value
step3: based on State value selected slice the data frame so that only new list of values are passed to bar plot.
I struggle in Step3 becaue bar chart is not taking column data source. and am not able to to figure how to use the Bokeh widget value to update dataframe.
declare imports
import pandas as pd
from bokeh.io import push_notebook, show, output_notebook, output_file
from bokeh.charts import Bar
from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Button, TextInput, Slider, Dropdown, Select
from bokeh.layouts import widgetbox ,row, column
In[3]:
get data
df = pd.read_excel(‘…/data.xlsx’)
df.head(19);
Name
City
State
0
ABC
Abington
PA
32000
1
DEF
Phoenix
AZ
0
2
DEF
Glendale
AZ
13097
3
ABC
Phoenix
AZ
9475
4
EFG
Phoenix
AZ
3975
5
GHI
Phoenix
AZ
5226
6
IHJ
Goodyear
AZ
10949
7
IJK
Crowley
LA
2487
8
KLM
Altamonte Springs
FL
292094
9
ABC
Oak Lawn
IL
37937
10
ABC
Libertyville
IL
16698
11
DEF
Downers Grove
IL
14159
12
DEF
Barrington
IL
9652
13
ABC
Downers Grove
IL
160902
14
EFG
Park Ridge
IL
26916
15
GHI
Hazel Crest
IL
10260
16
IHJ
Chicago
IL
9282
17
IJK
Anchorage
AK
7422
18
KLM
Anchorage
AK
5376
In[11]:
function to make a plot
def make_plot(cdf):
plot = Bar(cdf, label = 'City', values ='Metric1', title = 'BarPlot', legend=False, width=800, height=800)
push_notebook()
show(plot, notebook_handle = True)
return plot
function to update data
def update_plot(attr, old, new):
state = select.value
sdf = sdf[cdf['State'] == state]
make_plot(sdf)
In[13]:
#create a widget
instead of ipywidget can we use bokeh widget
create a function to update the plot based on the state selected
create a lists to assign to plot
stateslist = df[“State”].unique().tolist();
select = Select(title=“Select Name:”, options= stateslist)
In[15]:
plot it
plot = make_plot(df)
update values usinsg widgets
select.on_change(‘value’,update_plot)
picture the layout of plot and controls on ui
controls = column(select)
curdoc().add_root(row(plot,controls))
ps: Bokeh is wonderful to use til now., As i keep discovering new ways to express my data analysis.
–
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/00ed0b9c-c90f-4740-94f4-fe4ae66746d8%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.