|
| 1 | +# slidev-addon-python-runner |
| 2 | + |
| 3 | +Python runner for the [Monaco Runner feature](https://sli.dev/features/monaco-run) in [Slidev](https://sli.dev/). Code executed in browser using [Pyodide](https://pyodide.org/). |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +Firstly, install the package: |
| 10 | + |
| 11 | +```bash |
| 12 | +npm install slidev-addon-python-runner |
| 13 | +``` |
| 14 | + |
| 15 | +Then, add it as an addon in your headmatter in `slides.md`: |
| 16 | + |
| 17 | +```md |
| 18 | +--- |
| 19 | +addons: |
| 20 | + - slidev-addon-python-runner |
| 21 | + |
| 22 | +# Optional configuration for this runner |
| 23 | +python: |
| 24 | + # Install packages from PyPI. Default: [] |
| 25 | + installs: ["flask"] |
| 26 | + |
| 27 | + # Code executed to set up the environment. Default: "" |
| 28 | + prelude: | |
| 29 | + GREETING_FROM_PRELUDE = "Hello, Slidev!" |
| 30 | + |
| 31 | + # Automatically load the imported builtin packages. Default: true |
| 32 | + loadPackagesFromImports: true |
| 33 | + |
| 34 | + # Disable annoying warning from `pandas`. Default: true |
| 35 | + suppressDeprecationWarnings: true |
| 36 | + |
| 37 | + # Always reload the Python environment when the code changes. Default: false |
| 38 | + alwaysReload: false |
| 39 | + |
| 40 | + # Options passed to `loadPyodide`. Default: {} |
| 41 | + loadPyodideOptions: {} |
| 42 | +--- |
| 43 | +``` |
| 44 | + |
| 45 | +To add an interactive Python code runner, use the `monaco-run` directive: |
| 46 | + |
| 47 | +````md |
| 48 | +```python {monaco-run} |
| 49 | +from termcolor import colored |
| 50 | + |
| 51 | +print(colored("Hello, Slidev!", "blue")) |
| 52 | +``` |
| 53 | +```` |
| 54 | + |
| 55 | +## Bundle `pyodide` |
| 56 | + |
| 57 | +By default, when building slides (i.e. `slidev build`), the `pyodide` package will be replaced with the CDN version. This is because of https://github.com/pyodide/pyodide/issues/1949, which causes the imported python packages to be lost when using the bundled version. |
| 58 | + |
| 59 | +To bundle the local version of `pyodide`, set the `PYODIDE_BUNDLE` environment variable to `true`. Note that in this way you can't import python packages in the static build. |
0 commit comments