You can use Markdown-style fenced code blocks to dynamically generate quiz content. The code is executed, and its stdout replaces the code block in the final quiz file.
Requirements
- Top-level placement: Code block fences (
```) must not be indented. They must be at the top level of the document, not nested inside a question, choice, or feedback. - Security: Code execution is disabled by default. You must enable it using one of these methods:
- Run the CLI with the
--run-code-blocks flag. - Set
run_code_blocks = true in a .text2qti.bespon config file located in your user or home directory.
- Syntax: The opening fence must follow the pattern
```{.lang .run} or ```{.lang .run executable=<executable>}.
Executable Modes
- Language-based: If you use
{.lang .run}, text2qti uses the lang token to find an executable (e.g., python), saves the code to a temporary file, and runs lang <file>. - Explicit Executable: If you use
{.lang .run executable="path/to/exe"}, the lang token is ignored (used only for syntax highlighting), and the specified executable is used to run the code.
Python Specifics
When using .python, text2qti will attempt to use python3 if python3 exists and python is equivalent to python2, or if python is missing but python3 exists. To avoid ambiguity, use .python3 or .python2 explicitly.
Constraints
- Each code block runs in its own process; variables are not shared between blocks.
text2qti will throw an error if a code block generates duplicate questions or duplicate choices within a single question.
```{.python .run}
import textwrap
for x in [2, 3]:
print(textwrap.dedent(rf"""
1. What is ${x}\times 5$?
*a) ${x*5}$
b) ${x+1}$
"""))