Altair
Video Walkthrough

What will you get with this integration?

Altair is a declarative statistical visualization library for Python, based on Vega and Vega-Lite, and the source is available on GitHub. This integration lets you log interactive charts generated in Altair to Neptune.

Introduction

This guide will show you how to:
    Initialize Neptune and create a run,
    Log altair charts to Neptune.

Step 0: Before you start

Before you start, make sure that:

Install neptune-client and altair

Depending on your operating system open a terminal or CMD and run this command. All required libraries are available via pip and conda:
pip
conda
1
pip install neptune-client altair
Copied!
1
conda install -c conda-forge neptune-client altair
Copied!
For more help see installing neptune-client.
This integration is tested with neptune-client==0.9.19 and altair==4.1.0.

Step 1: Import and initialize neptune

1
import neptune.new as neptune
2
3
run = neptune.init(api_token='<your_api_token>', project='<your_project_name>')
Copied!
You can use the api_token='ANONYMOUS' and project='common/altair-support' to explore without having to create a Neptune account
Executing this cell will give you a link looking like: https://app.neptune.ai/common/altair-support/e/AL-1 with common/altair-support replaced by your project, and AL-1 replaced by your run.
Click on the link to open the run in Neptune. For now, it is empty but keep the tab with the run open to see what happens next.

Step 2: Log Altair figures to Neptune

Create a sample figure

Install vega_datasets to access sample data

pip
conda
1
pip install vega_datasets==0.9.0
Copied!
1
conda install -c conda-forge vega_datasets=0.9.0
Copied!

Create a sample figure using data from vega_datasets.data.cars()

1
import altair as alt
2
from vega_datasets import data
3
4
source = data.cars()
5
6
brush = alt.selection(type="interval")
7
8
points = (
9
alt.Chart(source)
10
.mark_point()
11
.encode(
12
x="Horsepower:Q",
13
y="Miles_per_Gallon:Q",
14
color=alt.condition(brush, "Origin:N", alt.value("lightgray")),
15
)
16
.add_selection(brush)
17
)
18
19
bars = (
20
alt.Chart(source)
21
.mark_bar()
22
.encode(y="Origin:N", color="Origin:N", x="count(Origin):Q")
23
.transform_filter(brush)
24
)
25
26
chart = points & bars
Copied!

Log interactive figure

1
run["interactive_img"].upload(neptune.types.File.as_html(chart))
Copied!

Step 3: Stop logging

Once you are done logging, you should stop tracking the run using the stop() method. This is needed only while logging from a notebook environment. While logging through a script, Neptune automatically stops tracking once the script has completed execution.
1
run.stop()
Copied!

Step 4: Explore the results in the Neptune

The images can be found in the All Metadata section:

Conclusion

You have learned how to:
    Initialize Neptune and create a run,
    Log altair charts to Neptune.

What's next

Now that you know how to log charts to Neptune, you can learn:
Last modified 1mo ago