Search code examples
pythonquarto

Quarto batch processing in python


Doing batch rendering with quarto using the following script From vscode

import os
import glob
from pathlib import Path

PHASE = [0, 0.1, 0.2]
for f in PHASE:
    os.system(
        f"quarto render individual_reference_template_copy.qmd --output phase_{f}.html -P phase:{f} --to html --no-cache"
    )

individual_reference_template_copy.qmd

---
execute:
  echo: true
format:
  html:
    code-fold: true
    theme: cosmo
jupyter: python3
---

```{python}
import numpy as np
import matplotlib.pyplot as plt
```

```{python}
#| tags: [parameters]
phase = 0
```

```{python}
x = np.arange(0,2*np.pi, 0.001)
y = np.sin(x+phase)
plt.scatter(x, y)
```

the output for the generated reports all show the same figure because the file

individual_reference_template_files\figure-html\cell-5-output-2.png

keeps getting overwritten.

EDIT replacing the first post with minimal example


Solution

  • An easy solution is to use embed-resources: true in the yaml section of the quarto document so that all the plots generated by quarto, css and javascript are bundled into a standalone HTML file.

    ---
    execute:
      echo: true
    format:
      html:
        code-fold: true
        theme: cosmo
        embed-resources: true
    jupyter: python3
    ---
    
    ```{python}
    import numpy as np
    import matplotlib.pyplot as plt
    ```
    
    ```{python}
    #| tags: [parameters]
    phase = 0
    ```
    
    ```{python}
    x = np.arange(0,2*np.pi, 0.001)
    y = np.sin(x+phase)
    plt.scatter(x, y)
    ```
    

    Then running the python script will create files with different plots for different parameters.