Skip to content

Commit 3059cc3

Browse files
authored
Update README.md
1 parent 3f24df4 commit 3059cc3

File tree

1 file changed

+60
-3
lines changed

1 file changed

+60
-3
lines changed

README.md

+60-3
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,40 @@ MicroPython transmuted into Javascript (WASM) by Emscripten.
55

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

8+
Testing and Contribution Needed, feel free to make an issue or even better, a PR
9+
10+
11+
What's New on 1.1
12+
--------------------
13+
14+
- New Async/Await or Promise API
15+
- New Python classes to expose JS API and Objects like DOM API, XHR, Node.JS require, and etc
16+
17+
818
Running with Node.js
919
--------------------
1020

21+
On Node.JS console
22+
1123
```javascript
12-
var mp_js = require('micropython');
24+
const mp_js = require('micropython');
1325

1426
mp_js.init(64 * 1024);
1527
mp_js.do_str("print('hello world')\n");
1628
```
1729

30+
On production/actual code use AsyncFunction or Promise to get the guaranteed result
31+
32+
```javascript
33+
(async () => { //AsyncFunction
34+
const mp_js = require('micropython');
35+
36+
mp_js.init(64 * 1024);
37+
await mp_js.do_str("variable1 = {'data1': 1}");
38+
await mp_js.do_str("variable1.get('data1')"); //Access variables for previous event loop
39+
})();
40+
```
41+
1842
Running with Webpack
1943
-----------------
2044
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)
@@ -28,8 +52,12 @@ And import it on your Javascript file
2852

2953
```javascript
3054
import mp_js from 'micropython';
31-
mp_js.init(64 * 1024);
32-
mp_js.do_str("print('hello world')\n");
55+
56+
(async () => {
57+
await mp_js;
58+
mp_js.init(64 * 1024);
59+
mp_js.do_str("print('hello world')\n");
60+
})();
3361
```
3462

3563
API
@@ -63,3 +91,32 @@ process_char(char)
6391

6492
Input character into MicroPython repl. `char` must be of type `number`. This
6593
will execute MicroPython code when necessary.
94+
95+
```
96+
init_python(stack_size)
97+
```
98+
99+
NEW!! This function execute js.py to expose JS API to Python, even some helper function and experimental asynchronous queue/stack logic. Example:
100+
101+
```javascript
102+
import mp_js from 'micropython';
103+
104+
(async () => {
105+
await mp_js;
106+
await mp_js.init_python(64 * 1024);
107+
await mp_js.do_str(`
108+
#This function executes every line one by one and await promise if it returns a promise
109+
exec("""
110+
111+
require = JS('require')
112+
fetch = require('node-fetch') #Or do JS('window.fetch') on browser
113+
response = fetch('https://github.com')
114+
response = wait(response) #This is the 'await' equivalent
115+
result = response.text()
116+
result = wait(result)
117+
print(result) #Print the resulting HTML
118+
119+
""")
120+
`);
121+
})();
122+
```

0 commit comments

Comments
 (0)