0

Is it possible to have both an internal update() function and a js callback attached to a slider in Bokeh.

For example, this code below is one of the Bokeh examples:

from bokeh.layouts import column
from bokeh.models import ColumnDataSource, CustomJS, Slider
from bokeh.plotting import Figure, output_file, show

output_file("js_on_change.html")

x = [x*0.005 for x in range(0, 200)]
y = x

source = ColumnDataSource(data=dict(x=x, y=y))

plot = Figure(plot_width=400, plot_height=400)
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)

callback = CustomJS(args=dict(source=source), code="""
    var data = source.data;
    var f = cb_obj.value
    var x = data['x']
    var y = data['y']
    for (var i = 0; i < x.length; i++) {
        y[i] = Math.pow(x[i], f)
    }
    source.change.emit();
""")

slider = Slider(start=0.1, end=4, value=1, step=.1, title="power")
slider.js_on_change('value', callback)

layout = column(slider, plot)

show(layout)

What I would like to do is have an update function:

def update():
    # Read a new bit of data.
    # Some pandas stuff.
    # source.data = new_data

So I can have:

# This will run a JS based callback.
slider.js_on_change('value', callback)

# This will run an internal function to fetch new piece of data.
slider.on_change('value', lambda attr, old, new: update())

So I can both update my underlying data and perform a JS event. Is this possible?

--- EDIT ---

Just adding a GIF file to demonstrate the problem. You can see the JS callback constantly fitting and updating the y_scale of the plot to make all candles fit the plot.

However, the moment the pull-down menu is switched to another value, the y_scale callback is not triggered. Only after we start to drag, the js callback kicks in and adapts the y_scale.

enter image description here

mbilyanov
  • 2,315
  • 4
  • 29
  • 49
  • I'm trying to have a slider, that once changed, will update the data and also change the `y_scale` based on the values within the data. I can achieve these tasks individually, but was not able to come up with a way to perform both the data update and the `y_scale` change simultaneously. – mbilyanov May 11 '20 at 22:16

1 Answers1

0

It seems to me that you're describing two completely separate problems in your question.

  1. You cannot run a callback both in JS and in Python when a certain property changes. Given your code, the answer is obvious - you're using show which creates static HTML documents. In order to run Python code, you need bokeh serve or a more complicated solution: https://docs.bokeh.org/en/latest/docs/user_guide/server.html#single-module-format

  2. You cannot both update the data and update the y_scale based on that data in some situations. I cannot really answer it in any useful way since you didn't provide any details about how you change y_scale - your code never mentions it. To make it possible to answer this question, please either update the code example in the question or just ask a new question

Eugene Pakhomov
  • 9,309
  • 3
  • 27
  • 53
  • Hi, yes. I'm using the app server, thus the GIF. I was not able to provide the full code and the problem is a bit too deep rooted to be easily isolated into a simple coded example. The GIF is there to demonstrate the fully running example. – mbilyanov May 12 '20 at 04:39
  • I understand. But if the problem is so complex that an minimal example cannot be created, I don't have a clue how it could be solved via StackOverflow. – Eugene Pakhomov May 12 '20 at 07:22
  • Ok. I will try to create a simpler case-study example. Besically, I have implemented this solution https://stackoverflow.com/questions/51162010/python-plot-candlesticks-with-automatic-y-zoom/51227346#51227346 for two plots. And it works. But I'm getting a weird jump on the second plot when I do the data update. But you are right, the problem is complex and will require intense debugging. – mbilyanov May 12 '20 at 11:21
  • I tried to explain the issue here as well: I'm sure others have also came across this problem. https://github.com/bokeh/bokeh/issues/10013 – mbilyanov May 12 '20 at 15:02
  • Would you be interested in checking out the github repo. I already gave Bryan access. Then I will answer my own question here, so that others can benefit. Thanks. – mbilyanov May 13 '20 at 12:06
  • Sure, I can take a look. – Eugene Pakhomov May 13 '20 at 12:42