Reading and Writing Data

Your code names a dataset, and ChartBook knows where it is. This page is the whole of that: how you write your own outputs, how you read any pipeline’s data, and the one case that has files and no path.

Every example runs in a pipeline made by chartbook init, once its chartbook.toml declares the dataframe with a path:

[dataframes.repo_public]
path = "./_data/repo_public.parquet"
date_col = "date"
docs = "Overnight repo rates and volumes."

Write your own outputs

# src/pull_repo.py, run from anywhere
from chartbook import data

df = fetch_from_fred()                        # your code
df.write_parquet(data.path("repo_public"))    # <pipeline>/_data/repo_public.parquet

data.path finds chartbook.toml by walking up from the file that called it, so a script three folders deep in src/ gets the same answer wherever it is run from; a notebook, which has no file, is resolved from its working directory, usually its own folder. It reads the entry’s path, joins it to the pipeline’s folder, and returns the absolute path. The file need not exist yet. The folder it goes in is made if it is not there, so the first run of a new pipeline does not fail for want of _data/.

The declaration must exist. Asking for a name that [dataframes] does not declare stops at once and names the chartbook.toml it searched and the table to add:

No dataframe named 'merged_rates' is declared in /work/rates/chartbook.toml.

To register it, add a table to that file, for example:

    [dataframes.merged_rates]
    path = "./_data/merged_rates.parquet"

ChartBook never invents a path. A dataset with no declaration would have no description, no page and no snapshot.

A dataset of many files is declared as a folder. Polars writes it with partition_by, and data.path hands over the folder:

[dataframes.articles]
path = "./_data/articles"
docs = "One parquet file a year, Hive style: year=2024/part-0.parquet."
df.write_parquet(data.path("articles"), partition_by="year")

A glob, ./_data/articles/**/*.parquet, reads the same, for a layout that needs one. It is not a place to write.

Charts, models and files assets are written the same way, with their kind in front: assets.path("chart/repo_rates"), models.path("rate_forecaster"). Write the format the declaration names, since that is how it is read back: a .parquet ending is parquet, .csv is CSV, and format in the entry says otherwise.

Read any pipeline’s data

One call reads a dataset of any shape, from your own pipeline or another’s:

from chartbook import data

mine  = data.load("repo_public")                        # my own, from my folder: a polars LazyFrame
rates = data.load("fed_yield_curve:treasury_rates")     # another pipeline's, as pinned
news  = data.load("news_headlines:articles")            # a Hive folder, its year= keys as a column
frame = data.load("repo_public", format="pandas")       # or "polars_eager"

Your own dataset is read from your folder, because it is built there and may not be snapshotted yet. Another pipeline’s is read from the store, at the snapshot your lockfile pins, so it does not change under you until you run chartbook pin --update (Linking Pipelines Together). Nothing is copied: the files are read where the store keeps them.

Where you built the other pipeline yourself, because no store is shared, what is read is your own build of the commit the lockfile names, once it is checked to hold the pinned rows. It may hold newer rows too. data.load and data.sql leave those out, so everyone with one lockfile loads one table; data.path hands over the file as it is; and data.as_of says where the pinned rows end:

last  = data.as_of("fed_yield_curve:treasury_rates")           # datetime.date(2026, 10, 30): the pin's last date
frame = pd.read_parquet(data.path("fed_yield_curve:treasury_rates"))
frame = frame[frame["date"].isna() | (frame["date"] <= last)]  # what data.load() gives: a row with no date is the pin's too

SQL reads the same datasets, named in braces. ChartBook puts each name in and DuckDB runs the rest:

joined = data.sql("""
    select a.date, a.sofr, b.dgs10
    from {repo_public} a
    join {fed_yield_curve:treasury_rates} b using (date)
""")

data.ref("repo_public") gives the text that stands for a dataset in SQL you run yourself, and data.connect(source) the raw connection to one of the pipeline’s [sources] (Reading From a Databricks Warehouse).

Another pipeline’s folder has files and no path

A store need not keep a dataset’s files in one folder. A bucket, and a store that ChartBook 0.5 laid out, keep each file once, usually in the first snapshot that had it; and a snapshot kept as an increment keeps an unchanged file in the complete snapshot it builds on. There a dataset that grew by a year between two snapshots has its files in two snapshot folders. There is no one folder to hand out, so data.path of another pipeline’s folder or glob dataset raises, whatever the folders happen to hold today, and says what to call instead:

'news_headlines:articles' is a dataset of 14 files, kept in 3 places in the store, so there is no
one folder. Read it with data.load('news_headlines:articles'), or hand
data.files('news_headlines:articles') to your own reader.

data.files lists every file, sorted by its path inside the dataset, wherever each is kept. A build tool takes the list, and so do polars and DuckDB for the rows. Not for the Hive keys: where a file is kept need not say them (a store that keeps each file once has two partitions of the same bytes at one partition’s path, an increment keeps a file that was renamed with the same bytes under the name it had, and an S3 store names its copies by their hashes), so read a partitioned dataset with data.load or data.sql, which take each file’s keys from its path inside the dataset.

# dodo.py
from chartbook import data

def task_merge():
    return {
        "file_dep": data.files("news_headlines:articles"),   # every file, wherever kept
        "targets": [data.path("merged")],                    # my own output
        "actions": ["python src/merge.py"],
    }

A model that is a folder has files too. A loader that takes a directory gets a temporary one:

from chartbook import assets

with assets.gathered("forecasting:model/rate_forecaster") as folder:
    model = AutoModel.from_pretrained(folder)      # the folder goes when the block ends

A dataframe of one file, which most are, has a path as before: data.path("fed_yield_curve:treasury_rates") is the parquet file in the store. Read it; never write to it.

Throwaway files

For intermediates that are not assets, a partial download or a file between two steps, data.scratch() is a folder inside the pipeline that git ignores and nothing documents:

raw = data.scratch() / "raw_download.csv"      # <pipeline>/_data/_scratch/raw_download.csv

scratch = "./_tmp" under [project] puts it elsewhere in the pipeline. Delete it whenever you like.

From the command line

chartbook path is data.path for Make, a shell, R or Stata, and --files is data.files:

$ chartbook path repo_public
/work/rates/_data/repo_public.parquet
$ chartbook path news_headlines:articles --files
/Users/x/Google Drive/chartbook-store/snapshots/news_headlines/2026-06-01T120000Z_e5f6a7b8c9d0/_data/articles/year=2023/part-0.parquet
/Users/x/Google Drive/chartbook-store/snapshots/news_headlines/2026-06-01T120000Z_e5f6a7b8c9d0/_data/articles/year=2024/part-0.parquet
MERGED := $(shell chartbook path merged_rates)

Where next