--{{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:
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 functionionality
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
--{{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
--{{0}}--
Only the Python standard library and six
are available at runtime, if you want
to make further libraries accessible in your scripts, then you have to load them
at startup with the @onload
macro. Simply call the function load_packages
with an array of all required Python libraries.
<!--
author: ...
email: ...
import: https://raw.githubusercontent.com/liaScript/pyodide_template/master/README.md
@onload: load_packages(["matplotlib", "numpy"]);
-->
...
Note:
scipy
is currently not available, due to memory restrictions at github, which does not allow to upload files larger than 100 MB.
--{{0}}--
This macro implementation only adds a simple script-tag that pushes the code of
your snippet directly to Pyodide. The @onload
macro is required to instantiate
Pyodide and load the required libraries, which might require some time, since
the loaded packages might be quite large.
script: https://cdn.jsdelivr.net/gh/LiaTemplates/pyodide/js/pyodide.js
@Pyodide.eval
<script>
if(window.pyodide_ready) {
pyodide.globals.print = (...e) => { e = e.slice(0,-1); console.log(...e) };
pyodide.runPython(`@input`);
}
else {
console.warn("Please wait, Pyodide is not ready yet...");
"LIA: stop";
}
</script>
@end
@onload
window.pyodide_ready = false;
languagePluginLoader.then(() => {
console.log("pyodide is ready")
if (window.py_packages) {
pyodide.loadPackage(window.py_packages).then(() => {
console.log("all packages loaded")
window.pyodide_ready = true;
});
}
else {
window.pyodide_ready = true;
}
});
var module = {};
window.load_packages = function (list) {
window.py_packages = list;
}
@end
--{{1}}--
If you want to minimize loading effort in your LiaScript project, you can also copy this code and paste it into your main comment header, see the code in the raw file of this document.
{{1}}
https://raw.githubusercontent.com/LiaTemplates/pyodide/master/README.md