Code Monkey home page Code Monkey logo

pyodide's Introduction

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

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}}
  1. Load the macros via

    import: https://raw.githubusercontent.com/LiaTemplates/pyodide/master/README.md

  2. Copy the definitions into your Project

  3. 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

Loading Libraries

                               --{{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.

Implementation

                               --{{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

pyodide's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.