What’s the Best Way to Use Widgets for Real-Time Data in Jupyter?

Hello

I have been building a Jupyter dashboard where data updates often sometimes every few seconds. I want to show these changes live using widgets like sliders / dropdowns; without having to manually re-run the notebook each time. :innocent:

I am looking for ideas on how to load live data into widgets while keeping everything smooth & responsive.:neutral_face:

I know about basic ipywidgets features like the @interact decorator and some layout controls, but I am not sure how to refresh widget outputs automatically. :thinking:

Are there good methods maybe using async updates, callbacks / custom widget code that let you push new data into widgets without manual steps?:thinking: Checked Tutorial: Understanding Jupyter Notebook Widgets | Saturn Cloud Blog guide related to this and found it quite informative.

Also; while researching this, I came across what is Azure Databricks, which has its own widget system and integrates ipywidgets for interactive notebooks. That made me wonder if some of those ideas could work in a regular Jupyter environment too.:innocent: I would love to hear from anyone who has built live data dashboards in Jupyter & how they made them run smoothly.

Thank you !!:slightly_smiling_face:

It kind of depends on how much live data we are talking about.

If the underlying data model already has an evented API, just setting a widget will be enough to do whatever should happen in the graph of widgets.

If the API isn’t evented, and it’s okay to send all the data every time on a schedule:

import asyncio
import ipywidgets as W
import time

interval = W.FloatSlider(1, min=0, max=10, description="interval")
current = W.FloatText()
display(W.HBox([interval, current]))

def tick():
    current.value = time.time()

async def tick_loop():
    while True:
        await asyncio.sleep(interval.value)
        try:
            tick()
        except:
            pass

task = asyncio.get_event_loop().create_task(tick_loop())

In the above, this would still be pretty interactive, as changing the definition of tick would run the new code every interval.value.

The custom event pattern (which powers Button.click) is an example of opting out of the “send the whole thing” pattern. Usually making full use of this would require some custom python and javascript to send only the right changed data, and update any in-browser values correctly.

2 Likes