Notebook
owid.grapher.notebook ¶
Tools for generating Jupyter notebooks from OWID chart configurations.
This module provides utilities to reverse-engineer OWID chart configurations back into Python code that recreates the charts using the owid-grapher-py API. This is useful for:
- Learning how to create specific chart types
- Converting existing OWID charts to notebooks for customization
- Generating example code from live charts
Example
from owid.site import get_chart_config, get_chart_data
from owid.grapher.notebook import translate_config
config = get_chart_config(url='https://ourworldindata.org/grapher/life-expectancy')
data = get_chart_data(url='https://ourworldindata.org/grapher/life-expectancy')
python_code = translate_config(config, data)
print(python_code)
UnsupportedChartType ¶
Bases:
Raised when attempting to translate an unsupported chart type.
Currently, only LineChart is fully supported. Other chart types (ScatterPlot, DiscreteBar, StackedDiscreteBar) will raise this exception.
translate_config ¶
Convert an OWID chart configuration into Python code.
Takes a chart configuration dictionary (as returned by get_chart_config) and the corresponding data, and generates Python code that recreates the chart using the owid-grapher-py Chart API.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
OWID chart configuration dictionary containing chart type, dimensions, selections, labels, and other settings.
TYPE:
|
data
|
pandas DataFrame containing the chart data in long format (typically with 'year'/'date', 'entity', and 'value' columns).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
String containing Python code that recreates the chart. The code uses method |
|
chaining with Chart().mark_*().encode().label() etc. |
| RAISES | DESCRIPTION |
|---|---|
|
If the chart type is not yet supported for translation. Currently supports: LineChart. |
Example
Source code in owid/grapher/notebook.py
translate_line_chart ¶
Translate a LineChart configuration to Python code.
Generates Python code for creating a line chart by analyzing the configuration and determining appropriate encoding, selection, labels, and interaction settings.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Chart configuration dictionary for a LineChart.
TYPE:
|
data
|
DataFrame containing the chart data.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Python code string for creating the line chart. |
Source code in owid/grapher/notebook.py
generate_notebook ¶
Generate a Jupyter notebook from a chart configuration.
Fetches the chart data, translates the config to Python code, and creates a complete Jupyter notebook with imports, data loading, and chart code.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
OWID chart configuration dictionary (must include 'slug').
TYPE:
|
path
|
Directory path where the notebook file will be saved. The notebook will be named {slug}.ipynb.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
|
If the chart type cannot be translated. |
Source code in owid/grapher/notebook.py
save_to_notebook ¶
Save chart code to a Jupyter notebook file.
Creates a new notebook with the chart title, imports, data loading, and the generated Python code.
| PARAMETER | DESCRIPTION |
|---|---|
slug
|
Chart slug for the filename and data loading.
TYPE:
|
title
|
Chart title for the notebook heading.
TYPE:
|
py
|
Python code string to include in the notebook.
TYPE:
|
path
|
Directory path where the notebook will be saved.
TYPE:
|
Source code in owid/grapher/notebook.py
main ¶
Take a large list of configs in JSONL format, and attempt to render as many as we can to notebooks.