2

I am using Bokeh and Python 2.7

Im trying to update the Data Source to change the plot based on Select Box. But I am not able to update the plot. what am I doing wrong? or is there a better way?

Code:

from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, output_file, show, output_notebook
from bokeh.models.widgets import Select
from bokeh.io import curdoc
from bokeh.layouts import column, row
from bokeh.io import output_file, show
from bokeh import models
import pandas as pd

d1 = dict(x=  [10,4,6,4], y =  [6,2,8,10])

d2 = dict(x=  [23,12,50,30], y =  [5,10,23,18,12])

source = ColumnDataSource(data=d1)

p = figure()

select = Select(title="Select d",  options=['d1', 'd2'])

def update_plot(attrname, old, new):
    if new == 'd1':
        newSource = d1

    if new == 'd2':
        newSource = d2


    source.data =  newSource

p.line(x='x', y='y',source = source)

select.on_change('value', update_plot)
layout = column(row(select, width=400), p)
curdoc().add_root(layout)
show(layout)
12
  • are you sure update_plot is triggered? Did you try calling update_plot directly without the on_change? Commented Dec 18, 2017 at 15:21
  • How can i verify if update_plot is triggered? I read in bokeh documentation that is necessary 'on_change' function Commented Dec 18, 2017 at 15:27
  • can you add a print('hello') statement within update_plot? Commented Dec 18, 2017 at 15:30
  • I added it, but the script doesn't print 'hello' Commented Dec 18, 2017 at 15:34
  • indeed. I am testing myself right now. Can you try open("/var/tmp/log.txt", "a").write("test\n") (if you are on Windows, use e.g. C:/tmp/log.txt?) Commented Dec 18, 2017 at 15:40

1 Answer 1

2

You need to start bokeh with the bokeh server, like this:

bokeh serve myscript.py

And then open localhost:5006 in your browser.

If you start bokeh without the server then it just creates a static html file and there is no way you can either make the page call your functions (that's why you don't see the prints) or alter the page with your python code after the initial load. From the docs:

The architecture of Bokeh is such that high-level “model objects” (representing things like plots, ranges, axes, glyphs, etc.) are created in Python, and then converted to a JSON format that is consumed by the client library, BokehJS. [...] However, if it were possible to keep the “model objects” in python and in the browser in sync with one another, then more [you could also] respond to UI and tool events generated in a browser with computations or queries using the full power of python

Sign up to request clarification or add additional context in comments.

1 Comment

Addtionally (Just an FYI) show is not usefu inside Bokeh apps and is ignored (but you can pass --show to bokeh serve to open a browser automatically).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.