{"id":15256990,"url":"https://github.com/rafi16jan/micropython-wasm","last_synced_at":"2025-04-12T00:31:55.685Z","repository":{"id":37819531,"uuid":"186518254","full_name":"rafi16jan/micropython-wasm","owner":"rafi16jan","description":"A WebAssembly module built from the official MicroPython port","archived":false,"fork":false,"pushed_at":"2022-11-16T01:55:17.000Z","size":1235,"stargazers_count":45,"open_issues_count":5,"forks_count":8,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-31T08:42:24.570Z","etag":null,"topics":["javascript","micropython","python","wasm","webassembly"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/micropython","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rafi16jan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-14T01:01:04.000Z","updated_at":"2024-10-08T20:30:10.000Z","dependencies_parsed_at":"2023-01-21T13:20:01.255Z","dependency_job_id":null,"html_url":"https://github.com/rafi16jan/micropython-wasm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafi16jan%2Fmicropython-wasm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafi16jan%2Fmicropython-wasm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafi16jan%2Fmicropython-wasm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rafi16jan%2Fmicropython-wasm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rafi16jan","download_url":"https://codeload.github.com/rafi16jan/micropython-wasm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223486880,"owners_count":17153241,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","micropython","python","wasm","webassembly"],"created_at":"2024-09-30T02:05:56.123Z","updated_at":"2024-11-07T09:03:33.570Z","avatar_url":"https://github.com/rafi16jan.png","language":"JavaScript","readme":"MicroPython.js\r\n==============\r\n\r\nMicroPython transmuted into Javascript (WASM) by Emscripten.\r\n\r\nOfficial Repo https://github.com/micropython/micropython/tree/master/ports/javascript\r\n\r\nTesting and Contribution Needed, feel free to make an issue or even better, a PR\r\n\r\n\r\nWhat's New on 1.1\r\n--------------------\r\n\r\n- New Async/Await or Promise API\r\n- New Python classes to expose JS API and Objects like DOM API, XHR, Node.JS require, and etc\r\n- New Python promise class to wait promises with emscripten_sleep, using Emterpreter\r\n\r\n\r\nRunning with Node.js\r\n--------------------\r\n\r\nOn Node.JS console\r\n\r\n```javascript\r\nconst mp_js = require('micropython');\r\n\r\nmp_js.init(64 * 1024);\r\nmp_js.do_str(\"print('hello world')\\n\");\r\n```\r\n\r\nOn production/actual code use AsyncFunction or Promise to get the guaranteed result\r\n\r\n```javascript\r\n(async () =\u003e { //AsyncFunction\r\n  const mp_js = await require('micropython');\r\n\r\n  mp_js.init(64 * 1024);\r\n  await mp_js.do_str(\"variable1 = {'data1': 1}\");\r\n  await mp_js.do_str(\"variable1.get('data1')\"); //Access variables from the previous event loop\r\n})();\r\n```\r\n\r\nRunning with Webpack\r\n-----------------\r\nRunning 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)\r\n\r\n```\r\nmkdir -p public/static/js\r\ncp node_modules/micropython/lib/firmware.wasm public/static/js\r\n```\r\n\r\nAnd import it on your Javascript file\r\n\r\n```javascript\r\nimport mp_js from 'micropython';\r\n\r\n(async () =\u003e {\r\n  await mp_js;\r\n  mp_js.init(64 * 1024);\r\n  const stdout = await mp_js.do_str(\"print('hello world')\\n\");\r\n  console.log(stdout);\r\n})();\r\n```\r\n\r\n\r\nPython API\r\n---\r\n\r\nThe following functions and classes is used to interact with Javascript. Load this API with ```mp_js.init_python(stack_size)```\r\n\r\n```python\r\nJS(variable_name)\r\n```\r\nCheck for variable on Javascript's global and return the corresponding types, functions and Javascript objects instantiate JSFunction and JSObject class. Promise instantiate JSPromise class.\r\n\r\n```python\r\nwait(promise)\r\n```\r\nWait for a promise to be resolved on Javascript, and then returns the value. Uses emscripten_sleep. Also available as JSPromise class function:\r\n\r\n```python\r\nfetch = JS('require')('node-fetch')\r\nresponse = fetch('https://github.com').wait() #Returns response object\r\nhtml = response.text().wait() #Returns HTML string\r\n```\r\n\r\nJavascript API\r\n---\r\n\r\nThe following functions have been exposed to Javascript.\r\n\r\n```\r\ninit(stack_size)\r\n```\r\n\r\nInitialize MicroPython with the given stack size in bytes. This must be\r\ncalled before attempting to interact with MicroPython.\r\n\r\n```\r\ndo_str(code)\r\n```\r\n\r\nExecute the input code. `code` must be a `string`. Returns a promise resulting an stdout.\r\n\r\n```\r\ninit_repl()\r\n```\r\n\r\nInitialize MicroPython repl. Must be called before entering characters into\r\nthe repl.\r\n\r\n```\r\nprocess_char(char)\r\n```\r\n\r\nInput character into MicroPython repl. `char` must be of type `number`. This \r\nwill execute MicroPython code when necessary.\r\n\r\n```\r\ninit_python(stack_size)\r\n```\r\n\r\nThis function execute js.py to expose JS Objects to Python, Example:\r\n\r\n```javascript\r\nmp_js = require('micropython');\r\n\r\n(async () =\u003e {\r\n  await mp_js;\r\n  await mp_js.init_python(64 * 1024);\r\n  await mp_js.do_str(`\r\n\r\n  import js\r\n\r\n  fetch = False\r\n  if isbrowser:\r\n     fetch = JS('fetch')\r\n  else:\r\n     require = JS('require')\r\n     fetch = require('node-fetch')\r\n  response = fetch('https://github.com').wait()\r\n  result = response.text().wait()\r\n  print(result)\r\n  \r\n  `);\r\n})();\r\n```\r\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frafi16jan%2Fmicropython-wasm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frafi16jan%2Fmicropython-wasm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frafi16jan%2Fmicropython-wasm/lists"}