Skip to content

Instantly share code, notes, and snippets.

@epifanio
Created June 26, 2020 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epifanio/fb6123ae73dadf10e8fa2ca6a881edd0 to your computer and use it in GitHub Desktop.
Save epifanio/fb6123ae73dadf10e8fa2ca6a881edd0 to your computer and use it in GitHub Desktop.
import pandas as pd
from bokeh.io import show
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, CustomJS
from bokeh.models import Select
from bokeh.models.tools import HoverTool
from bokeh.plotting import figure, curdoc
df = pd.DataFrame({'2007': [10, 20, 30, 40],
'2008': [90, 60, 70, 40],
'2009': [30, 60, 70, 10],
'2010': [80, 50, 30, 10]},
index=[0, 0.5, 1, 1.5])
ds = ColumnDataSource(df)
p = figure(toolbar_location="above", x_axis_type="linear")
p.add_tools(HoverTool(tooltips=[("y", "@index")]))
line_renderer = p.line('2007', 'index', source=ds)
def handler(value, old, new):
print('value:', value, 'old:', old, 'new:', new)
print(ds.data[new])
line_renderer.glyph.x = ds.data[new]
#handler = CustomJS(args=dict(line_renderer=line_renderer), code="""
# line_renderer.glyph.x = {field: cb_obj.value};
#""")
select = Select(title="df-column:", options=list(df.columns))
#select.js_on_change('value', handler)
select.on_change('value', handler)
curdoc().add_root(column(select, p))
@epifanio
Copy link
Author

epifanio commented Jun 27, 2020

Thanks to @eugene on SO I updated the code to a working version:

select and update pandas dataframe columns in bokeh plot

  • Python Callback
import pandas as pd

from bokeh.io import show
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, CustomJS
from bokeh.models import Select
from bokeh.models.tools import HoverTool
from bokeh.plotting import figure, curdoc

df = pd.DataFrame({'2007': [10, 20, 30, 40],
                   '2008': [90, 60, 70, 40],
                   '2009': [30, 60, 70, 10],
                   '2010': [80, 50, 30, 10]},
                  index=[0, 0.5, 1, 1.5])
                  
ds = ColumnDataSource(df)
p = figure(toolbar_location="above", x_axis_type="linear")
p.add_tools(HoverTool(tooltips=[("y", "@index")]))

line_renderer = p.line('2007', 'index', source=ds)

def handler(value, old, new):
    line_renderer.glyph.x = dict(field=new)

    
select = Select(title="df-column:", options=list(df.columns))
select.on_change('value', handler)

curdoc().add_root(column(select, p))
  • JS Callback
import pandas as pd

from bokeh.io import show
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, CustomJS
from bokeh.models import Select
from bokeh.models.tools import HoverTool
from bokeh.plotting import figure, curdoc

df = pd.DataFrame({'2007': [10, 20, 30, 40],
                   '2008': [90, 60, 70, 40],
                   '2009': [30, 60, 70, 10],
                   '2010': [80, 50, 30, 10]},
                  index=[0, 0.5, 1, 1.5])
ds = ColumnDataSource(df)
p = figure(toolbar_location="above", x_axis_type="linear")
p.add_tools(HoverTool(tooltips=[("y", "@index")]))

line_renderer = p.line('2007', 'index', source=ds)

handler = CustomJS(args=dict(line_renderer=line_renderer), code="""
   line_renderer.glyph.x = {field: cb_obj.value};
""")

select = Select(title="df-column:", options=list(df.columns))
select.js_on_change('value', handler)

curdoc().add_root(column(select, p))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment