Course material may show code for reading, execute code while rendering, or produce interactive notebooks. The syntax distinguishes displayed examples from executable cells.

Static code

Use an ordinary fenced block when code should be read or copied but not run by Quarto:

```python
def concentration(amount, volume):
    return amount / volume
```

This provides syntax highlighting only, the cell above for example produces the following syntax-highlighted code

def concentration(amount, volume):
    return amount / volume

Executable Python

Configuration

In order to include executable Python code in a document the project must have a Python execution engine, this means

  • Having Python declared as a dependency in pixi.toml
  • Any packages used by the Python code need to be declared in pixi.toml.

An example with a mimimal dependency set is shown below

[dependencies]
quarto = ">=1.9.38,<2"
python = "==3.12"

[pypi-dependencies]
jupyter = ">=1.1.1, <2"

Additional packages can be added using

pixi add <package-name> --pypi

Example

When the project is configured with a Python execution engine, braces mark a cell for execution:

```{python}
amount = 0.004
volume = 0.2
amount / volume
print(volume)
```

This code will be executed during rendering, producing highlighted code and output like below

Code
amount = 0.004
volume = 0.2
amount / volume
print(volume)
0.2

Execution behavior, cached outputs, and output formats are controlled by document front matter and _quarto.yml.

Learner and answer cells

For new projects with the strip-solution teaching-tools extension, mark conditional cells with the preferred teaching option:

```{python}
#| teaching: exercise
#| eval: false
result = ...  # learner completes this cell
```

```{python}
#| teaching: solution
result = 0.004 / 0.2
result
```

With teaching.show-solutions: false, the student render removes the solution cell. With teaching.show-solutions: true, the instructor render removes the exercise cell. Mark student scaffold cells with #| eval: false when they should not execute; the extension does not do this automatically.

NoteExisting material

The extension still accepts legacy #| exercise: true and #| solution: true options for existing repositories. Use the teaching option for new authoring.

Verification

When editing executable material, verify that:

  • The relevant profile includes the intended cells.
  • Code executes without errors in rendered outputs.
  • A learner-facing cell leaves an appropriate starting point.
  • A generated notebook contains the cells and outputs learners are expected to use.