= 1
a print(a + 1)
2
Ludovic Deneuville
If you want to run python code in your quarto document, first you will have to :
pip install jupyter
pip install matplotlib numpy pandas ...
Instead of install packages one by one
requirements.txt
at the project rootpip install -r requirements.txt
Official documentation :
On the left side, you can see the code entered in the .qmd file, and on the right side, you can see the result.
Cell options impact the execution and output of executable code blocks.
They are specified within comments at the beginning of a block.
Some option examples :
To facilitate import management, it is recommended to group all of them in the first cell.
If you add a dot (.) before the keyword python
, the cell will not be evaluated.
if you remove the dot (.) before the keyword Python
, the cell will be evaluated.
def fibonacci(n) -> list[int]:
"""Generate the Fibonacci sequence up to the nth term."""
if not isinstance(n, int):
raise TypeError("n must be an integer")
fib_sequence = []
a, b = 0, 1
for _ in range(n):
fib_sequence.append(a)
a, b = b, a + b
return fib_sequence
# Example usage
fib_10 = fibonacci(10)
mean_fib_10 = np.mean(fib_10)
print(fib_10)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
It is possible to embed code within a line of text :
---
title: "Executed python - Example"
description: "Example of a web page with executed python code"
author: "Ludovic Deneuville"
format:
html:
toc: true
toc-location: left
toc-expand: 3
code-tools:
source: true
toggle: false
from: markdown+emoji
number-sections: true
lightbox: true
---
## Python
If you want to run python code in your quarto document, first you will have to :
- [ ] Download and install [Python](https://www.python.org/downloads/){target="_blank"}
- [ ] Install jupyter
- `pip install jupyter`
- [ ] Install all needed packages
- `pip install matplotlib numpy pandas ...`
::: {.callout-tip}
Instead of install packages one by one
- list all needed packages in file `requirements.txt` at the project root
- run `pip install -r requirements.txt`
:::
Official documentation :
- [Using Python](https://quarto.org/docs/computations/python.html){target="_blank"}
- [Jupyter Code Cells](https://quarto.org/docs/reference/cells/cells-jupyter.html){target="_blank"}
## Cells
### How it works ?
On the left side, you can see the code entered in the *.qmd* file, and on the right side, you can see the result.
:::: {.columns}
::: {.column}
Code in your *.qmd* file
```{python}
a = 1
print(a + 1)
```
:::
::: {.column}
Output (Code + result)
```{python}
a = 1
print(a + 1)
```
:::
::::
### Cell options
Cell options impact the execution and output of executable code blocks.
They are specified within comments at the beginning of a block.
Some option examples :
```{python}
#| label: my_label # Unique label for code cell
#| eval: true # Evaluate the cell
#| echo: true # Print the code
#| output: false # Don't print the output
#| warning: false # Hide warnings
b = [1, 2, 3, 4]
print(b)
```
### Imports
To facilitate import management, it is recommended to group all of them in the first cell.
```{python}
#| label: python-imports
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
from IPython.display import Markdown
from tabulate import tabulate
```
### A non-evaluated cell
If you add a dot (.) before the keyword `python`, the cell will **not** be evaluated.
:::: {.columns}
::: {.column}
Code
```
```{.python}
x = np.linspace(-5, 5, 1000)
pdf = norm.pdf(x, 0, 1)
plt.plot(x, pdf, label='Normal Distribution')
plt.show()
```
:::
::: {.column}
Output
```{.python}
x = np.linspace(-3, 3, 1000)
pdf = norm.pdf(x, 0, 1)
plt.plot(x, pdf, label='Normal Distribution')
plt.show()
```
:::
::::
### An evaluated cell
if you remove the dot (.) before the keyword `Python`, the cell will be evaluated.
```{python}
x = np.linspace(-3, 3, 1000)
pdf = norm.pdf(x, 0, 1)
plt.plot(x, pdf, label='Normal Distribution', color='darkcyan')
plt.show()
```
## Tabulations
::: {.panel-tabset .nav-pills}
#### Code
``` {python}
#| filename: file.py
#| eval: false
np.random.seed(42)
pairs = np.random.rand(10, 2)
plt.scatter(pairs[:, 0], pairs[:, 1], color='darkcyan', alpha=0.7)
plt.show()
```
#### Plot
```{python}
#| code-fold: true
np.random.seed(42)
pairs = np.random.rand(10, 2)
plt.scatter(pairs[:, 0], pairs[:, 1], color='darkcyan', alpha=0.7)
plt.show()
```
#### Data
Display table of data using `tabulate` ([see imports](#python-imports))
``` {python}
#| code-fold: true
np.random.seed(42)
pairs = np.random.rand(10, 2)
Markdown(tabulate(
pairs,
headers=["X","Y"]
))
```
:::
## Line numbers
```{python}
#| code-line-numbers: true
def fibonacci(n) -> list[int]:
"""Generate the Fibonacci sequence up to the nth term."""
if not isinstance(n, int):
raise TypeError("n must be an integer")
fib_sequence = []
a, b = 0, 1
for _ in range(n):
fib_sequence.append(a)
a, b = b, a + b
return fib_sequence
# Example usage
fib_10 = fibonacci(10)
mean_fib_10 = np.mean(fib_10)
print(fib_10)
```
## Inline code
It is possible to embed code within a line of text :
- The mean of fibonacci(10) is `{python} mean_fib_10`.