I’m trying to create a jupyter Bokeh demo in which a graph is updated in real time (e.g. via a loop) but at the same time, the updating depends on interactivity from a slider. So a simple example would be:
a random walk in 2D shown on a scatter plot, which updates every millisecond, in which the size of the random jumps is controlled by a slider.
With a Bokeh server app you can mix and use standard Bokeh server periodic callbacks and event callbacks. push_notebook is an older API that pre-dates the Bokeh server by several years, and has various significant limitations in comparison. It is useful for exactly the sort of examples already in the repo, and not much beyond that.
In the linked example, which is very helpful, I see that the variable source can be mutated in a callback. However a new variable I define (eg. j = 0 on the line below the definition of source) cannot be without raising an error. Is there a good way to keep a state variable which isn’t a ColumnDataSource?
Since the entire app including callback functions is itself defined inside a function, you’d need to use the somewhat uncommon nonlocal keyword in this instance.
There are potentially other ways to embed Bokeh apps in notebooks using different applications handler types that don’t involve nested functions. But I’m not sure they are especially better in any particular ways.
Actually, looking back at the relevant codepaths, I think you could define a class with a __call__ method that does the work of modify_doc and then pass an instance of that callable object to show and it would work. In that case, you could use instance vars e.g. self.j freely, which seems a little more commonplace than relying on nonlocal