mkdocs-pyscript
mkdocs-pyscript is a plugin for mkdocs that allows you to transform code blocks into executable Python scripts that run in the user's browser, with no backend server, using PyScript.
Installation
Install the plugin into your environment using pip, or your favorite environment manager that ues PYPI:
pip3 install mkdocs-pyscript
Enable the plugin by adding it to mkdocs.yaml:
plugins:
- mkdocs-pyscript
Usage
With this plugin enabled, all Python fenced code blocks (type py or python or any other label that maps to lang-python) will have an added LOAD button in the lower-right corrner. When clicked, the code block will be transformed into an editable code snippet (using codemirror). When the user clicks on the green "run" arrow in the lower right corner, or pressed SHIFT+ENTER when focused, will run the inner Python code using PyScript.
The included code is run in a Web Worker, so as not to block the main thread. Each snippet is run in a separate web worker; variables, objects, and names are not shared between executions of the same cell.
Environments
Documentation may benefit from some executable code blocks being isolated from others to create distinct examples. Add the env attribute to a fenced code block, using the style of the attr_list markdown plugin, to create an editor for the PyScript Environment, e.g. a specific copy of the interpreter isolated from other evnironments.
```{.py env="one"}
x = 1
```
```{.py env="one"}
print(x) # Shares an interpreter with the first block
```
```{.py env="two"}
print(x) # Error: 'x' is not defined (in this interpreter)
```
setup code
Some demo code snippets may require setup code to properly function, which don't necessarily need to be displayed to the user. To run a chunk of Python code before any code the user runs in a particular code editor, add the setup attribute to a fenced code block with a specific environment, using the style of the attr_list markdown plugin.
```{.py setup env="three"}
print("This is some setup code")
```
```{.py env="three"}
print("This is the main tag")
```
Configuration
mkdocs-pyscript supports options that can be set in mkdocs.yaml to control the behavior of the plugin
pyscript_version
The pyscript_version property of the plugin controls the version of PyScript that is loaded. If not specified, the current default version is releases/2024.4.2.
To support both pre-release snapshots and released versions of PyScript, the provided value is inserted into the following string:
SCRIPT = f'https://pyscript.net/{pyscript_version}/core.js'
That is, to load a specific release or snapshot, use:
#Load a release
plugins:
- mkdocs-pyscript:
pyscript_version: "releases/2024.2.1"
#Load a snapshot
plugins:
- mkdocs-pyscript:
pyscript_version: "snapshots/2023.11.1.RC3"
selective
By default, mkdocs-pyscript turns all blocks with type py or python into exectuable code snippets. To cause only selected blocks to be transformed:
- Set the
selectiveproperty totruein your configuration:
plugins:
- mkdocs-pyscript:
selective: true
- Specify
.pyscriptas an additional class for the codeblocks that you want to be runnable:
```{.py .pyscript}
print("Hello, world!")
```
Release files for mkdocs-pyscript 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mkdocs_pyscript-0.2.1.tar.gz | 16.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mkdocs_pyscript-0.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 33.8 kB
Release files / mkdocs_pyscript-0.2.1.tar.gz
| Download URL | mkdocs_pyscript-0.2.1.tar.gz |
|---|---|
| Size | 16.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d67e567a8e590adc67f22c9dd1560351768d43627b50582ce59db4c07043c384
|
|
BLAKE2b-256 checksum How to use checksums |
b17cea97543b6f0f8835dae618f49c45110757418cdd9726dfcd85bc33e66fa9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.0 CPython/3.10.12
|
Release files / mkdocs_pyscript-0.2.1-py3-none-any.whl
| Download URL | mkdocs_pyscript-0.2.1-py3-none-any.whl |
|---|---|
| Size | 17.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a913c4f34461fb9cc6c0ebef6a62398877891f173da44d6a44b49d1d0bc66cf7
|
|
BLAKE2b-256 checksum How to use checksums |
e62b3b3134aa8bbeb8cb6e11283af77fe23944fb0a6e7b3edcd2ce6e4f1c6b08
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.0 CPython/3.10.12
|