npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

micropython

v1.1.8

Published

A WASM module built from the official MicroPython port

Downloads

17

Readme

MicroPython.js

MicroPython transmuted into Javascript (WASM) by Emscripten.

Official Repo https://github.com/micropython/micropython/tree/master/ports/javascript

Testing and Contribution Needed, feel free to make an issue or even better, a PR

What's New on 1.1

  • New Async/Await or Promise API
  • New Python classes to expose JS API and Objects like DOM API, XHR, Node.JS require, and etc
  • New Python promise class to wait promises with emscripten_sleep, using Emterpreter

Running with Node.js

On Node.JS console

const mp_js = require('micropython');

mp_js.init(64 * 1024);
mp_js.do_str("print('hello world')\n");

On production/actual code use AsyncFunction or Promise to get the guaranteed result

(async () => { //AsyncFunction
  const mp_js = await require('micropython');

  mp_js.init(64 * 1024);
  await mp_js.do_str("variable1 = {'data1': 1}");
  await mp_js.do_str("variable1.get('data1')"); //Access variables from the previous event loop
})();

Running with Webpack

Running MicroPython on Webpack is a little bit tricky. It expects the firmware.wasm file at /static/js/firmware.wasm. So a simple solution is to make static and js folder on webpack's public directory and put firmware.wasm on it. (PR is accepted for a better solution)

mkdir -p public/static/js
cp node_modules/micropython/lib/firmware.wasm public/static/js

And import it on your Javascript file

import mp_js from 'micropython';

(async () => {
  await mp_js;
  mp_js.init(64 * 1024);
  mp_js.do_str("print('hello world')\n");
})();

Python API

The following functions and classes is used to interact with Javascript. Load this API with mp_js.init_python(stack_size)

JS(variable_name)

Check for variable on Javascript's global and return the corresponding types, functions and Javascript objects instantiate JSFunction and JSObject class. Promise instantiate JSPromise class.

wait(promise)

Wait for a promise to be resolved on Javascript, and then returns the value. Uses emscripten_sleep. Also available as JSPromise class function:

fetch = JS('require')('node-fetch')
response = fetch('https://github.com').wait() #Returns response object
html = response.text().wait() #Returns HTML string

Javascript API

The following functions have been exposed to Kavascript.

init(stack_size)

Initialize MicroPython with the given stack size in bytes. This must be called before attempting to interact with MicroPython.

do_str(code)

Execute the input code. code must be a string. Returns a promise resulting an stdout.

init_repl()

Initialize MicroPython repl. Must be called before entering characters into the repl.

process_char(char)

Input character into MicroPython repl. char must be of type number. This will execute MicroPython code when necessary.

init_python(stack_size)

This function execute js.py to expose JS Objects to Python, Example:

mp_js = require('micropython');

(async () => {
  await mp_js;
  await mp_js.init_python(64 * 1024);
  await mp_js.do_str(`

  import js

  fetch = False
  if isbrowser:
     fetch = JS('fetch')
  else:
     require = JS('require')
     fetch = require('node-fetch')
  response = fetch('https://github.com').wait()
  result = response.text().wait()
  print(result)
  
  `);
})();