Streaming data with Bokeh and Panel periodic callbacks?

Hey!
Any way to make the same streaming example to work inside a Jupyter(Lab) app too?

@ItamarShDev Can you be more specific? It should already work, what went wrong when you tried?

when i run your code from your comment inside a local JupyterLab
i get:

This event loop is already running

Oh you should not try to start a new event loop or start a server in Jupyter, just display the app as normal:

app = Application()
app.panel()

AttributeError: ‘_state’ object has no attribute ‘add_periodic_callback’

Note the version requirement I stated above:

2 Likes

Using the code still not working for me on 0a18.

tried moving to Stream and still nothing. no error either

import panel as pn
import param
from tornado.ioloop import IOLoop
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
import numpy as np
pn.extension()


class Application(param.Parameterized):
    do_calc = param.Action(lambda self: self._do_calc())

    def __init__(self, **params):
        super(Application, self).__init__(**params)
        self.source = ColumnDataSource({"x": [1,2,3], "y": [2,3,4]})
        self.figure = figure()
        self.figure.line(x="x", y="y", source=self.source)
        self.col = pn.Column(
            pn.pane.Markdown("## Title"),
            self.param.do_calc,
            self.figure,
        )
        pn.state.add_periodic_callback(self._do_calc, 200)  # <- this line does not work properly


    def _do_calc(self):
        data = list(np.random.randint(0, 2 ** 31, 10))
        self.source.stream({"y": data, "x": data}, 300)

    def panel(self):
        return self.col


app = Application()
app.panel()
1 Like

The example is not working for me either.

Neither the param button, nor the periodic callback work.

However, when using bokeh’s button, the chart does update.

import panel as pn
import param
from tornado.ioloop import IOLoop
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, Button
import pandas as pd
import numpy as np
pn.extension()


class Application(param.Parameterized):
    do_calc = param.Action(lambda self: self._do_calc())

    def __init__(self, **params):
        super(Application, self).__init__(**params)
        self.source = ColumnDataSource(
            pd.DataFrame({
                'x':np.random.randint(0, 2 ** 31, 10),
                 'y':np.random.randint(0, 2 ** 31, 10)
            }).cumsum().set_index('x')
        )
        self.figure = figure()
        self.figure.line(x="x", y="y", source=self.source)
        self.button = Button(label="Update Plot")
        self.button.on_click(self._do_calc)
        self.col = pn.Column(
            pn.pane.Markdown("## Title"),
#             self.param.do_calc, # <- this line does not work properly
            self.button, # bokeh's button works
            self.figure,
        )

#         pn.state.add_periodic_callback(self._do_calc, 200)  # <- this line does not work properly


    def _do_calc(self,event='test'):
        data = pd.DataFrame({'x':np.random.randint(0, 2 ** 31, 10),
                             'y':np.random.randint(0, 2 ** 31, 10)}).cumsum()
        self.source.data.update(data)

    def panel(self):
        return self.col


app = Application()
app.panel()