Code
library(reticulate)
use_python(here::here("website_venv/bin/python3"))
# use_virtualenv(here::here("website_venv"))
With Quarto & Python
December 5, 2024
I wanted to make a quick post about this topic because I could not find much online on this topic and it was something I realized I had not tried in Python. Below is the code for a parameterized report where I am filtering for data from the palmerpenguins
package to only look at data from 2007. I decided on the palmerpenguins
dataset because it is accessible and can showcase a simple for loop that renders a yearly report on penguin measurements.
The for loop script can be found here and the actual files created are showcased here. This example will only show the template that was used for the Quarto document. Also, as I was creating this I came across a wonderful video explaining a more in-depth example of looping through a couple of parameters for Quarto documents in Python (video here, Quarto document code here, for loop code here).
species island bill_length_mm ... body_mass_g sex year
0 Adelie Torgersen 39.1 ... 3750.0 male 2007
1 Adelie Torgersen 39.5 ... 3800.0 female 2007
2 Adelie Torgersen 40.3 ... 3250.0 female 2007
3 Adelie Torgersen NaN ... NaN NaN 2007
4 Adelie Torgersen 36.7 ... 3450.0 female 2007
[5 rows x 8 columns]
year
2009 120
2008 114
2007 110
Name: count, dtype: int64
species island sex ... bill_depth_mm flipper_length_mm body_mass_g
0 Adelie Biscoe female ... 18.580000 181.800000 3470.000000
1 Adelie Biscoe male ... 18.300000 181.600000 3770.000000
2 Adelie Dream female ... 17.844444 185.000000 3269.444444
3 Adelie Dream male ... 19.430000 188.600000 4102.500000
4 Adelie Torgersen female ... 18.162500 187.625000 3475.000000
5 Adelie Torgersen male ... 20.485714 192.285714 4139.285714
6 Chinstrap Dream female ... 17.838462 188.692308 3569.230769
7 Chinstrap Dream male ... 19.130769 196.153846 3819.230769
8 Gentoo Biscoe female ... 13.993750 211.062500 4618.750000
9 Gentoo Biscoe male ... 15.364706 218.882353 5552.941176
[10 rows x 7 columns]
species island sex ... bill_depth_mm flipper_length_mm body_mass_g
0 Adelie Biscoe female ... 18.58 181.80 3470.00
1 Adelie Biscoe male ... 18.30 181.60 3770.00
2 Adelie Dream female ... 17.84 185.00 3269.44
3 Adelie Dream male ... 19.43 188.60 4102.50
4 Adelie Torgersen female ... 18.16 187.62 3475.00
5 Adelie Torgersen male ... 20.49 192.29 4139.29
6 Chinstrap Dream female ... 17.84 188.69 3569.23
7 Chinstrap Dream male ... 19.13 196.15 3819.23
8 Gentoo Biscoe female ... 13.99 211.06 4618.75
9 Gentoo Biscoe male ... 15.36 218.88 5552.94
[10 rows x 7 columns]
species island sex variable value
0 Adelie Biscoe female bill_length_mm 37.48
1 Adelie Biscoe male bill_length_mm 39.16
2 Adelie Dream female bill_length_mm 37.86
3 Adelie Dream male bill_length_mm 40.38
4 Adelie Torgersen female bill_length_mm 38.28
5 Adelie Torgersen male bill_length_mm 39.90
6 Chinstrap Dream female bill_length_mm 46.57
7 Chinstrap Dream male bill_length_mm 50.88
8 Gentoo Biscoe female bill_length_mm 45.06
9 Gentoo Biscoe male bill_length_mm 49.00
10 Adelie Biscoe female bill_depth_mm 18.58
11 Adelie Biscoe male bill_depth_mm 18.30
12 Adelie Dream female bill_depth_mm 17.84
13 Adelie Dream male bill_depth_mm 19.43
14 Adelie Torgersen female bill_depth_mm 18.16
15 Adelie Torgersen male bill_depth_mm 20.49
16 Chinstrap Dream female bill_depth_mm 17.84
17 Chinstrap Dream male bill_depth_mm 19.13
18 Gentoo Biscoe female bill_depth_mm 13.99
19 Gentoo Biscoe male bill_depth_mm 15.36
20 Adelie Biscoe female flipper_length_mm 181.80
21 Adelie Biscoe male flipper_length_mm 181.60
22 Adelie Dream female flipper_length_mm 185.00
23 Adelie Dream male flipper_length_mm 188.60
24 Adelie Torgersen female flipper_length_mm 187.62
25 Adelie Torgersen male flipper_length_mm 192.29
26 Chinstrap Dream female flipper_length_mm 188.69
27 Chinstrap Dream male flipper_length_mm 196.15
28 Gentoo Biscoe female flipper_length_mm 211.06
29 Gentoo Biscoe male flipper_length_mm 218.88
30 Adelie Biscoe female body_mass_g 3470.00
31 Adelie Biscoe male body_mass_g 3770.00
32 Adelie Dream female body_mass_g 3269.44
33 Adelie Dream male body_mass_g 4102.50
34 Adelie Torgersen female body_mass_g 3475.00
35 Adelie Torgersen male body_mass_g 4139.29
36 Chinstrap Dream female body_mass_g 3569.23
37 Chinstrap Dream male body_mass_g 3819.23
38 Gentoo Biscoe female body_mass_g 4618.75
39 Gentoo Biscoe male body_mass_g 5552.94
<IPython.core.display.Markdown object>
species | island | sex | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g |
---|---|---|---|---|---|---|
Adelie | Biscoe | female | 37.48 | 18.58 | 181.8 | 3470.0 |
Adelie | Biscoe | male | 39.16 | 18.3 | 181.6 | 3770.0 |
Adelie | Dream | female | 37.86 | 17.84 | 185.0 | 3269.44 |
Adelie | Dream | male | 40.38 | 19.43 | 188.6 | 4102.5 |
Adelie | Torgersen | female | 38.28 | 18.16 | 187.62 | 3475.0 |
Adelie | Torgersen | male | 39.9 | 20.49 | 192.29 | 4139.29 |
Chinstrap | Dream | female | 46.57 | 17.84 | 188.69 | 3569.23 |
Chinstrap | Dream | male | 50.88 | 19.13 | 196.15 | 3819.23 |
Gentoo | Biscoe | female | 45.06 | 13.99 | 211.06 | 4618.75 |
Gentoo | Biscoe | male | 49.0 | 15.36 | 218.88 | 5552.94 |