Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d9bf5ca

Browse filesBrowse files
Fix #1814 - Basic mpy integration (#1815)
1 parent cd95a42 commit d9bf5ca
Copy full SHA for d9bf5ca

File tree

7 files changed

+146
-26
lines changed
Filter options

7 files changed

+146
-26
lines changed

‎Makefile

Copy file name to clipboardExpand all lines: Makefile
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ clean:
5656

5757
# Build PyScript.
5858
build:
59-
cd pyscript.core && npm run build
59+
cd pyscript.core && npx playwright install && npm run build
6060

6161
# Run the precommit checks (run eslint).
6262
precommit-check:

‎pyscript.core/package-lock.json

Copy file name to clipboardExpand all lines: pyscript.core/package-lock.json
+60Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pyscript.core/package.json

Copy file name to clipboardExpand all lines: pyscript.core/package.json
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
},
2121
"scripts": {
2222
"server": "npx static-handler --coi .",
23-
"build": "npm run build:toml && npm run build:stdlib && npm run build:plugins && npm run build:core && eslint src/ && npm run ts",
23+
"build": "npm run build:toml && npm run build:stdlib && npm run build:plugins && npm run build:core && eslint src/ && npm run ts && npm run test:mpy",
2424
"build:core": "rm -rf dist && rollup --config rollup/core.config.js",
2525
"build:plugins": "node rollup/plugins.cjs",
2626
"build:stdlib": "node rollup/stdlib.cjs",
2727
"build:toml": "node rollup/toml.cjs",
28+
"test:mpy": "static-handler --coi . 2>/dev/null & SH_PID=$!; EXIT_CODE=0; playwright test --fully-parallel test/ || EXIT_CODE=$?; kill $SH_PID 2>/dev/null; exit $EXIT_CODE",
2829
"dev": "node dev.cjs",
2930
"release": "npm run build && npm run zip",
3031
"size": "echo -e \"\\033[1mdist/*.js file size\\033[0m\"; for js in $(ls dist/*.js); do echo -e \"\\033[2m$js:\\033[0m $(cat $js | brotli | wc -c) bytes\"; done",
@@ -46,6 +47,7 @@
4647
"type-checked-collections": "^0.1.7"
4748
},
4849
"devDependencies": {
50+
"@playwright/test": "^1.39.0",
4951
"@rollup/plugin-node-resolve": "^15.2.3",
5052
"@rollup/plugin-terser": "^0.4.4",
5153
"@webreflection/toml-j0.4": "^1.1.3",

‎pyscript.core/src/stdlib.js

Copy file name to clipboardExpand all lines: pyscript.core/src/stdlib.js
+9-2Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import pyscript from "./stdlib/pyscript.js";
1010

1111
const { entries } = Object;
1212

13-
const python = ["from pathlib import Path as _Path", "_path = None"];
13+
const python = [
14+
"import os as _os",
15+
"from pathlib import Path as _Path",
16+
"_path = None",
17+
];
1418

1519
const write = (base, literal) => {
1620
for (const [key, value] of entries(literal)) {
@@ -19,7 +23,9 @@ const write = (base, literal) => {
1923
const code = JSON.stringify(value);
2024
python.push(`_path.write_text(${code})`);
2125
} else {
22-
python.push("_path.mkdir(parents=True, exist_ok=True)");
26+
// @see https://github.com/pyscript/pyscript/pull/1813#issuecomment-1781502909
27+
python.push(`if not _os.path.exists("${base}/${key}"):`);
28+
python.push(" _path.mkdir(parents=True, exist_ok=True)");
2329
write(`${base}/${key}`, value);
2430
}
2531
}
@@ -29,6 +35,7 @@ write(".", pyscript);
2935

3036
python.push("del _Path");
3137
python.push("del _path");
38+
python.push("del _os");
3239
python.push("\n");
3340

3441
export default python.join("\n");

‎pyscript.core/test/hooks.html

Copy file name to clipboardExpand all lines: pyscript.core/test/hooks.html
+27-19Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,19 @@
66
<title>PyScript Next Plugin Bug?</title>
77
<link rel="stylesheet" href="../dist/core.css">
88
<script type="module">
9-
import { hooks } from "../dist/core.js";
10-
11-
// Worker
12-
hooks.worker.onReady.add((wrap, xworker) => {
13-
console.log("worker", "onReady");
14-
console.log("worker", "wrap", wrap);
15-
console.log("worker", "xworker", xworker);
16-
});
17-
hooks.worker.onBeforeRun.add(() => {
18-
console.log("worker", "onBeforeRun");
19-
});
20-
hooks.worker.codeBeforeRun.add('print("worker", "codeBeforeRun")');
21-
hooks.worker.codeAfterRun.add('print("worker", "codeAfterRun")');
22-
hooks.worker.onAfterRun.add(() => {
23-
console.log("worker", "onAfterRun");
9+
addEventListener('mpy:done', () => {
10+
document.documentElement.classList.add('done');
2411
});
2512

13+
import { hooks } from "../dist/core.js";
14+
2615
// Main
2716
hooks.main.onReady.add((wrap, element) => {
2817
console.log("main", "onReady");
29-
console.log("main", "wrap", wrap);
30-
console.log("main", "element", element);
18+
if (location.search === '?debug') {
19+
console.debug("main", "wrap", wrap);
20+
console.debug("main", "element", element);
21+
}
3122
});
3223
hooks.main.onBeforeRun.add(() => {
3324
console.log("main", "onBeforeRun");
@@ -37,13 +28,30 @@
3728
hooks.main.onAfterRun.add(() => {
3829
console.log("main", "onAfterRun");
3930
});
31+
32+
// Worker
33+
hooks.worker.onReady.add((wrap, xworker) => {
34+
console.log("worker", "onReady");
35+
if (location.search === '?debug') {
36+
console.debug("worker", "wrap", wrap);
37+
console.debug("worker", "xworker", xworker);
38+
}
39+
});
40+
hooks.worker.onBeforeRun.add(() => {
41+
console.log("worker", "onBeforeRun");
42+
});
43+
hooks.worker.codeBeforeRun.add('print("worker", "codeBeforeRun")');
44+
hooks.worker.codeAfterRun.add('print("worker", "codeAfterRun")');
45+
hooks.worker.onAfterRun.add(() => {
46+
console.log("worker", "onAfterRun");
47+
});
4048
</script>
4149
</head>
4250
<body>
43-
<script type="py" worker>
51+
<script type="mpy" worker>
4452
print("actual code in worker")
4553
</script>
46-
<script type="py">
54+
<script type="mpy">
4755
print("actual code in main")
4856
</script>
4957
</body>

‎pyscript.core/test/mpy.html

Copy file name to clipboardExpand all lines: pyscript.core/test/mpy.html
+9-3Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,25 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>PyScript Next</title>
77
<script>
8-
addEventListener("mpy:ready", console.log);
8+
addEventListener('mpy:done', () => {
9+
document.documentElement.classList.add('done');
10+
});
911
</script>
1012
<link rel="stylesheet" href="../dist/core.css">
1113
<script type="module" src="../dist/core.js"></script>
1214
</head>
1315
<body>
1416
<script type="mpy">
1517
from pyscript import display
16-
display("Hello", "M-PyScript Next", append=False)
18+
display("Hello", "M-PyScript Main 1", append=False)
1719
</script>
20+
<mpy-script>
21+
from pyscript import display
22+
display("Hello", "M-PyScript Main 2", append=False)
23+
</mpy-script>
1824
<mpy-script worker>
1925
from pyscript import display
20-
display("Hello", "M-PyScript Next Worker", append=False)
26+
display("Hello", "M-PyScript Worker", append=False)
2127
</mpy-script>
2228
</body>
2329
</html>

‎pyscript.core/test/mpy.spec.js

Copy file name to clipboard
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { test, expect } from '@playwright/test';
2+
3+
test('MicroPython display', async ({ page }) => {
4+
await page.goto('http://localhost:8080/test/mpy.html');
5+
await page.waitForSelector('html.done');
6+
const body = await page.evaluate(() => document.body.innerText);
7+
await expect(body.trim()).toBe([
8+
'M-PyScript Main 1',
9+
'M-PyScript Main 2',
10+
'M-PyScript Worker',
11+
].join('\n'));
12+
});
13+
14+
test('MicroPython hooks', async ({ page }) => {
15+
const logs = [];
16+
page.on('console', msg => {
17+
const text = msg.text();
18+
if (!text.startsWith('['))
19+
logs.push(text);
20+
});
21+
await page.goto('http://localhost:8080/test/hooks.html');
22+
await page.waitForSelector('html.done');
23+
await expect(logs.join('\n')).toBe([
24+
'main onReady',
25+
'main onBeforeRun',
26+
'main codeBeforeRun',
27+
'actual code in main',
28+
'main codeAfterRun',
29+
'main onAfterRun',
30+
'worker onReady',
31+
'worker onBeforeRun',
32+
'worker codeBeforeRun',
33+
'actual code in worker',
34+
'worker codeAfterRun',
35+
'worker onAfterRun',
36+
].join('\n'));
37+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.