Search code examples
pythonbokehtextinput

make a bokeh TextInput callback responsive as text is typed


Current functionality of the on_change method of a bokeh TextInput object is to run the callback after all text is typed and then some other key is pressed. I want to know if a TextInput object can be configured to run a callback as text is being typed / deleted. The following code is a minimal example.

from bokeh.io import curdoc
from bokeh.layouts import widgetbox, column
from bokeh.models import TextInput, Button

button = Button(button_type='success')
ti = TextInput(title='enter text to enable button')
layout = column(
    widgetbox(ti),
    widgetbox(button))

button.disabled = True

def callback(attr, old, new):
    if ti.value != '':
        button.disabled = False
    else:
        button.disabled = True

ti.on_change('value', callback)

curdoc().add_root(layout)

Here is a screenshot of the corresponding application.

...

The desired behavior is for the button to become active as soon as text is typed. The screenshot demonstrates this is not happening. Not shown is that the button will become active after 'enter' is pressed.


Solution

  • As of Bokeh 1.3 you can watch the value_input property

    ti.on_change('value_input', callback)