how to update the bar chart that has dataframe as source with Bokeh Select widget

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.

It looks like the issue is that the callback creates a new plot but doesn’t add it to the current document. You can do this creating a layout instance before you add everything to the current doc.

# 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)

This will give you access to the layout instance in your callback; allowing the layout to be updated.

def update_plot(attr, old, new):
    state = select.value
sdf = sdf[cdf['State'    ] == state]
# update the layout by replacing the previous chart
    layout.children[0] = make_plot(sdf)

···

On Mon, Mar 6, 2017 at 12:17 PM, [email protected] 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/99ce830b-e6df-495d-8a93-8afc6343d4a4%40continuum.io.

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

Tyler

Thank you so much for the solution . I will try to implement this and update you with my outcome.

···

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.

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.

curdoc()
is an abstraction that makes more sense as part of a web interface than
a plotting library. I think a big stumbling point in learning to use Bokeh from a non-development perspective is understanding that the rendered app is created and updated by a browser-side javascript client.
Bokeh abstracts this in a way that our python code models browser-side objects to create an interface that feels like a plotting library written in Python. These documentation pages 1 2 3 helped me get my head around how Bokeh, Bokeh Server and Bokeh.js work as a whole.

···

On Thu, Mar 9, 2017 at 2:13 PM, NSR Murthy [email protected] 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/ce477c0a-ba0e-4337-8bec-1f864dd0ce27%40continuum.io.

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

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.

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.

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.

Just update the ColumnDataSource by streaming new or modified data like in this live-plotting example:
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, curdoc
from datetime import datetime
import random

source = ColumnDataSource(dict(time = [datetime.now()], value = [random.randint(5, 10)]))
plot = figure(x_axis_type = ‘datetime’)
plot.line(x = ‘time’, y = ‘value’, source = source)

def update():
source.stream(dict(time = [datetime.now()], value = [random.randint(5, 10)]))

curdoc().add_root(plot)
curdoc().add_periodic_callback(update, 1000)

``

···

On Monday, February 18, 2019 at 9:44:51 AM UTC+1, Andrea Ciufo wrote:

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.

Yes, I have done something like that, but in this live-plotting example the system return me just a:

<bokeh.server.callbacks.PeriodicCallback at 0xf964748>

``

I think because is not for jupyter but for a Bokeh Server.

I tried a different approach, and now it works:

activity_list=df[‘Activity’].unique().tolist().copy()

df[‘Date’]=pd.to_datetime(df[‘Date’])

activity_selected=‘A’

def modify_doc(doc):

df_r=df.copy()

source = ColumnDataSource(data=df_r)

plot=figure(title='Daily Hours',x_axis_type="datetime")

plot.vbar(x="Date", top="Count",source=source, width=4)

def update_plot(attr, old, new):

    activity =  select.value

    data = df_r[df_r['Activity'] == activity]

    source.data = ColumnDataSource(data=data).data





select = Select(title='Select Activity', value=activity_selected, options=activity_list)

select.on_change('value', update_plot)

layout=column(select, plot)

doc.add_root(layout)

show(modify_doc)

``

···

On Monday, 18 February 2019 13:17:17 UTC+1, tony halik wrote:

Just update the ColumnDataSource by streaming new or modified data like in this live-plotting example:
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, curdoc
from datetime import datetime
import random

source = ColumnDataSource(dict(time = [datetime.now()], value = [random.randint(5, 10)]))
plot = figure(x_axis_type = ‘datetime’)
plot.line(x = ‘time’, y = ‘value’, source = source)

def update():
source.stream(dict(time = [datetime.now()], value = [random.randint(5, 10)]))

curdoc().add_root(plot)
curdoc().add_periodic_callback(update, 1000)

``

On Monday, February 18, 2019 at 9:44:51 AM UTC+1, Andrea Ciufo wrote:

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.