Pyodide
A template for using Pyodide in LiaScript
Install / Use
/learn @LiaTemplates/PyodideREADME
Pyodide - Template
--{{0}}--
A template for executing Python code in LiaScript based on the Pyodide webassembly port to JavaScript. This port tries to make Python scientific programming accessible within the browser, see the Iodide project for more information.
Try it on LiaScript:
https://liascript.github.io/course/?https://raw.githubusercontent.com/LiaTemplates/Pyodide/master/README.md
<!-- style="display:none" -->
See the project on Github:
https://github.com/LiaTemplates/pyodide
--{{1}}--
There are three ways to use this template. The easiest way is to use the
import statement and the url of the raw text-file of the master branch or any
other branch or version. But you can also copy the required functionality
directly into the header of your Markdown document, see therefor the
Implementation. And of course, you could also clone this project and
change it, as you wish.
{{1}}
-
Load the macros via
import: https://raw.githubusercontent.com/LiaTemplates/Pyodide/master/README.md -
Copy the definitions into your Project
-
Clone this repository on GitHub
@Pyodide.eval
--{{0}}--
Simply attach the macro @Pyodide.eval to the end of your code-block to make
your Python code executable.
import sys
for i in range(5):
print("Hello", 'World #', i)
sys.version
@Pyodide.eval
--{{1}}--
If you want to use matplotlib, you will have to pass your figure to the plot
function, as it is done in the last line below. This function converts your
image into a base64 representation and passes this string to the DOM. It is currently only possible to plot one figure per snippet.
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button, Slider
from matplotlib.widgets import Slider
# The parametrized function to be plotted
def f(variable, m):
return np.sin(np.pi*variable*m)**2//np.sin(np.pi*variable)**2
def main():
x_min = -1.1
x_max = 1.1
x_num = 1000
y_min = 0
y_max = 100
x_label = "$\\frac{\\Delta k \\cdot a }{2\\pi}$"
#x_label = "xlabel"
#y_label = "ylabel"
y_label = "$\\|F|^2$"
# Define slider parameters
init_m = 4
m_min = 1
m_max = 30
m_step = 1
slider_label = "Anzahl der Atome"
x = np.linspace(x_min, x_max, x_num)
# Create the figure and the line that we will manipulate
fig, ax = plt.subplots()
line, = ax.plot(x, f(x, init_m), lw=3)
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
ax.set_ylim(y_min, y_max)
# adjust the main plot to make room for the sliders
fig.subplots_adjust(left=0.25, bottom=0.25)
# Make a horizontal slider to control the frequency.
axfreq = fig.add_axes([0.25, 0.1, 0.65, 0.03])
m_slider = Slider(
ax=axfreq,
label=slider_label,
valmin=m_min,
valmax=m_max,
valstep=m_step,
valinit=init_m,
)
def update(val):
line.set_ydata(f(x, m_slider.val))
fig.canvas.draw_idle()
def reset(event):
m_slider.reset()
# register the update function with each slider
m_slider.on_changed(update)
resetax = fig.add_axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', hovercolor='0.975')
button.on_clicked(reset)
plt.show()
main()
@Pyodide.eval
@Pyodide.exec
This macro works similar to the previous one, but the code is only passed as a parameter. The user will only see the result and will not have the chance to directly modify the the Python code.
import sys
for i in range(5):
print("Hello", 'World #', i)
sys.version
import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2 * np.pi * t)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.grid(True, linestyle='-.')
ax.tick_params(labelcolor='r', labelsize='medium', width=3)
plt.show()
Loading Libraries
--{{0}}--
Only the Python standard library and six are available at the beginning, other
libraries are globally loaded, if defined within the script.
Note: loading large packages such as
scipymay take some time, sinc
Security Score
Audited on Jan 12, 2026
