on_change function on widget doesn't work as expected

my short script looks like the following:

output_server(‘ts_sample.html’)

count = 0

def update_title(attrname, old, new):

global count

count = count + 1

textInput = TextInput(title=“query_parameters”, name=‘fcp_chp_id’, value=‘fcp_chp_id’)

textInput.on_change(‘value’, update_title)

curdoc().add_root(textInput)

p = figure( width=800, height=650,title=“ts_sample”,x_axis_label=‘datetime’ )

p.line(np.array(data[‘date_trunc’].values, dtype=np.datetime64), data[‘latitude’], legend=“test”)

p.xaxis[0].formatter=bkmodels.formatters.DatetimeTickFormatter(formats=dict(hours=["%F %T"]))

show(curdoc())

It works, when bokeh server(bokeh serve) is running and I got the plotting, but on_change callback doesn’t work as expected.

Assumed the value of textInput should be the content/string in the input box, but I changed it multiple times but the callback function update_title is never called (the count global variable is always 0). So apparently the underlying textInput.value is not changed, how can I change value attr and trigger the on_change function ?

Hi Wu

First, please let me encourage you (and all users) to always submit as much information as possible: Bokeh version, platform, browser, etc. And most especially: a *complete, minimal, runnable sample* that reproduces the behaviour that we can run. It's often very hard to debug or offer advice without being able to run code. And it can often take 5-10 minutes or more to try and turn partial code into runnable code. That may not sound like alot but there are only a handful of bokeh devs and many thousands of bokeh users. We very much want to help support users but that kind of effort is simply not scalable.

···

-----

In this case there are definitely some conceptual issues we need to explain better leading up to the 0.11 release. Unless you are needing to work interactively (in the notebook, say) then we would encourage every to use the new "bokeh serve" command. Then you could delete the show, and output_server lines and just run "bokeh serve myapp.py" But note, in this case, the callback runs *in the server session*.

However if you need to run interactively, that's fine, but if you want callbacks you will have to use the new client session API. Unfortunately docs for it are not up yet (Bokeh 0.11 is still pre-release!) but I can point you to an example you can run and model your code after here:

  https://github.com/bokeh/bokeh/blob/master/examples/glyphs/population_server.py

Note in particular these lines:

  from bokeh.client import push_session

  document = Document()
  session = push_session(document)

  # stuff

  def create_layout():
        year_select = Select(title="Year:", value="2010", options=years)
        location_select = Select(title="Location:", value="World", options=locations)

        year_select.on_change('value', on_year_change)
        location_select.on_change('value', on_location_change)

       controls = HBox(children=[year_select, location_select])
        layout = VBox(children=[controls, pyramid(), population()])

      return layout

  layout = create_layout()
  update_data()

  document.add_root(layout)
  session.show(layout)

Which shows the relevant parts for you.

Bryan

On Dec 7, 2015, at 3:26 PM, [email protected] wrote:

my short script looks like the following:

output_server('ts_sample.html')

count = 0
def update_title(attrname, old, new):
    global count
    count = count + 1

textInput = TextInput(title="query_parameters", name='fcp_chp_id', value='fcp_chp_id')
textInput.on_change('value', update_title)

curdoc().add_root(textInput)
p = figure( width=800, height=650,title="ts_sample",x_axis_label='datetime' )
p.line(np.array(data['date_trunc'].values, dtype=np.datetime64), data['latitude'], legend="test")
p.xaxis[0].formatter=bkmodels.formatters.DatetimeTickFormatter(formats=dict(hours=["%F %T"]))
show(curdoc())

It works, when bokeh server(bokeh serve) is running and I got the plotting, but on_change callback doesn't work as expected.

Assumed the value of textInput should be the content/string in the input box, but I changed it multiple times but the callback function update_title is never called (the count global variable is always 0). So apparently the underlying textInput.value is not changed, how can I change value attr and trigger the on_change function ?

--
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/0dd53722-6ab6-4d64-be68-61b75e1c5282%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

thanks a lot for the reply and redirection :slight_smile:

···

On Monday, December 7, 2015 at 10:26:32 PM UTC+1, [email protected] wrote:

my short script looks like the following:

output_server(‘ts_sample.html’)

count = 0

def update_title(attrname, old, new):

global count

count = count + 1

textInput = TextInput(title=“query_parameters”, name=‘fcp_chp_id’, value=‘fcp_chp_id’)

textInput.on_change(‘value’, update_title)

curdoc().add_root(textInput)

p = figure( width=800, height=650,title=“ts_sample”,x_axis_label=‘datetime’ )

p.line(np.array(data[‘date_trunc’].values, dtype=np.datetime64), data[‘latitude’], legend=“test”)

p.xaxis[0].formatter=bkmodels.formatters.DatetimeTickFormatter(formats=dict(hours=["%F %T"]))

show(curdoc())

It works, when bokeh server(bokeh serve) is running and I got the plotting, but on_change callback doesn’t work as expected.

Assumed the value of textInput should be the content/string in the input box, but I changed it multiple times but the callback function update_title is never called (the count global variable is always 0). So apparently the underlying textInput.value is not changed, how can I change value attr and trigger the on_change function ?