Skip to main content

Python

Connect directly to Foxglove or play back local data recorded with the Python SDK.

Install the SDK

The SDK is distributed through PyPI. You can get started with a new project or use Foxglove from a notebook.

Install foxglove-sdk from PyPI: https://pypi.org/project/foxglove-sdk

pip install foxglove-sdk

The exact installation will depend on your package manager; here's an example using poetry.

poetry new example
poetry add foxglove-sdk

Live data

Log messages from Python

Create a new file in your project (for example, src/example/main.py)

import foxglove
import time

foxglove.start_server()

while True:
foxglove.log("/hello", {"time": time.time()})
time.sleep(0.03)

Run the script

Run the script to start logging. Following the poetry example above, you can run the following command from the "example" directory:

poetry run python src/example/main.py

Connect

In Foxglove, select "Open connection" from the dashboard or left-hand menu.

Select open connection

Select "Foxglove WebSocket" in the "Open a new connection" dialog, then click "Open" to accept the default connection string:

Foxglove WebSocket dialog

Local data

Recording data to a file

The SDK can also log data to disk. Let's augment the example above to also produce an MCAP file which can later be opened in Foxglove:

import foxglove
import time

foxglove.start_server()

# Keep a reference to the mcap. It will automatically close the file when the program exits, but
# we could also close it manually with `mcap.close()`.
mcap = foxglove.open_mcap("example.mcap")

while True:
foxglove.log("/hello", {"time": time.time()})
time.sleep(0.03)

Viewing data from a file

To load local files for visualization, you can:

  • Click "Open local file(s)…" in the dashboard or left-hand menu
  • Open or drag-and-drop the files from your OS file manager (desktop only)

Local file dialog

note

When opening multiple files, Foxglove will merge the data into a single playback timeline.

You can only merge multiple files of the same format.

Imported data

After importing data to Foxglove, select individual resources to visualize on the Recordings or Events pages:

Visualize on Recordings page

Select a custom time range of data (can span multiple recordings or events) to visualize on the Timeline page: