Add PyScript to your mkdocs site
Project description
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
selective
property totrue
in your configuration:
plugins:
- mkdocs-pyscript:
selective: true
- Specify
.pyscript
as an additional class for the codeblocks that you want to be runnable:
```{.py .pyscript}
print("Hello, world!")
```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file mkdocs_pyscript-0.2.0.tar.gz
.
File metadata
- Download URL: mkdocs_pyscript-0.2.0.tar.gz
- Upload date:
- Size: 16.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ddfbf1e94cef03fa550133c7a2457fa56ce9b8ce1c32c014cfec94dfa0c89cb9 |
|
MD5 | dcfb1ae894a68805d2f1c60d2c733500 |
|
BLAKE2b-256 | 994f6022147aa01f1eed8efd8b1632abb0f3d5f72f0152a9de3ec9de6acf36fc |
File details
Details for the file mkdocs_pyscript-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: mkdocs_pyscript-0.2.0-py3-none-any.whl
- Upload date:
- Size: 17.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 475555960cbe775904523574478842eb6b77ceb33aa2ff8cb0d2ba944f788219 |
|
MD5 | da237ce05fa071cfbe0d39690cb4e360 |
|
BLAKE2b-256 | 7e5571fac33df0a523a35b8fbad7b4285bf3b2f923c893041d8d1fa583adb85d |