Jupyter notebook integration
The Python SDK provides a notebook integration that allows you to visualize data in any Jupyter-like environment (e.g. Jupyter Notebook, JupyterLab, Google Colab, VS Code).
Install the integration
The notebook integration is an optional extra package that can be installed with the following command:
pip install "foxglove-sdk[notebook]"
Using the integration
Once the integration is installed, you can use the foxglove.init_notebook_buffer function to create a notebook buffer. Notebook buffers are used to collect messages.
After the messages are logged, you can call the show method on the notebook buffer to display the data in a Foxglove viewer.
When logging a message, if you don't set the log_time parameter, the SDK will use the current time as the log time.
For this reason, you should set the log_time parameter to the desired timestamp or you might experience unexpected playback behavior.
import foxglove
import time
# Create a notebook buffer to collect messages
nb_buffer = foxglove.init_notebook_buffer()
# Log 300 messages, spaced out by 33ms intervals (roughly 10 seconds)
for t in range(10*30):
# Artificially set the log time to 33ms intervals
timestamp = t * 0.033
# Log the message with the artificial timestamp
foxglove.log("/hello", {"count": t}, log_time=int(timestamp*1e9))
# Display the data in a Foxglove viewer.
# This needs to be the last expression in the cell for the widget to be displayed.
nb_buffer.show(layout_storage_key="layout-name")
Buffer management
The notebook buffer won't drain messages when you call show, but you can do it manually by calling clear.
# Clear buffer data
nb_buffer.clear()
# Display a new viewer with empty data.
# This needs to be the last expression in the cell for the widget to be displayed.
ng_buffer.show(layout_storage_key="layout-name")
Reloading viewer data
An alternative workflow is to show a single viewer and update the data its data by calling refresh on the widget.
For that, you'll need to keep a reference to the widget created by the show method.
For example, you can have a cell that creates a viewer:
import foxglove
# Create a notebook buffer to collect messages
nb_buffer = foxglove.init_notebook_buffer()
# Create a viewer and keep a reference to it
viewer = nb_buffer.show(layout_storage_key="layout-name")
# `viewer` needs to be the last expression in the cell for the widget to be displayed
viewer
Then, in another cell, you can update the data in the viewer by calling refresh on the widget:
import time
# Log 300 messages, spaced out by 33ms intervals (roughly 10 seconds)
for t in range(10*30):
# Artificially set the log time to 33ms intervals
timestamp = t * 0.033
# Log the message with the artificial timestamp
foxglove.log("/hello", {"count": t}, log_time=int(timestamp*1e9))
# Refresh the viewer to update the data
viewer.refresh()
You can even clear the buffered data and call refresh again (which will result in the viewer having no data):
# Clear the buffered data
nb_buffer.clear()
# Refresh the viewer to update the data
viewer.refresh()