Skip to content

Instantly share code, notes, and snippets.

@Btibert3
Created September 30, 2024 17:50
Show Gist options
  • Save Btibert3/b34141fe1a7438654a58872dcd23451e to your computer and use it in GitHub Desktop.
Save Btibert3/b34141fe1a7438654a58872dcd23451e to your computer and use it in GitHub Desktop.
---
title: "A Basic HTML Report that accepts params"
format:
html:
embed-resources: true
---
```{python}
#| tags: [parameters]
#| echo: false
year = 2023
```
## Resources:
* Example here: https://posit.co/blog/parameterized-quarto/
## Use variables inline
Thank you for participating in our `{python} year` project!
To see how the templates is used:
```
Thank you for participating in our `{{python}} year` project!
```
## Rendering the report
We can render reports with parameters. Below is an example with two parameters that are defined and included within
the file report4.qmd.
```
quarto render report4.qmd -P year:2025 -P producer_id:BROCK1
```
The `-P` command is how we can pass in the argument values to a report that supports these parameters. Let's call that `template.qmd`.
:::{.callout-important}
You will need to install papermill with `pip install papermill` for reports with parameters. Be careful if you are using VS Code, you might need to install this in your base environment, as it appears that Quarto may not always respect the (conda) virutal environment that is activated where the render report was invoked.
:::
## The Report
```{python}
#| echo: false
import pandas as pd
import datetime
import numpy as np
def generate_fake_timeseries(year):
# Generate a date range for the year
dates = pd.date_range(start=f'{year}-01-01', end=f'{year}-12-31', freq='D')
# Generate random data
np.random.seed(42)
sales = np.random.randint(1000, 5000, size=len(dates)) # Random sales between 1000 and 5000
temperature = np.random.uniform(low=-10, high=35, size=len(dates)) # Random temperature between -10 and 35 degrees
traffic = np.random.randint(500, 10000, size=len(dates)) # Website traffic between 500 and 10000 visits
# Create the DataFrame
df = pd.DataFrame({
'date': dates,
'sales': sales,
'temperature': temperature,
'website_traffic': traffic
})
return df
df_fake_timeseries = generate_fake_timeseries(year)
```
Here is a fake timeseries dataset for the year `{python} year`.
```{python}
#| echo: false
df_fake_timeseries.plot(kind="line", x="date", y="website_traffic")
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment