File tree Expand file tree Collapse file tree 2 files changed +64
-13
lines changed Expand file tree Collapse file tree 2 files changed +64
-13
lines changed Original file line number Diff line number Diff line change 4
4
< meta charset ="UTF-8 ">
5
5
< meta name ="viewport " content ="width=device-width,initial-scale=1.0 ">
6
6
< script type ="module ">
7
- import { serialize } from 'https://esm.run/@ungap/serialization-registry' ;
8
- import '../converter.js' ;
9
-
10
- globalThis . test = arg => {
11
- console . log ( ...serialize ( arg ) ) ;
12
- } ;
13
-
14
- const base = 'https://cdn.jsdelivr.net/npm/@micropython/micropython-webassembly-pyscript@latest' ;
15
- const { loadMicroPython } = await import ( `${ base } /micropython.mjs` ) ;
16
- const interpreter = await loadMicroPython ( { url : `${ base } /micropython.wasm` } ) ;
17
7
const { textContent } = document . querySelector ( 'script[type=micropython]' ) ;
18
- interpreter . runPythonAsync ( textContent ) ;
8
+ const worker = new Worker ( './worker.js' , { type : 'module' } ) ;
9
+ worker . postMessage ( textContent ) ;
10
+ let i = 0 ;
11
+ ( function idle ( ) {
12
+ requestAnimationFrame ( idle ) ;
13
+ document . body . textContent = i ++ ;
14
+ } ( ) ) ;
19
15
</ script >
20
16
< script type ="micropython " async >
21
- import js
22
- js . test ( [ { 'a' : 123 } , { 'a' : 456 } ] )
17
+ import pyodide
18
+
19
+ print ( "importing pyodide.sys" )
20
+
21
+ sys = await pyodide . sys
22
+ print ( sys . version )
23
+
24
+ print ( "---" )
25
+
26
+ sys = await pyodide . sys
27
+ print ( sys . version )
28
+
29
+ print ( "---" )
30
+
31
+ pandas = await pyodide . pandas
32
+ print ( pandas . __version__ )
23
33
</ script >
24
34
</ head >
25
35
</ html >
Original file line number Diff line number Diff line change
1
+ const { promise, resolve } = Promise . withResolvers ( ) ;
2
+
3
+ addEventListener ( 'message' , async ( { data } ) => {
4
+ await promise ;
5
+ await interpreter . runPythonAsync ( data ) ;
6
+ } ) ;
7
+
8
+ // Instant MicroPython
9
+ const base = 'https://cdn.jsdelivr.net/npm/@micropython/micropython-webassembly-pyscript@latest' ;
10
+ const { loadMicroPython } = await import ( `${ base } /micropython.mjs` ) ;
11
+ const interpreter = await loadMicroPython ( { url : `${ base } /micropython.wasm` } ) ;
12
+
13
+ // Lazy Pyodide
14
+ let pyodide ;
15
+ interpreter . registerJsModule ( 'pyodide' , new Proxy ( new Map , {
16
+ async get ( locals , prop ) {
17
+ const [ module ] = prop . split ( / [ @ < > = ] / ) ;
18
+ if ( ! locals . has ( module ) ) {
19
+ if ( ! pyodide ) {
20
+ const { loadPyodide } = await import ( `https://cdn.jsdelivr.net/pyodide/v0.28.1/full/pyodide.mjs` ) ;
21
+ pyodide = await loadPyodide ( { indexURL : 'https://cdn.jsdelivr.net/pyodide/v0.28.1/full' } ) ;
22
+ }
23
+ try {
24
+ pyodide . runPython ( `import ${ module } ` , { locals } ) ;
25
+ }
26
+ catch {
27
+ let micropip = locals . get ( 'micropip' ) ;
28
+ if ( ! micropip ) {
29
+ await pyodide . loadPackage ( 'micropip' ) ;
30
+ micropip = pyodide . pyimport ( 'micropip' ) ;
31
+ locals . set ( 'micropip' , micropip ) ;
32
+ }
33
+ await micropip . install ( [ prop ] , { keep_going : true } ) ;
34
+ pyodide . runPython ( `import ${ module } ` , { locals } ) ;
35
+ }
36
+ }
37
+ return locals . get ( module ) ;
38
+ }
39
+ } ) ) ;
40
+
41
+ resolve ( ) ;
You can’t perform that action at this time.
0 commit comments