You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/blog/posts/2025-07-23-parameterized-reports-python/index.qmd
+40-35Lines changed: 40 additions & 35 deletions
Original file line number
Diff line number
Diff line change
@@ -144,39 +144,7 @@ In Jupyter, you can add this tag through the cell toolbar:
144
144
145
145
{fig-alt="Screenshot of a Jupyter notebook cell with a tag 'parameters' added to it."}
146
146
147
-
::: {.callout-tip}
148
-
149
-
## `jupyter` vs `knitr`
150
-
151
-
Setting parameters differs based on the engine you are using in Quarto.
152
-
With a Jupyter notebook (`.ipynb`), or a plain text Quarto document (`.qmd`) with only Python code cells, Quarto will default to the `jupyter` engine.
153
-
As described above, the `jupyter` engine uses cell tags to identify parameters.
154
-
155
-
If you are working in a `.ipynb` file, your IDE will likely provide a way to add these tags through the cell toolbar.
156
-
If you are working in a `.qmd` file, you can add the tags as code cell options:
157
-
158
-
````markdown
159
-
```{{python}}
160
-
#| tags: [parameters]
161
-
city = "Corvallis"
162
-
```
163
-
````
164
-
165
-
If you are working in a Quarto document (`.qmd`) with R code cells, Quarto will default to the `knitr` engine.
166
-
With the `knitr` engine, you set parameters in the document header under `params`:
167
-
168
-
```yaml
169
-
---
170
-
params:
171
-
city: "Corvallis"
172
-
---
173
-
```
174
-
175
-
In `knitr`, parameters are accessed as elements of `params`, e.g. `params$city`, rather than as global variables like `city`.
176
-
177
-
:::
178
-
179
-
You can see the updated notebook (now a parameterized report), [`climate.ipynb`, on GitHub](https://github.com/cwickham/one-notebook-many-reports/blob/main/03-many-reports/climate.ipynb).
147
+
You can see the updated notebook, now a parameterized notebook, on GitHub: [`climate.ipynb`](hhttps://github.com/cwickham/one-notebook-many-reports/blob/main/02-one-parameterized-report/climate.ipynb).
180
148
181
149
### 3. Render with different parameter values
182
150
@@ -204,7 +172,7 @@ To generate all 50 reports, we need to run `quarto render` 50 times, each time w
204
172
You could automate this in many ways, but let's use a Python script.
205
173
For example, you might have a dataset of cities and their corresponding output filenames:
I've generated a small example above, but in reality you would likely read `cities` in from a file.
215
183
Then you could iterate over the rows of this dataset, rendering the notebook for each city:
216
184
217
-
```python
185
+
```{.python filename="gen-reports.py"}
218
186
from quarto import render
219
187
for row in cities.iter_rows(named=True):
220
188
render(
@@ -226,6 +194,8 @@ for row in cities.iter_rows(named=True):
226
194
227
195
Run this script once, and you'll get all 50 custom reports!
228
196
197
+
You can find the complete working example on GitHub: [cwickham/one-notebook-many-reports/03-many-reports](https://github.com/cwickham/one-notebook-many-reports/tree/main/03-many-reports).
198
+
229
199
## Pretty Reports: Brand and Typst
230
200
231
201
The steps above to produce parameterized reports apply to any output format supported by Quarto.
@@ -268,8 +238,43 @@ As an example, you could add a header with the city name and a map of the locati
268
238
269
239
You can see the source for this example at [cwickham/one-notebook-many-reports/05-pretty-reports](https://github.com/cwickham/one-notebook-many-reports/tree/main/05-pretty-reports).
270
240
241
+
## `jupyter` vs `knitr`
242
+
243
+
The steps for creating a parameterized report above are specific to documents that use the `jupyter` engine.
244
+
With a Jupyter notebook (`.ipynb`),
245
+
or a plain text Quarto document (`.qmd`) with only Python code cells,
246
+
Quarto will default to the `jupyter` engine.
247
+
As described above, the `jupyter` engine uses cell tags to identify parameters.
248
+
249
+
If you are working in a `.ipynb` file, your IDE will likely provide a way to add these tags through the cell toolbar.
250
+
If you are working in a `.qmd` file, you can add tags as a code cell option:
251
+
252
+
````markdown
253
+
```{{python}}
254
+
#| tags: [parameters]
255
+
city = "Corvallis"
256
+
```
257
+
````
258
+
259
+
With the `jupyter` engine, parameters can then be accessed directly as variables, e.g. `city`, in later code cells.
260
+
261
+
If you are working in a Quarto document (`.qmd`) with R code cells, Quarto will default to the `knitr` engine.
262
+
With the `knitr` engine, you set parameters in the document header under `params`:
263
+
264
+
```yaml
265
+
---
266
+
params:
267
+
city: "Corvallis"
268
+
---
269
+
```
270
+
271
+
In `knitr`, parameters are accessed as elements of `params`, e.g. `params$city`.
272
+
273
+
You can read more about setting and using parameters in [Guide > Computations > Parameters](/docs/computations/parameters.html).
274
+
271
275
## Wrapping Up
272
276
273
277
Parameterized reports turn one notebook into many customized outputs.
274
278
You've seen the process of going from a notebook with a hardcoded value to a parameterized report that can be rendered with different values.
275
279
You can then automate the rendering in any way you choose to generate dozens of reports at once.
0 commit comments