Notebooks
New in 0.6.0
Notebooks: SQL, Markdown, plot and dash cells in one .sql file.
A notebook is a .sql file read as a column of cells. A SQL cell has its own editor and its own results; a Markdown cell explains the step in prose; a plot cell charts a SQL cell's result; a dash cell holds a whole dashboard. Use one when an analysis has steps worth keeping and explaining, instead of a pile of loose queries.
The file stays a plain SQL script. Markdown, plots and dashboards are SQL comments, so duckdb < notebook.sql runs it, and it diffs and reviews like any other text. Results are not saved in it: rerunning is one keystroke.
Open or make one
New: the
+at the end of the tab strip → New notebook. It starts as one empty SQL cell and has no file until you save it.Open: the title bar's Open menu → Open notebook…, drag a
.sqlfile onto the window, or name it on the command line:bashducklocal analysis.sqlAgain: the sidebar's Notebooks group lists the notebooks you opened recently, sorted by name; click one to open it. A notebook stays there after its tab closes, until newer documents push it out. Open notebook tabs come back at the next launch.
Any .sql file opens as a notebook. One with no cell markers is a single SQL cell, and saving it writes it back unchanged as long as it stays one cell.
⌘S saves. An untitled notebook asks where; closing a notebook with unsaved edits asks first.
Cells
| Cell | Holds | Shows |
|---|---|---|
| SQL | One or more SQL statements | An editor, and the last statement's result under it |
| Markdown | Markdown | The text, rendered |
| Plot | A .dash plot block's attributes | A chart of a SQL cell's result |
| Dash | Any .dash blocks | A dashboard, drawn in place |
Each cell has a gutter on its left with its run button and run number. The selected cell is marked by a bar down its left edge. Hover a cell for Move up, Move down and Delete cell at its top right.
To add a cell, hover the gap between two cells and click the + that appears, or use the + in the notebook's toolbar: SQL cell, Markdown cell, Plot cell or Dash cell.
Markdown, plot and dash cells show what they draw. Double-click one, or use its Edit button on hover, to change its text; click away and it draws again.
Run cells
| Do | How |
|---|---|
| Run a cell | ⌘↵ in it, or its run button |
| Run a cell and move to the next | ⇧↵ |
| Run every SQL cell | Run all in the toolbar: top to bottom, stopping at the first error |
| Stop | Stop in the toolbar: interrupts the running statement and drops the queued cells |
- Cells share the window's connection, so a table one cell creates is there for the next, and the open database, attached files and loaded extensions are there for all of them.
- Relative paths start from the notebook's folder:
'orders.csv'in a notebook reads the file beside it. - A SQL cell may hold several statements. They run in order, stop at the first failure, and the cell shows the last statement's result, in the same results panel a query tab has.
- A statement that returns no rows says so in one line instead of an empty table.
The file format
A notebook uses the "percent" format that Jupytext and VS Code read. A line starting -- %% begins a cell:
| Marker | Cell |
|---|---|
-- %% | SQL |
-- %% name=NAME | SQL, named so a plot can draw it |
-- %% [markdown] | Markdown, its lines as -- comments |
-- %% [plot] | Plot, its attributes as -- comments |
-- %% [dash] | Dash, its .dash blocks as -- comments |
-- %% [markdown]
-- # Orders review
--
-- Revenue by channel, from `orders.csv` beside this file.
-- %% name=channels
SELECT channel, round(sum(revenue), 2) AS revenue
FROM 'orders.csv'
GROUP BY channel
ORDER BY revenue DESC;
-- %% [plot]
-- type = "bar"
-- data = channels
-- x = "channel"
-- y = "revenue"
-- format = "usd"examples/orders_dashboard/orders.sql in the DuckLocal repository is a complete notebook with every kind of cell.
Plot cells
A plot cell holds the attributes of a dashboard plot block, one a line — type, x, y, series, title, format and the rest — and takes the same plot types and rules. It draws:
- the SQL cell named by
data = NAME, which carriesname=NAMEon its marker line; or, - without
data, the nearest SQL cell above it.
The chart redraws when its SQL cell runs again. Before that it says to run that cell. A mistake in the plot is listed by line.
A new plot cell added under a SQL cell that has run starts as a bar chart of that result.
Dash cells
A dash cell holds any .dash blocks — source, query, plot, filter — as comments, and draws them as a dashboard in the notebook: the grid, cards and click-to-filter included. Its relative paths start beside the notebook. A new dash cell under a SQL cell that has run starts as a dashboard of that SQL with a bar chart.
- Editing the cell re-runs only the queries whose SQL changed.
- Run all re-runs every dashboard after the SQL cells, since a dashboard may read tables they made.
- Running a dash cell that is showing runs its queries again.
- A dash cell has no comments, proposals or file watcher; those belong to
.dashfiles.
From the command line
ducklocal notebook cells analysis.sql # list the cells as JSON; runs nothing
ducklocal notebook run analysis.sql # run them; one JSON report
ducklocal notebook run analysis.sql --format md # the notebook as a Markdown reportnotebook run runs the SQL cells in order on one connection, from the notebook's folder, and checks each plot cell against its data cell's columns and each dash cell as ducklocal check would. It stops at the first failing cell unless --keep-going; --cell N (repeatable) runs only those cells. A failed cell still prints the report and exits 1. --format md writes the Markdown cells as they are, each SQL cell as a code block and its result as a table, ready to paste into a document.
--database, --read-write, --limit and --restricted work as for ducklocal query. ducklocal notebook --help has the details.
