Pyodide¤
This special pyodide
fence uses Pyodide, Ace and Highlight.js to render an interactive Python editor. Everything runs on the client side. The first time Pyodide is loaded by the browser can be a bit long, but then it will be cached and the next time you load the page it will be much faster.
Click the Run button in the top-right corner, or hit Ctrl+Enter to run the code. You can install packages with Micropip:
import micropip print("Installing cowsay...") await micropip.install("cowsay") print("done!")
```pyodide
import micropip
print("Installing cowsay...")
await micropip.install("cowsay")
print("done!")
```
Then you can import and use the packages you installed:
import cowsay cowsay.cow("Hello World")
```pyodide
import cowsay
cowsay.cow("Hello World")
```
Packages installed with Micropip are cached by the browser as well, making future installations much faster.
Sessions¤
Editors with the same session share the same globals()
dictionary, so you can reuse variables, classes, imports, etc., from another editor within the same session. This is why you can import cowsay
in this editor, given you actually installed it in the first. Sessions are ephemeral: everything is reset when reloading the page. This means you cannot persist sessions across multiple pages. Try refreshing your page and running the code of the second editor: you should get a ModuleNotFoundError.
To use other sessions, simply pass the session="name"
option to the code block:
something = "hello"
```pyodide session="something"
something = "hello"
```
Now lets print it in another editor with the same session:
print(something)
```pyodide session="something"
print(something)
```
And in another editor with the default session:
print(something)
```pyodide
print(something)
```
Pre-installing packages¤
In your own documentation pages, you might not want to add import micropip; await micropip.install("your-package")
to every editor to show how to use your package. In this case, you can use the install
option to pre-install packages. The option takes a list of comma-separated package distribution names:
import griffe import dependenpy print("OK!")
```pyodide install="griffe,dependenpy"
import griffe
import dependenpy
print("OK!")
```
Excluding assets¤
When you add a Pyodide fence to a page, Markdown Exec will inject <script>
and <link>
tags to load Javascript and CSS assets. If you add multiple Pyodide fences to the same page, the same assets will be included many times. The browser is clever enough not to re-download them everytime (they are cached), but we can still avoid re-injecting assets to make the HTML page smaller and faster.
```pyodide assets="no"
print("hello")
```
Make sure that at least one Pyodide fence per page injects the assets.
Editor themes¤
The editor provided by Ace supports different color themes. The complete list can be found here: https://github.com/ajaxorg/ace/tree/master/src/theme.
To use a specific theme for both light and dark schemes, use the theme
option on the Pyodide code block:
```pyodide theme="ambiance"
print("hello")
```
To specify different themes for the light and dark schemes, separate them with a comma:
```pyodide theme="ambiance,chaos"
print("hello")
```
See all previews below.
ambiance
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
chaos
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
chrome
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
cloud9_day
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
cloud9_night
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
cloud9_night_low_color
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
cloud_editor
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
cloud_editor_dark
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
clouds
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
clouds_midnight
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
cobalt
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
crimson_editor
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
dawn
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
dracula
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
dreamweaver
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
eclipse
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
github
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
github_dark
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
github_light_default
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
gob
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
gruvbox
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
gruvbox_dark_hard
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
gruvbox_light_hard
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
idle_fingers
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
iplastic
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
katzenmilch
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
kr_theme
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
kuroir
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
merbivore
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
merbivore_soft
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
mono_industrial
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
monokai
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
nord_dark
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
one_dark
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
pastel_on_dark
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
solarized_dark
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
solarized_light
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
sqlserver
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
terminal
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
textmate
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
tomorrow
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
tomorrow_night
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
tomorrow_night_blue
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
tomorrow_night_bright
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
tomorrow_night_eighties
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
twilight
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
vibrant_ink
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")
xcode
¤
from typing import Iterator # This is an example class Math: @staticmethod def fib(n: int) -> Iterator[int]: """Fibonacci series up to n.""" a, b = 0, 1 while a < n: yield a a, b = b, a + b result = sum(Math.fib(42)) print(f"The answer is {result}")