Can one clear the displayed text in a TextInput box?

I’d like to be able to clear the text that is shown in the TextInput widget. My use case involves sliders: I have a slider plus 2 TextInput boxes to optionally set upper and lower limits. If the user uses these boxes, the slider limits are set to them; if the user then uses the slider itself to change the limits again, I’d like to be able to clear out the text that’s displayed in the TextInput boxes.

Can that be done?

Thanks!

Richard

I bet you could do it by using a callback on the slider and setting the TextInput value to an empty string ("").

If you run into problems, just post your code here and we can get more specific!

Yes, but :slight_smile:

When I set the TextInput value it calls the TextInput callback. How do I stop it from doing that?

Your suggestion does indeed blank out the box…

You can’t, but you can check if the new value is the empty string, and then do nothing in the callback in that case:

if new == "": return

That had occurred to me as a hack, but had been hoping to be able to avoid an unnecessary callback call. Oh well.

in any case, that fix did the trick. Thanks!! And thanks for the rapid responses!

Richard

You could also do

slider.remove_on_change(...)
_set value_
slider.on_change(...)

I use this in some of my callback functions that need to manipulate values of itself and/or others.
However it isn’t such a nice solution either :confused:

I feel like I should clarify that when I suggested

if new == "": return

it was not intended as a workaround or anything I would consider hacky. It’s the correct and appropriate way to handle this use case.

I hope a little context might shed light on why things are best as they are. As it is, the operation of callbacks is completely uniform and can be explained in once succinct statement:

Callbacks fire any time a Bokeh model property changes.

I hope everyone agrees that going from "foo" to "" constitutes a change. :slight_smile: Additionally, all users, including the ones who do want to do something when the value is the empty string, can satisfy their needs. Contrast if we start adding special cases:

Callbacks fire any time a Bokeh model property change. Except for these special properties on these modes, that all each behave in these special different ways: .


Now the only way to know for sure what the behavior of a model actual is, is to dig through reference documentation. But worse, there is now an arbitrary limitation on capability that means that any users who do want to respond to the empty string no longer have any recourse to do so.