☰Read-only archive⌕
Looking for help with Bokeh?

This is a read-only archive. Ask questions and join the conversation on GitHub.

Visit Community Support on GitHub →
Archived discussions · Captured October 28, 2026

TextInput activates callback upon pressing 'Enter'

Community Support
thisuser
thisuser

Just a quick question. Is it possible to activate a callback solely by placing the cursor inside a TextInput followed by pressing the ‘Enter’ on the keyboard, without changing the TextInput value? Thanks!

⌁   Link to post
thisuser
thisuser

Sorry, my fault. Below please see the minimal example. The timer is expected to start upon the ‘Enter’ key is pressed while the cursor is blinking in the text field of ‘duration in sec’, even if its value has not been changed.

from bokeh.server.server import Server
from bokeh.models import TextInput, Button
from bokeh.layouts import column, row

import time


def make_document(doc):
    def process():
        duration = int(duration_text.value)
        time.sleep(duration)
        status_text.value = 'Time is up'

    def commoncb():
        status_text.value = 'Timer starts'

    def bn_callback(event):
        commoncb()
        doc.add_next_tick_callback(process)

    def text_callback(attr, old, new):
        commoncb()
        doc.add_next_tick_callback(process)

    status_text = TextInput(title='', value='', disabled=True, sizing_mode='stretch_width')
    this_bn = Button(label="Start", button_type="success", sizing_mode='stretch_width')
    this_bn.on_click(bn_callback)

    duration_text = TextInput(title='duration in sec', value='2')
    duration_text.on_change('value', text_callback)

    doc.add_root(column([status_text, this_bn, duration_text]))


apps = {'/': make_document}
server = Server(apps)
server.start()
server.io_loop.add_callback(server.show, "/")
server.io_loop.start()
⌁   Link to post
thisuser
thisuser

@Bryan Since there was no reply, here is another simpler example if you don’t like the above one, thanks for your help on this matter! :smiley: (I want to activate a callback whenever the Enter is pressed inside the text field)

from bokeh.server.server import Server
from bokeh.models import TextInput, Button
from bokeh.layouts import column, row


def make_document(doc):
    def text_callback(attr, old, new):
        status_text.value = 'Text field Enter button pressed'

    def bn_callback(event):
        status_text.value = 'Reset'

    status_text = TextInput(title='', value='', disabled=True, sizing_mode='stretch_width')

    text_field = TextInput(title='Text field', value='Ready')
    text_field.on_change('value', text_callback)

    reset_bn = Button(label="Reset", button_type="success", sizing_mode='stretch_width')
    reset_bn.on_click(bn_callback)

    doc.add_root(column([status_text, text_field, reset_bn]))


apps = {'/': make_document}
server = Server(apps)
server.start()
server.io_loop.add_callback(server.show, "/")
server.io_loop.start()
⌁   Link to post
Bryan
Bryan

@thisuser Pease be patient, everyone here is donating time out of their personal free time offer help.

My initial assessment is unchanged. Bokeh on_change events only fire when values change, and no value has changed. The text property value starts as “Ready” and when <enter> is pressed the value is still “Ready”. No value change, therefore no callback.

If you want or need something different to support a specific use-case, you could propose new feature development in a GitHub Issue. The best way to bolster any feature request is to make it thorough, e.g. with proposed code, and considerations of compatibility and minimal footprint, as well as an argument of why it would be of benefit many Bokeh users.

⌁   Link to post
anx
anx

a bit late to the party @thisuser but if you are still working on this another option would be to use some javascript to force an update. something like:

input.dispatchEvent(new Event(‘change’, { bubbles: true }));

// that mimics the behavior of a change for Bokeh and should force an update you just need to attach an event listener to enter to your input (Detect the ENTER key in a text input field with JavaScript)

but you would need to tinker with a bit of JS to wire things up.

⌁   Link to post
anx
anx

Good point @Bryan . if it does block it you can easily add to the change event an actual change to force it to send the update and then just remove it on the server side such as appending to the input a space.

⌁   Link to post

End of archived discussion · Replies are disabled