Skip to content

Commit a1ebc13

Browse files
committed
Language tweaks
1 parent 41c3263 commit a1ebc13

File tree

1 file changed

+19
-15
lines changed
  • docs/blog/posts/2025-07-23-parameterized-reports-python

1 file changed

+19
-15
lines changed

docs/blog/posts/2025-07-23-parameterized-reports-python/index.qmd

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ lightbox: true
1919

2020
## Based on a talk at SciPy 2025
2121

22-
This post is based on the talk "From One Notebook to Many Reports: Automating with Quarto" delivered at [SciPy 2025](https://www.scipy2025.scipy.org), July 2025 by Charlotte Wickham.
22+
This post is based on the talk "From One Notebook to Many Reports: Automating with Quarto" delivered at [SciPy 2025](https://www.scipy2025.scipy.org) by Charlotte Wickham.
2323
You can find the slides at [cwickham.github.io/one-notebook-many-reports](https://cwickham.github.io/one-notebook-many-reports/) and example code at [github.com/cwickham/one-notebook-many-reports](https://github.com/cwickham/one-notebook-many-reports).
2424

2525
:::
2626

2727
## The Problem: Repetitive Reporting
2828

29-
Would you rather read a generic "Climate summary" or a "Climate summary for _exactly where you live_"? Reports that are personalized to a specific situation increase understanding, engagement, and connection. But producing many customized reports manually is tedious and error-prone.
29+
Would you rather read a generic "Climate summary" or a "Climate summary for _exactly where you live_"? Reports that are personalized to a specific situation increase engagement and connection. But producing many customized reports manually is tedious and error-prone.
3030

31-
Quarto solves this with parameterized reports-you create a single document template, then render it multiple times with different parameter values to generate customized outputs automatically.
31+
Quarto solves this with parameterized reports---you create a single document template, then render it multiple times with different parameter values to generate customized outputs automatically.
3232

3333
A great example is the customized soil health reports from Washington Soil Health Initiative's [State of the Soils Assessment](https://washingtonsoilhealthinitiative.com/state-of-the-soils/), presented at posit::conf(2023) by [Jadey Ryan](https://jadeyryan.com) (watch on [YouTube](https://youtu.be/lbE5uOqfT70?si=C-d5U5Q2VXo1wlDs)). Jadey demonstrated this approach using R and plain text Quarto files (`.qmd`).
3434

35-
This post shows you how to apply the same principles using Python: I'll walk through converting a Jupyter notebook (`.ipynb`) into a parameterized report, then automating the generation of multiple customized outputs. Then I'll show you how to make those reports look professionally polished.
35+
This post shows you how to apply the same principles using Python: we'll walk through converting a Jupyter notebook (`.ipynb`) into a parameterized report, then automating the generation of multiple customized outputs. Then I'll give you some tips for making your reports look polished.
3636

3737
## The Solution: Parameterized Reports
3838

@@ -48,7 +48,7 @@ You can see the full notebook, [`corvallis.ipynb`, on GitHub](https://github.com
4848

4949
- The document options specify `echo: false` so no code appears in the final output, and `format: typst` so the output is a PDF produced via [Typst](https://typst.app), a modern alternative to LaTeX.
5050

51-
This single notebook can be rendered:
51+
This single notebook can be rendered with Quarto:
5252

5353
```{.bash filename="Terminal"}
5454
quarto render corvallis.ipynb
@@ -62,7 +62,7 @@ Now, imagine we want to create this report for the 50 largest cities in Oregon.
6262
Here's the steps we'll take:
6363

6464
1. Turn hardcoded values into variables
65-
2. Make those variables parameters by tagging the cell
65+
2. Declare those variables parameters
6666
3. Render the notebook with different parameter values
6767
4. Automate rendering with many parameter values
6868

@@ -135,10 +135,10 @@ We can also use an f-string here to include the `city` variable:
135135
Now, we should be able to test our changes by explicitly setting the `city` variable to something other than "Corvallis" and re-running the cells.
136136
Since our report is no longer specific to Corvallis, we can rename it `climate.ipynb`.
137137

138-
### 2. Make variables into parameters
138+
### 2. Declare those variables parameters
139139

140140
Now we have a variable that represents the parameter, we need to let Quarto know it's a parameter.
141-
Quarto's parameterized reports are implemented using [Papermill](https://papermill.readthedocs.io/en/latest/), and inherits Papermill's approach: tag the cell containing the parameter with `parameters`.
141+
Quarto's parameterized reports are implemented using [Papermill](https://papermill.readthedocs.io/en/latest/), and inherit Papermill's approach: tag the cell defining the parameter with `parameters`.
142142

143143
In Jupyter, you can add this tag through the cell toolbar:
144144

@@ -172,19 +172,21 @@ params:
172172
---
173173
```
174174

175+
In `knitr`, parameters are accessed as elements of `params`, e.g. `params$city`, rather than as global variables like `city`.
176+
175177
:::
176178

177179
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).
178180

179181
### 3. Render with different parameter values
180182

181-
If we render the notebook now, it will still produce the same report for Corvallis, because we haven't changed the parameter value:
183+
If we render `climate.ipynb`, it will still produce the same report for Corvallis, because we haven't changed the parameter value:
182184

183185
```{.bash filename="Terminal"}
184186
quarto render climate.ipynb
185187
```
186188

187-
But we can now pass parameter values to Quarto when we render with the `-P` flag:
189+
But we can now pass parameter values to Quarto with the `-P` flag:
188190

189191
```{.bash filename="Terminal"}
190192
# Generate report for Portland
@@ -199,7 +201,7 @@ We've also added `--output-file` to ensure each report gets its own filename.
199201
### 4. Automate rendering with many parameter values
200202

201203
To generate all 50 reports, we need to run `quarto render` 50 times, each time with a different city as the parameter value.
202-
You could automate this in many ways, but let use a Python script.
204+
You could automate this in many ways, but let's use a Python script.
203205
For example, you might have a dataset of cities and their corresponding output filenames:
204206

205207
```python
@@ -209,7 +211,7 @@ cities = pl.DataFrame({
209211
})
210212
```
211213

212-
I've generated a small example above, but in reality you would likely read `cities` in from a CSV file.
214+
I've generated a small example above, but in reality you would likely read `cities` in from a file.
213215
Then you could iterate over the rows of this dataset, rendering the notebook for each city:
214216

215217
```python
@@ -231,7 +233,7 @@ However, if you are targeting `typst` you can take advantage of additional featu
231233

232234
### Brand.yml
233235

234-
Quarto supports [brand files](https://posit-dev.github.io/brand-yml/) that automatically apply your organization's colors, fonts, and logos across your reports:
236+
Quarto supports [brand.yml](https://posit-dev.github.io/brand-yml/) a way to specify colors, fonts, and logos:
235237

236238
```{.yaml filename="_brand.yml"}
237239
color:
@@ -250,8 +252,10 @@ logo:
250252
medium: logo.png
251253
```
252254

253-
Place this file in your project, and Quarto automatically applies your branding to all generated reports.
254-
You can see a full example of using `_brand.yml` with `climate.ipynb` at [cwickham/one-notebook-many-reports/04-branded-reports](https://github.com/cwickham/scipy-talk/tree/main/04-branded-reports).
255+
Quarto will detect the `_brand.yml` file and apply the colors, fonts and logo to your report.
256+
Colors and fonts in your figures will need to be customized in your code, but that is made much easier with the [brand-yml](https://posit-dev.github.io/brand-yml/pkg/py/) Python package which imports your values from `_brand.yml`.
257+
258+
You can see a full example of using `_brand.yml` with `climate.ipynb` at [cwickham/one-notebook-many-reports/04-branded-reports](https://github.com/cwickham/scipy-talk/tree/main/04-branded-reports), and learn more about Quarto's support for brand in the [Brand guide](https://quarto.org/docs/authoring/brand.html).
255259

256260

257261
### Typst

0 commit comments

Comments
 (0)