mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-20 02:37:41 -05:00
Compare commits
29 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c653296821 | ||
|
|
44cd6273ba | ||
|
|
d7d2dfb383 | ||
|
|
2d5cf096e0 | ||
|
|
6ee8217593 | ||
|
|
6d45728787 | ||
|
|
65954a627e | ||
|
|
2f1b764251 | ||
|
|
1fb6cddd70 | ||
|
|
239add4e20 | ||
|
|
4e4ac56729 | ||
|
|
1447cb3094 | ||
|
|
2f3659b676 | ||
|
|
910c666319 | ||
|
|
eee2f64c1d | ||
|
|
d080246a0f | ||
|
|
98c0f5e50d | ||
|
|
a1268f1aa2 | ||
|
|
69b8884045 | ||
|
|
df1d699fe6 | ||
|
|
84f197b657 | ||
|
|
5bed5ede52 | ||
|
|
f6d5cf06c8 | ||
|
|
30c6c830ae | ||
|
|
d7084f7f55 | ||
|
|
a87d2b3fea | ||
|
|
81a26363a3 | ||
|
|
53e945201d | ||
|
|
181d276c8b |
4
.github/ISSUE_TEMPLATE/bug-report.yml
vendored
4
.github/ISSUE_TEMPLATE/bug-report.yml
vendored
@@ -11,7 +11,9 @@ body:
|
|||||||
|
|
||||||
There will always be more issues than there is time to do them, and so we will need to selectively close issues that don't provide enough information, so we can focus our time on helping people like you who fill out the issue form completely. Thank you for your collaboration!
|
There will always be more issues than there is time to do them, and so we will need to selectively close issues that don't provide enough information, so we can focus our time on helping people like you who fill out the issue form completely. Thank you for your collaboration!
|
||||||
|
|
||||||
There are also already a lot of open issues, so please take 2 minutes and search through existing ones to see if what you are experiencing already exists
|
There are also already a lot of open issues, so please take 2 minutes and search through existing ones to see if what you are experiencing already exists.
|
||||||
|
|
||||||
|
Finally, if you are opening **a bug report related to PyScript.com** please [use this repository instead](https://github.com/anaconda/pyscript-dot-com-issues/issues/new/choose).
|
||||||
|
|
||||||
Thanks for helping PyScript be amazing. We are nothing without people like you helping build a better community 💐!
|
Thanks for helping PyScript be amazing. We are nothing without people like you helping build a better community 💐!
|
||||||
- type: checkboxes
|
- type: checkboxes
|
||||||
|
|||||||
13
.github/dependabot.yml
vendored
Normal file
13
.github/dependabot.yml
vendored
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
# Keep GitHub Actions up to date with GitHub's Dependabot...
|
||||||
|
# https://docs.github.com/en/code-security/dependabot/working-with-dependabot/keeping-your-actions-up-to-date-with-dependabot
|
||||||
|
# https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file#package-ecosystem
|
||||||
|
version: 2
|
||||||
|
updates:
|
||||||
|
- package-ecosystem: github-actions
|
||||||
|
directory: /
|
||||||
|
groups:
|
||||||
|
github-actions:
|
||||||
|
patterns:
|
||||||
|
- "*" # Group all Actions updates into a single larger pull request
|
||||||
|
schedule:
|
||||||
|
interval: weekly
|
||||||
6
.github/workflows/prepare-release.yml
vendored
6
.github/workflows/prepare-release.yml
vendored
@@ -17,12 +17,12 @@ jobs:
|
|||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Install node
|
- name: Install node
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 18.x
|
node-version: 18.x
|
||||||
|
|
||||||
- name: Cache node modules
|
- name: Cache node modules
|
||||||
uses: actions/cache@v3
|
uses: actions/cache@v4
|
||||||
env:
|
env:
|
||||||
cache-name: cache-node-modules
|
cache-name: cache-node-modules
|
||||||
with:
|
with:
|
||||||
@@ -48,7 +48,7 @@ jobs:
|
|||||||
run: zip -r -q ./build.zip ./dist
|
run: zip -r -q ./build.zip ./dist
|
||||||
|
|
||||||
- name: Prepare Release
|
- name: Prepare Release
|
||||||
uses: softprops/action-gh-release@v1
|
uses: softprops/action-gh-release@v2
|
||||||
with:
|
with:
|
||||||
draft: true
|
draft: true
|
||||||
prerelease: true
|
prerelease: true
|
||||||
|
|||||||
4
.github/workflows/publish-release.yml
vendored
4
.github/workflows/publish-release.yml
vendored
@@ -19,12 +19,12 @@ jobs:
|
|||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Install node
|
- name: Install node
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 18.x
|
node-version: 18.x
|
||||||
|
|
||||||
- name: Cache node modules
|
- name: Cache node modules
|
||||||
uses: actions/cache@v3
|
uses: actions/cache@v4
|
||||||
env:
|
env:
|
||||||
cache-name: cache-node-modules
|
cache-name: cache-node-modules
|
||||||
with:
|
with:
|
||||||
|
|||||||
4
.github/workflows/publish-snapshot.yml
vendored
4
.github/workflows/publish-snapshot.yml
vendored
@@ -23,12 +23,12 @@ jobs:
|
|||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Install node
|
- name: Install node
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 18.x
|
node-version: 18.x
|
||||||
|
|
||||||
- name: Cache node modules
|
- name: Cache node modules
|
||||||
uses: actions/cache@v3
|
uses: actions/cache@v4
|
||||||
env:
|
env:
|
||||||
cache-name: cache-node-modules
|
cache-name: cache-node-modules
|
||||||
with:
|
with:
|
||||||
|
|||||||
4
.github/workflows/publish-unstable.yml
vendored
4
.github/workflows/publish-unstable.yml
vendored
@@ -24,12 +24,12 @@ jobs:
|
|||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Install node
|
- name: Install node
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 18.x
|
node-version: 18.x
|
||||||
|
|
||||||
- name: Cache node modules
|
- name: Cache node modules
|
||||||
uses: actions/cache@v3
|
uses: actions/cache@v4
|
||||||
env:
|
env:
|
||||||
cache-name: cache-node-modules
|
cache-name: cache-node-modules
|
||||||
with:
|
with:
|
||||||
|
|||||||
10
.github/workflows/test.yml
vendored
10
.github/workflows/test.yml
vendored
@@ -37,12 +37,12 @@ jobs:
|
|||||||
run: git log --graph -3
|
run: git log --graph -3
|
||||||
|
|
||||||
- name: Install node
|
- name: Install node
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 20.x
|
node-version: 20.x
|
||||||
|
|
||||||
- name: Cache node modules
|
- name: Cache node modules
|
||||||
uses: actions/cache@v3
|
uses: actions/cache@v4
|
||||||
env:
|
env:
|
||||||
cache-name: cache-node-modules
|
cache-name: cache-node-modules
|
||||||
with:
|
with:
|
||||||
@@ -55,7 +55,7 @@ jobs:
|
|||||||
${{ runner.os }}-
|
${{ runner.os }}-
|
||||||
|
|
||||||
- name: setup Miniconda
|
- name: setup Miniconda
|
||||||
uses: conda-incubator/setup-miniconda@v2
|
uses: conda-incubator/setup-miniconda@v3
|
||||||
|
|
||||||
- name: Create and activate virtual environment
|
- name: Create and activate virtual environment
|
||||||
run: |
|
run: |
|
||||||
@@ -76,7 +76,7 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
make test-integration
|
make test-integration
|
||||||
|
|
||||||
- uses: actions/upload-artifact@v3
|
- uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: pyscript
|
name: pyscript
|
||||||
path: |
|
path: |
|
||||||
@@ -84,7 +84,7 @@ jobs:
|
|||||||
if-no-files-found: error
|
if-no-files-found: error
|
||||||
retention-days: 7
|
retention-days: 7
|
||||||
|
|
||||||
- uses: actions/upload-artifact@v3
|
- uses: actions/upload-artifact@v4
|
||||||
if: success() || failure()
|
if: success() || failure()
|
||||||
with:
|
with:
|
||||||
name: test_results
|
name: test_results
|
||||||
|
|||||||
2
.github/workflows/test_report.yml
vendored
2
.github/workflows/test_report.yml
vendored
@@ -8,7 +8,7 @@ jobs:
|
|||||||
report:
|
report:
|
||||||
runs-on: ubuntu-latest-8core
|
runs-on: ubuntu-latest-8core
|
||||||
steps:
|
steps:
|
||||||
- uses: dorny/test-reporter@v1.6.0
|
- uses: dorny/test-reporter@v1.9.0
|
||||||
with:
|
with:
|
||||||
artifact: test_results
|
artifact: test_results
|
||||||
name: Test reports
|
name: Test reports
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ repos:
|
|||||||
- id: trailing-whitespace
|
- id: trailing-whitespace
|
||||||
|
|
||||||
- repo: https://github.com/psf/black
|
- repo: https://github.com/psf/black
|
||||||
rev: 23.11.0
|
rev: 24.3.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: black
|
- id: black
|
||||||
exclude: pyscript\.core/src/stdlib/pyscript/__init__\.py
|
exclude: pyscript\.core/src/stdlib/pyscript/__init__\.py
|
||||||
@@ -46,7 +46,7 @@ repos:
|
|||||||
args: [--tab-width, "4"]
|
args: [--tab-width, "4"]
|
||||||
|
|
||||||
- repo: https://github.com/pycqa/isort
|
- repo: https://github.com/pycqa/isort
|
||||||
rev: 5.12.0
|
rev: 5.13.2
|
||||||
hooks:
|
hooks:
|
||||||
- id: isort
|
- id: isort
|
||||||
name: isort (python)
|
name: isort (python)
|
||||||
|
|||||||
100
CONTRIBUTING.md
100
CONTRIBUTING.md
@@ -79,3 +79,103 @@ The Project abides by the Organization's [trademark policy](https://github.com/p
|
|||||||
|
|
||||||
Part of MVG-0.1-beta.
|
Part of MVG-0.1-beta.
|
||||||
Made with love by GitHub. Licensed under the [CC-BY 4.0 License](https://creativecommons.org/licenses/by-sa/4.0/).
|
Made with love by GitHub. Licensed under the [CC-BY 4.0 License](https://creativecommons.org/licenses/by-sa/4.0/).
|
||||||
|
|
||||||
|
# Quick guide to pytest
|
||||||
|
|
||||||
|
We make heavy usage of pytest. Here is a quick guide and collection of useful options:
|
||||||
|
|
||||||
|
- To run all tests in the current directory and subdirectories: pytest
|
||||||
|
|
||||||
|
- To run tests in a specific directory or file: pytest path/to/dir/test_foo.py
|
||||||
|
|
||||||
|
- -s: disables output capturing
|
||||||
|
|
||||||
|
- --pdb: in case of exception, enter a (Pdb) prompt so that you can inspect what went wrong.
|
||||||
|
|
||||||
|
- -v: verbose mode
|
||||||
|
|
||||||
|
- -x: stop the execution as soon as one test fails
|
||||||
|
|
||||||
|
- -k foo: run only the tests whose full name contains foo
|
||||||
|
|
||||||
|
- -k 'foo and bar'
|
||||||
|
|
||||||
|
- -k 'foo and not bar'
|
||||||
|
|
||||||
|
## Running integration tests under pytest
|
||||||
|
|
||||||
|
make test is useful to run all the tests, but during the development is useful to have more control on how tests are run. The following guide assumes that you are in the directory pyscriptjs/tests/integration/.
|
||||||
|
|
||||||
|
### To run all the integration tests, single or multi core
|
||||||
|
|
||||||
|
$ pytest -xv
|
||||||
|
...
|
||||||
|
|
||||||
|
test_00_support.py::TestSupport::test_basic[chromium] PASSED [ 0%]
|
||||||
|
test_00_support.py::TestSupport::test_console[chromium] PASSED [ 1%]
|
||||||
|
test_00_support.py::TestSupport::test_check_js_errors_simple[chromium] PASSED [ 2%]
|
||||||
|
test_00_support.py::TestSupport::test_check_js_errors_expected[chromium] PASSED [ 3%]
|
||||||
|
test_00_support.py::TestSupport::test_check_js_errors_expected_but_didnt_raise[chromium] PASSED [ 4%]
|
||||||
|
test_00_support.py::TestSupport::test_check_js_errors_multiple[chromium] PASSED [ 5%]
|
||||||
|
...
|
||||||
|
|
||||||
|
-x means "stop at the first failure". -v means "verbose", so that you can see all the test names one by one. We try to keep tests in a reasonable order, from most basic to most complex. This way, if you introduced some bug in very basic things, you will notice immediately.
|
||||||
|
|
||||||
|
If you have the pytest-xdist plugin installed, you can run all the integration tests on 4 cores in parallel:
|
||||||
|
|
||||||
|
$ pytest -n 4
|
||||||
|
|
||||||
|
### To run a single test, headless
|
||||||
|
|
||||||
|
$ pytest test_01_basic.py -k test_pyscript_hello -s
|
||||||
|
...
|
||||||
|
[ 0.00 page.goto ] pyscript_hello.html
|
||||||
|
[ 0.01 request ] 200 - fake_server - http://fake_server/pyscript_hello.html
|
||||||
|
...
|
||||||
|
[ 0.17 console.info ] [py-loader] Downloading pyodide-x.y.z...
|
||||||
|
[ 0.18 request ] 200 - CACHED - https://cdn.jsdelivr.net/pyodide/vx.y.z/full/pyodide.js
|
||||||
|
...
|
||||||
|
[ 3.59 console.info ] [pyscript/main] PyScript page fully initialized
|
||||||
|
[ 3.60 console.log ] hello pyscript
|
||||||
|
|
||||||
|
-k selects tests by pattern matching as described above. -s instructs pytest to show the output to the terminal instead of capturing it. In the output you can see various useful things, including network requests and JS console messages.
|
||||||
|
|
||||||
|
### To run a single test, headed
|
||||||
|
|
||||||
|
$ pytest test_01_basic.py -k test_pyscript_hello -s --headed
|
||||||
|
...
|
||||||
|
|
||||||
|
Same as above, but with --headed the browser is shown in a window, and you can interact with it. The browser uses a fake server, which means that HTTP requests are cached.
|
||||||
|
|
||||||
|
Unfortunately, in this mode source maps does not seem to work, and you cannot debug the original typescript source code. This seems to be a bug in playwright, for which we have a workaround:
|
||||||
|
|
||||||
|
$ pytest test_01_basic.py -k test_pyscript_hello -s --headed --no-fake-server
|
||||||
|
...
|
||||||
|
|
||||||
|
As the name implies, -no-fake-server disables the fake server: HTTP requests are not cached, but source-level debugging works.
|
||||||
|
|
||||||
|
Finally:
|
||||||
|
|
||||||
|
$ pytest test_01_basic.py -k test_pyscript_hello -s --dev
|
||||||
|
...
|
||||||
|
|
||||||
|
--dev implies --headed --no-fake-server. In addition, it also automatically open chrome dev tools.
|
||||||
|
|
||||||
|
### To run only main thread or worker tests
|
||||||
|
|
||||||
|
By default, we run each test twice: one with execution_thread = "main" and one with execution_thread = "worker". If you want to run only half of them, you can use -m:
|
||||||
|
|
||||||
|
$ pytest -m main # run only the tests in the main thread
|
||||||
|
$ pytest -m worker # ron only the tests in the web worker
|
||||||
|
|
||||||
|
## Fake server, HTTP cache
|
||||||
|
|
||||||
|
By default, our test machinery uses a playwright router which intercepts and cache HTTP requests, so that for example you don't have to download pyodide again and again. This also enables the possibility of running tests in parallel on multiple cores.
|
||||||
|
|
||||||
|
The cache is stored using the pytest-cache plugin, which means that it survives across sessions.
|
||||||
|
|
||||||
|
If you want to temporarily disable the cache, the easiest thing is to use --no-fake-server, which bypasses it completely.
|
||||||
|
|
||||||
|
If you want to clear the cache, you can use the special option --clear-http-cache:
|
||||||
|
|
||||||
|
NOTE: this works only if you are inside tests/integration, or if you explicitly specify tests/integration from the command line. This is due to how pytest decides to search for and load the various conftest.py.
|
||||||
|
|||||||
615
pyscript.core/package-lock.json
generated
615
pyscript.core/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pyscript/core",
|
"name": "@pyscript/core",
|
||||||
"version": "0.3.23",
|
"version": "0.4.21",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"description": "PyScript",
|
"description": "PyScript",
|
||||||
"module": "./index.js",
|
"module": "./index.js",
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"server": "npx static-handler --coi .",
|
"server": "npx static-handler --coi .",
|
||||||
"build": "npm run build:3rd-party && npm run build:stdlib && npm run build:plugins && npm run build:core && eslint src/ && npm run ts && npm run test:mpy",
|
"build": "export ESLINT_USE_FLAT_CONFIG=false; npm run build:3rd-party && npm run build:stdlib && npm run build:plugins && npm run build:core && eslint src/ && npm run ts && npm run test:mpy",
|
||||||
"build:core": "rm -rf dist && rollup --config rollup/core.config.js && cp src/3rd-party/*.css dist/",
|
"build:core": "rm -rf dist && rollup --config rollup/core.config.js && cp src/3rd-party/*.css dist/",
|
||||||
"build:plugins": "node rollup/plugins.cjs",
|
"build:plugins": "node rollup/plugins.cjs",
|
||||||
"build:stdlib": "node rollup/stdlib.cjs",
|
"build:stdlib": "node rollup/stdlib.cjs",
|
||||||
@@ -42,31 +42,32 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@ungap/with-resolvers": "^0.1.0",
|
"@ungap/with-resolvers": "^0.1.0",
|
||||||
"basic-devtools": "^0.1.6",
|
"basic-devtools": "^0.1.6",
|
||||||
"polyscript": "^0.6.18",
|
"polyscript": "^0.12.3",
|
||||||
"sticky-module": "^0.1.1",
|
"sticky-module": "^0.1.1",
|
||||||
"to-json-callback": "^0.1.1",
|
"to-json-callback": "^0.1.1",
|
||||||
"type-checked-collections": "^0.1.7"
|
"type-checked-collections": "^0.1.7"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@codemirror/commands": "^6.3.3",
|
"@codemirror/commands": "^6.3.3",
|
||||||
"@codemirror/lang-python": "^6.1.3",
|
"@codemirror/lang-python": "^6.1.5",
|
||||||
"@codemirror/language": "^6.10.0",
|
"@codemirror/language": "^6.10.1",
|
||||||
"@codemirror/state": "^6.4.0",
|
"@codemirror/state": "^6.4.1",
|
||||||
"@codemirror/view": "^6.23.1",
|
"@codemirror/view": "^6.26.2",
|
||||||
"@playwright/test": "^1.41.1",
|
"@playwright/test": "^1.43.0",
|
||||||
"@rollup/plugin-commonjs": "^25.0.7",
|
"@rollup/plugin-commonjs": "^25.0.7",
|
||||||
"@rollup/plugin-node-resolve": "^15.2.3",
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
||||||
"@rollup/plugin-terser": "^0.4.4",
|
"@rollup/plugin-terser": "^0.4.4",
|
||||||
"@webreflection/toml-j0.4": "^1.1.3",
|
"@webreflection/toml-j0.4": "^1.1.3",
|
||||||
"@xterm/addon-fit": "^0.9.0-beta.1",
|
"@xterm/addon-fit": "^0.10.0",
|
||||||
"chokidar": "^3.5.3",
|
"@xterm/addon-web-links": "^0.11.0",
|
||||||
|
"chokidar": "^3.6.0",
|
||||||
"codemirror": "^6.0.1",
|
"codemirror": "^6.0.1",
|
||||||
"eslint": "^8.56.0",
|
"eslint": "^9.0.0",
|
||||||
"rollup": "^4.9.6",
|
"rollup": "^4.14.1",
|
||||||
"rollup-plugin-postcss": "^4.0.2",
|
"rollup-plugin-postcss": "^4.0.2",
|
||||||
"rollup-plugin-string": "^3.0.0",
|
"rollup-plugin-string": "^3.0.0",
|
||||||
"static-handler": "^0.4.3",
|
"static-handler": "^0.4.3",
|
||||||
"typescript": "^5.3.3",
|
"typescript": "^5.4.4",
|
||||||
"xterm": "^5.3.0",
|
"xterm": "^5.3.0",
|
||||||
"xterm-readline": "^1.1.1"
|
"xterm-readline": "^1.1.1"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -51,6 +51,9 @@ const modules = {
|
|||||||
"xterm_addon-fit.js": fetch(`${CDN}/@xterm/addon-fit/+esm`).then((b) =>
|
"xterm_addon-fit.js": fetch(`${CDN}/@xterm/addon-fit/+esm`).then((b) =>
|
||||||
b.text(),
|
b.text(),
|
||||||
),
|
),
|
||||||
|
"xterm_addon-web-links.js": fetch(
|
||||||
|
`${CDN}/@xterm/addon-web-links/+esm`,
|
||||||
|
).then((b) => b.text()),
|
||||||
"xterm.css": fetch(`${CDN}/xterm@${v("xterm")}/css/xterm.min.css`).then(
|
"xterm.css": fetch(`${CDN}/xterm@${v("xterm")}/css/xterm.min.css`).then(
|
||||||
(b) => b.text(),
|
(b) => b.text(),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -26,13 +26,42 @@ import { ErrorCode } from "./exceptions.js";
|
|||||||
import { robustFetch as fetch, getText } from "./fetch.js";
|
import { robustFetch as fetch, getText } from "./fetch.js";
|
||||||
import { hooks, main, worker, codeFor, createFunction } from "./hooks.js";
|
import { hooks, main, worker, codeFor, createFunction } from "./hooks.js";
|
||||||
|
|
||||||
|
import stdlib from "./stdlib.js";
|
||||||
|
export { stdlib };
|
||||||
|
|
||||||
// generic helper to disambiguate between custom element and script
|
// generic helper to disambiguate between custom element and script
|
||||||
const isScript = ({ tagName }) => tagName === "SCRIPT";
|
const isScript = ({ tagName }) => tagName === "SCRIPT";
|
||||||
|
|
||||||
|
// Used to create either Pyodide or MicroPython workers
|
||||||
|
// with the PyScript module available within the code
|
||||||
|
const [PyWorker, MPWorker] = [...TYPES.entries()].map(
|
||||||
|
([TYPE, interpreter]) =>
|
||||||
|
/**
|
||||||
|
* A `Worker` facade able to bootstrap on the worker thread only a PyScript module.
|
||||||
|
* @param {string} file the python file to run ina worker.
|
||||||
|
* @param {{config?: string | object, async?: boolean}} [options] optional configuration for the worker.
|
||||||
|
* @returns {Promise<Worker & {sync: object}>}
|
||||||
|
*/
|
||||||
|
async function PyScriptWorker(file, options) {
|
||||||
|
await configs.get(TYPE).plugins;
|
||||||
|
const xworker = XWorker.call(
|
||||||
|
new Hook(null, hooked.get(TYPE)),
|
||||||
|
file,
|
||||||
|
{
|
||||||
|
...options,
|
||||||
|
type: interpreter,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
assign(xworker.sync, sync);
|
||||||
|
return xworker.ready;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
// avoid multiple initialization of the same library
|
// avoid multiple initialization of the same library
|
||||||
const [
|
const [
|
||||||
{
|
{
|
||||||
PyWorker: exportedPyWorker,
|
PyWorker: exportedPyWorker,
|
||||||
|
MPWorker: exportedMPWorker,
|
||||||
hooks: exportedHooks,
|
hooks: exportedHooks,
|
||||||
config: exportedConfig,
|
config: exportedConfig,
|
||||||
whenDefined: exportedWhenDefined,
|
whenDefined: exportedWhenDefined,
|
||||||
@@ -40,6 +69,7 @@ const [
|
|||||||
alreadyLive,
|
alreadyLive,
|
||||||
] = stickyModule("@pyscript/core", {
|
] = stickyModule("@pyscript/core", {
|
||||||
PyWorker,
|
PyWorker,
|
||||||
|
MPWorker,
|
||||||
hooks,
|
hooks,
|
||||||
config: {},
|
config: {},
|
||||||
whenDefined,
|
whenDefined,
|
||||||
@@ -48,6 +78,7 @@ const [
|
|||||||
export {
|
export {
|
||||||
TYPES,
|
TYPES,
|
||||||
exportedPyWorker as PyWorker,
|
exportedPyWorker as PyWorker,
|
||||||
|
exportedMPWorker as MPWorker,
|
||||||
exportedHooks as hooks,
|
exportedHooks as hooks,
|
||||||
exportedConfig as config,
|
exportedConfig as config,
|
||||||
exportedWhenDefined as whenDefined,
|
exportedWhenDefined as whenDefined,
|
||||||
@@ -314,24 +345,3 @@ for (const [TYPE, interpreter] of TYPES) {
|
|||||||
// export the used config without allowing leaks through it
|
// export the used config without allowing leaks through it
|
||||||
exportedConfig[TYPE] = structuredClone(config);
|
exportedConfig[TYPE] = structuredClone(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* A `Worker` facade able to bootstrap on the worker thread only a PyScript module.
|
|
||||||
* @param {string} file the python file to run ina worker.
|
|
||||||
* @param {{config?: string | object, async?: boolean}} [options] optional configuration for the worker.
|
|
||||||
* @returns {Worker & {sync: ProxyHandler<object>}}
|
|
||||||
*/
|
|
||||||
function PyWorker(file, options) {
|
|
||||||
const hooks = hooked.get("py");
|
|
||||||
// this propagates pyscript worker hooks without needing a pyscript
|
|
||||||
// bootstrap + it passes arguments and it defaults to `pyodide`
|
|
||||||
// as the interpreter to use in the worker, as all hooks assume that
|
|
||||||
// and as `pyodide` is the only default interpreter that can deal with
|
|
||||||
// all the features we need to deliver pyscript out there.
|
|
||||||
const xworker = XWorker.call(new Hook(null, hooks), file, {
|
|
||||||
type: "pyodide",
|
|
||||||
...options,
|
|
||||||
});
|
|
||||||
assign(xworker.sync, sync);
|
|
||||||
return xworker;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
import { FetchError, ErrorCode } from "./exceptions.js";
|
import { FetchError, ErrorCode } from "./exceptions.js";
|
||||||
import { getText } from "polyscript/exports";
|
|
||||||
|
|
||||||
export { getText };
|
/**
|
||||||
|
* @param {Response} response
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
export const getText = (response) => response.text();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is a fetch wrapper that handles any non 200 responses and throws a
|
* This is a fetch wrapper that handles any non 200 responses and throws a
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// PyScript py-editor plugin
|
// PyScript py-editor plugin
|
||||||
import { Hook, XWorker, dedent } from "polyscript/exports";
|
import { Hook, XWorker, dedent } from "polyscript/exports";
|
||||||
import { TYPES } from "../core.js";
|
import { TYPES, stdlib } from "../core.js";
|
||||||
|
|
||||||
const RUN_BUTTON = `<svg style="height:20px;width:20px;vertical-align:-.125em;transform-origin:center;overflow:visible;color:green" viewBox="0 0 384 512" aria-hidden="true" role="img" xmlns="http://www.w3.org/2000/svg"><g transform="translate(192 256)" transform-origin="96 0"><g transform="translate(0,0) scale(1,1)"><path d="M361 215C375.3 223.8 384 239.3 384 256C384 272.7 375.3 288.2 361 296.1L73.03 472.1C58.21 482 39.66 482.4 24.52 473.9C9.377 465.4 0 449.4 0 432V80C0 62.64 9.377 46.63 24.52 38.13C39.66 29.64 58.21 29.99 73.03 39.04L361 215z" fill="currentColor" transform="translate(-192 -256)"></path></g></g></svg>`;
|
const RUN_BUTTON = `<svg style="height:20px;width:20px;vertical-align:-.125em;transform-origin:center;overflow:visible;color:green" viewBox="0 0 384 512" aria-hidden="true" role="img" xmlns="http://www.w3.org/2000/svg"><g transform="translate(192 256)" transform-origin="96 0"><g transform="translate(0,0) scale(1,1)"><path d="M361 215C375.3 223.8 384 239.3 384 256C384 272.7 375.3 288.2 361 296.1L73.03 472.1C58.21 482 39.66 482.4 24.52 473.9C9.377 465.4 0 449.4 0 432V80C0 62.64 9.377 46.63 24.52 38.13C39.66 29.64 58.21 29.99 73.03 39.04L361 215z" fill="currentColor" transform="translate(-192 -256)"></path></g></g></svg>`;
|
||||||
|
|
||||||
@@ -8,13 +8,15 @@ let id = 0;
|
|||||||
const getID = (type) => `${type}-editor-${id++}`;
|
const getID = (type) => `${type}-editor-${id++}`;
|
||||||
|
|
||||||
const envs = new Map();
|
const envs = new Map();
|
||||||
|
const configs = new Map();
|
||||||
|
|
||||||
const hooks = {
|
const hooks = {
|
||||||
worker: {
|
worker: {
|
||||||
|
codeBeforeRun: () => stdlib,
|
||||||
// works on both Pyodide and MicroPython
|
// works on both Pyodide and MicroPython
|
||||||
onReady: ({ runAsync, io }, { sync }) => {
|
onReady: ({ runAsync, io }, { sync }) => {
|
||||||
io.stdout = (line) => sync.write(line);
|
io.stdout = io.buffered(sync.write);
|
||||||
io.stderr = (line) => sync.writeErr(line);
|
io.stderr = io.buffered(sync.writeErr);
|
||||||
sync.revoke();
|
sync.revoke();
|
||||||
sync.runAsync = runAsync;
|
sync.runAsync = runAsync;
|
||||||
},
|
},
|
||||||
@@ -23,15 +25,26 @@ const hooks = {
|
|||||||
|
|
||||||
async function execute({ currentTarget }) {
|
async function execute({ currentTarget }) {
|
||||||
const { env, pySrc, outDiv } = this;
|
const { env, pySrc, outDiv } = this;
|
||||||
|
const hasRunButton = !!currentTarget;
|
||||||
|
|
||||||
|
if (hasRunButton) {
|
||||||
currentTarget.disabled = true;
|
currentTarget.disabled = true;
|
||||||
outDiv.innerHTML = "";
|
outDiv.innerHTML = "";
|
||||||
|
}
|
||||||
|
|
||||||
if (!envs.has(env)) {
|
if (!envs.has(env)) {
|
||||||
const srcLink = URL.createObjectURL(new Blob([""]));
|
const srcLink = URL.createObjectURL(new Blob([""]));
|
||||||
const xworker = XWorker.call(new Hook(null, hooks), srcLink, {
|
const details = { type: this.interpreter };
|
||||||
type: this.interpreter,
|
const { config } = this;
|
||||||
});
|
if (config) {
|
||||||
|
details.configURL = config;
|
||||||
|
const { parse } = config.endsWith(".toml")
|
||||||
|
? await import(/* webpackIgnore: true */ "../3rd-party/toml.js")
|
||||||
|
: JSON;
|
||||||
|
details.config = parse(await fetch(config).then((r) => r.text()));
|
||||||
|
}
|
||||||
|
|
||||||
|
const xworker = XWorker.call(new Hook(null, hooks), srcLink, details);
|
||||||
|
|
||||||
const { sync } = xworker;
|
const { sync } = xworker;
|
||||||
const { promise, resolve } = Promise.withResolvers();
|
const { promise, resolve } = Promise.withResolvers();
|
||||||
@@ -46,21 +59,25 @@ async function execute({ currentTarget }) {
|
|||||||
// before executing the current code
|
// before executing the current code
|
||||||
envs.get(env).then((xworker) => {
|
envs.get(env).then((xworker) => {
|
||||||
xworker.onerror = ({ error }) => {
|
xworker.onerror = ({ error }) => {
|
||||||
|
if (hasRunButton) {
|
||||||
outDiv.innerHTML += `<span style='color:red'>${
|
outDiv.innerHTML += `<span style='color:red'>${
|
||||||
error.message || error
|
error.message || error
|
||||||
}</span>\n`;
|
}</span>\n`;
|
||||||
|
}
|
||||||
console.error(error);
|
console.error(error);
|
||||||
};
|
};
|
||||||
|
|
||||||
const enable = () => {
|
const enable = () => {
|
||||||
currentTarget.disabled = false;
|
if (hasRunButton) currentTarget.disabled = false;
|
||||||
};
|
};
|
||||||
const { sync } = xworker;
|
const { sync } = xworker;
|
||||||
sync.write = (str) => {
|
sync.write = (str) => {
|
||||||
outDiv.innerText += `${str}\n`;
|
if (hasRunButton) outDiv.innerText += `${str}\n`;
|
||||||
};
|
};
|
||||||
sync.writeErr = (str) => {
|
sync.writeErr = (str) => {
|
||||||
|
if (hasRunButton) {
|
||||||
outDiv.innerHTML += `<span style='color:red'>${str}</span>\n`;
|
outDiv.innerHTML += `<span style='color:red'>${str}</span>\n`;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
sync.runAsync(pySrc).then(enable, enable);
|
sync.runAsync(pySrc).then(enable, enable);
|
||||||
});
|
});
|
||||||
@@ -120,7 +137,6 @@ const init = async (script, type, interpreter) => {
|
|||||||
{ keymap },
|
{ keymap },
|
||||||
{ defaultKeymap },
|
{ defaultKeymap },
|
||||||
] = await Promise.all([
|
] = await Promise.all([
|
||||||
// TODO: find a way to actually produce these bundles locally
|
|
||||||
import(/* webpackIgnore: true */ "../3rd-party/codemirror.js"),
|
import(/* webpackIgnore: true */ "../3rd-party/codemirror.js"),
|
||||||
import(/* webpackIgnore: true */ "../3rd-party/codemirror_state.js"),
|
import(/* webpackIgnore: true */ "../3rd-party/codemirror_state.js"),
|
||||||
import(
|
import(
|
||||||
@@ -131,6 +147,42 @@ const init = async (script, type, interpreter) => {
|
|||||||
import(/* webpackIgnore: true */ "../3rd-party/codemirror_commands.js"),
|
import(/* webpackIgnore: true */ "../3rd-party/codemirror_commands.js"),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
const isSetup = script.hasAttribute("setup");
|
||||||
|
const hasConfig = script.hasAttribute("config");
|
||||||
|
const env = `${interpreter}-${script.getAttribute("env") || getID(type)}`;
|
||||||
|
|
||||||
|
if (hasConfig && configs.has(env)) {
|
||||||
|
throw new SyntaxError(
|
||||||
|
configs.get(env)
|
||||||
|
? `duplicated config for env: ${env}`
|
||||||
|
: `unable to add a config to the env: ${env}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
configs.set(env, hasConfig);
|
||||||
|
|
||||||
|
const source = script.src
|
||||||
|
? await fetch(script.src).then((b) => b.text())
|
||||||
|
: script.textContent;
|
||||||
|
const context = {
|
||||||
|
interpreter,
|
||||||
|
env,
|
||||||
|
config:
|
||||||
|
hasConfig &&
|
||||||
|
new URL(script.getAttribute("config"), location.href).href,
|
||||||
|
get pySrc() {
|
||||||
|
return isSetup ? source : editor.state.doc.toString();
|
||||||
|
},
|
||||||
|
get outDiv() {
|
||||||
|
return isSetup ? null : outDiv;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isSetup) {
|
||||||
|
execute.call(context, { currentTarget: null });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const selector = script.getAttribute("target");
|
const selector = script.getAttribute("target");
|
||||||
|
|
||||||
let target;
|
let target;
|
||||||
@@ -149,18 +201,6 @@ const init = async (script, type, interpreter) => {
|
|||||||
if (!target.hasAttribute("exec-id")) target.setAttribute("exec-id", 0);
|
if (!target.hasAttribute("exec-id")) target.setAttribute("exec-id", 0);
|
||||||
if (!target.hasAttribute("root")) target.setAttribute("root", target.id);
|
if (!target.hasAttribute("root")) target.setAttribute("root", target.id);
|
||||||
|
|
||||||
const env = `${interpreter}-${script.getAttribute("env") || getID(type)}`;
|
|
||||||
const context = {
|
|
||||||
interpreter,
|
|
||||||
env,
|
|
||||||
get pySrc() {
|
|
||||||
return editor.state.doc.toString();
|
|
||||||
},
|
|
||||||
get outDiv() {
|
|
||||||
return outDiv;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
// @see https://github.com/JeffersGlass/mkdocs-pyscript/blob/main/mkdocs_pyscript/js/makeblocks.js
|
// @see https://github.com/JeffersGlass/mkdocs-pyscript/blob/main/mkdocs_pyscript/js/makeblocks.js
|
||||||
const listener = execute.bind(context);
|
const listener = execute.bind(context);
|
||||||
const [boxDiv, outDiv] = makeBoxDiv(listener, type);
|
const [boxDiv, outDiv] = makeBoxDiv(listener, type);
|
||||||
@@ -210,7 +250,7 @@ const resetTimeout = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// triggered both ASAP on the living DOM and via MutationObserver later
|
// triggered both ASAP on the living DOM and via MutationObserver later
|
||||||
const pyEditor = async () => {
|
const pyEditor = () => {
|
||||||
if (timeout) return;
|
if (timeout) return;
|
||||||
timeout = setTimeout(resetTimeout, 250);
|
timeout = setTimeout(resetTimeout, 250);
|
||||||
for (const [type, interpreter] of TYPES) {
|
for (const [type, interpreter] of TYPES) {
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
// PyScript py-terminal plugin
|
// PyScript py-terminal plugin
|
||||||
import { TYPES, hooks } from "../core.js";
|
import { TYPES, hooks } from "../core.js";
|
||||||
import { notify } from "./error.js";
|
import { notify } from "./error.js";
|
||||||
import { defineProperty } from "polyscript/exports";
|
import { customObserver, defineProperties } from "polyscript/exports";
|
||||||
|
|
||||||
const SELECTOR = [...TYPES.keys()]
|
// will contain all valid selectors
|
||||||
.map((type) => `script[type="${type}"][terminal],${type}-script[terminal]`)
|
const SELECTORS = [];
|
||||||
.join(",");
|
|
||||||
|
|
||||||
// show the error on main and
|
// show the error on main and
|
||||||
// stops the module from keep executing
|
// stops the module from keep executing
|
||||||
@@ -14,8 +13,6 @@ const notifyAndThrow = (message) => {
|
|||||||
throw new Error(message);
|
throw new Error(message);
|
||||||
};
|
};
|
||||||
|
|
||||||
const notParsedYet = (script) => !bootstrapped.has(script);
|
|
||||||
|
|
||||||
const onceOnMain = ({ attributes: { worker } }) => !worker;
|
const onceOnMain = ({ attributes: { worker } }) => !worker;
|
||||||
|
|
||||||
const bootstrapped = new WeakSet();
|
const bootstrapped = new WeakSet();
|
||||||
@@ -25,75 +22,128 @@ let addStyle = true;
|
|||||||
// this callback will be serialized as string and it never needs
|
// this callback will be serialized as string and it never needs
|
||||||
// to be invoked multiple times. Each xworker here is bootstrapped
|
// to be invoked multiple times. Each xworker here is bootstrapped
|
||||||
// only once thanks to the `sync.is_pyterminal()` check.
|
// only once thanks to the `sync.is_pyterminal()` check.
|
||||||
const workerReady = ({ interpreter, io, run }, { sync }) => {
|
const workerReady = ({ interpreter, io, run, type }, { sync }) => {
|
||||||
if (!sync.is_pyterminal()) return;
|
if (!sync.is_pyterminal()) return;
|
||||||
|
|
||||||
// in workers it's always safe to grab the polyscript currentScript
|
// in workers it's always safe to grab the polyscript currentScript
|
||||||
run("from polyscript.currentScript import terminal as __terminal__");
|
// the ugly `_` dance is due MicroPython not able to import via:
|
||||||
|
// `from polyscript.currentScript import terminal as __terminal__`
|
||||||
|
run(
|
||||||
|
"from polyscript import currentScript as _; __terminal__ = _.terminal; del _",
|
||||||
|
);
|
||||||
|
|
||||||
// This part is inevitably duplicated as external scope
|
|
||||||
// can't be reached by workers out of the box.
|
|
||||||
// The detail is that here we use sync though, not readline.
|
|
||||||
const decoder = new TextDecoder();
|
|
||||||
let data = "";
|
let data = "";
|
||||||
|
const { pyterminal_read, pyterminal_write } = sync;
|
||||||
|
const decoder = new TextDecoder();
|
||||||
const generic = {
|
const generic = {
|
||||||
isatty: true,
|
isatty: false,
|
||||||
write(buffer) {
|
write(buffer) {
|
||||||
data = decoder.decode(buffer);
|
data = decoder.decode(buffer);
|
||||||
sync.pyterminal_write(data);
|
pyterminal_write(data);
|
||||||
return buffer.length;
|
return buffer.length;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// This part works already in both Pyodide and MicroPython
|
||||||
|
io.stderr = (error) => {
|
||||||
|
pyterminal_write(String(error.message || error));
|
||||||
|
};
|
||||||
|
|
||||||
|
// MicroPython has no code or code.interact()
|
||||||
|
// This part patches it in a way that simulates
|
||||||
|
// the code.interact() module in Pyodide.
|
||||||
|
if (type === "mpy") {
|
||||||
|
// monkey patch global input otherwise broken in MicroPython
|
||||||
|
interpreter.registerJsModule("_pyscript_input", {
|
||||||
|
input: pyterminal_read,
|
||||||
|
});
|
||||||
|
run("from _pyscript_input import input");
|
||||||
|
|
||||||
|
// this is needed to avoid truncated unicode in MicroPython
|
||||||
|
// the reason is that `linebuffer` false just send one byte
|
||||||
|
// per time and readline here doesn't like it much.
|
||||||
|
// MicroPython also has issues with code-points and
|
||||||
|
// replProcessChar(byte) but that function accepts only
|
||||||
|
// one byte per time so ... we have an issue!
|
||||||
|
// @see https://github.com/pyscript/pyscript/pull/2018
|
||||||
|
// @see https://github.com/WebReflection/buffer-points
|
||||||
|
const bufferPoints = (stdio) => {
|
||||||
|
const bytes = [];
|
||||||
|
let needed = 0;
|
||||||
|
return (buffer) => {
|
||||||
|
let written = 0;
|
||||||
|
for (const byte of buffer) {
|
||||||
|
bytes.push(byte);
|
||||||
|
// @see https://encoding.spec.whatwg.org/#utf-8-bytes-needed
|
||||||
|
if (needed) needed--;
|
||||||
|
else if (0xc2 <= byte && byte <= 0xdf) needed = 1;
|
||||||
|
else if (0xe0 <= byte && byte <= 0xef) needed = 2;
|
||||||
|
else if (0xf0 <= byte && byte <= 0xf4) needed = 3;
|
||||||
|
if (!needed) {
|
||||||
|
written += bytes.length;
|
||||||
|
stdio(new Uint8Array(bytes.splice(0)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return written;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
io.stdout = bufferPoints(generic.write);
|
||||||
|
|
||||||
|
// tiny shim of the code module with only interact
|
||||||
|
// to bootstrap a REPL like environment
|
||||||
|
interpreter.registerJsModule("code", {
|
||||||
|
interact() {
|
||||||
|
let input = "";
|
||||||
|
let length = 1;
|
||||||
|
|
||||||
|
const encoder = new TextEncoder();
|
||||||
|
const acc = [];
|
||||||
|
const handlePoints = bufferPoints((buffer) => {
|
||||||
|
acc.push(...buffer);
|
||||||
|
pyterminal_write(decoder.decode(buffer));
|
||||||
|
});
|
||||||
|
|
||||||
|
// avoid duplicating the output produced by the input
|
||||||
|
io.stdout = (buffer) =>
|
||||||
|
length++ > input.length ? handlePoints(buffer) : 0;
|
||||||
|
|
||||||
|
interpreter.replInit();
|
||||||
|
|
||||||
|
// loop forever waiting for user inputs
|
||||||
|
(function repl() {
|
||||||
|
const out = decoder.decode(new Uint8Array(acc.splice(0)));
|
||||||
|
// print in current line only the last line produced by the REPL
|
||||||
|
const data = `${pyterminal_read(out.split("\n").at(-1))}\r`;
|
||||||
|
length = 0;
|
||||||
|
input = encoder.encode(data);
|
||||||
|
for (const c of input) interpreter.replProcessChar(c);
|
||||||
|
repl();
|
||||||
|
})();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} else {
|
||||||
interpreter.setStdout(generic);
|
interpreter.setStdout(generic);
|
||||||
interpreter.setStderr(generic);
|
interpreter.setStderr(generic);
|
||||||
interpreter.setStdin({
|
interpreter.setStdin({
|
||||||
isatty: true,
|
isatty: false,
|
||||||
stdin: () => sync.pyterminal_read(data),
|
stdin: () => pyterminal_read(data),
|
||||||
});
|
});
|
||||||
|
|
||||||
io.stderr = (error) => {
|
|
||||||
sync.pyterminal_write(`${error.message || error}\n`);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
const pyTerminal = async () => {
|
|
||||||
const terminals = document.querySelectorAll(SELECTOR);
|
|
||||||
|
|
||||||
const unknown = [].filter.call(terminals, notParsedYet);
|
|
||||||
|
|
||||||
// no results will look further for runtime nodes
|
|
||||||
if (!unknown.length) return;
|
|
||||||
// early flag elements as known to avoid concurrent
|
|
||||||
// MutationObserver invokes of this async handler
|
|
||||||
else unknown.forEach(bootstrapped.add, bootstrapped);
|
|
||||||
|
|
||||||
// we currently support only one terminal as in "classic"
|
|
||||||
if ([].filter.call(terminals, onceOnMain).length > 1)
|
|
||||||
notifyAndThrow("You can use at most 1 main terminal");
|
|
||||||
|
|
||||||
// import styles lazily
|
|
||||||
if (addStyle) {
|
|
||||||
addStyle = false;
|
|
||||||
document.head.append(
|
|
||||||
Object.assign(document.createElement("link"), {
|
|
||||||
rel: "stylesheet",
|
|
||||||
href: new URL("./xterm.css", import.meta.url),
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const pyTerminal = async (element) => {
|
||||||
// lazy load these only when a valid terminal is found
|
// lazy load these only when a valid terminal is found
|
||||||
const [{ Terminal }, { Readline }, { FitAddon }] = await Promise.all([
|
const [{ Terminal }, { Readline }, { FitAddon }, { WebLinksAddon }] =
|
||||||
|
await Promise.all([
|
||||||
import(/* webpackIgnore: true */ "../3rd-party/xterm.js"),
|
import(/* webpackIgnore: true */ "../3rd-party/xterm.js"),
|
||||||
import(/* webpackIgnore: true */ "../3rd-party/xterm-readline.js"),
|
import(/* webpackIgnore: true */ "../3rd-party/xterm-readline.js"),
|
||||||
import(/* webpackIgnore: true */ "../3rd-party/xterm_addon-fit.js"),
|
import(/* webpackIgnore: true */ "../3rd-party/xterm_addon-fit.js"),
|
||||||
|
import(
|
||||||
|
/* webpackIgnore: true */ "../3rd-party/xterm_addon-web-links.js"
|
||||||
|
),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
for (const element of unknown) {
|
|
||||||
// hopefully to be removed in the near future!
|
|
||||||
if (element.matches('script[type="mpy"],mpy-script'))
|
|
||||||
notifyAndThrow("Unsupported terminal.");
|
|
||||||
|
|
||||||
const readline = new Readline();
|
const readline = new Readline();
|
||||||
|
|
||||||
// common main thread initialization for both worker
|
// common main thread initialization for both worker
|
||||||
@@ -121,10 +171,29 @@ const pyTerminal = async () => {
|
|||||||
const fitAddon = new FitAddon();
|
const fitAddon = new FitAddon();
|
||||||
terminal.loadAddon(fitAddon);
|
terminal.loadAddon(fitAddon);
|
||||||
terminal.loadAddon(readline);
|
terminal.loadAddon(readline);
|
||||||
|
terminal.loadAddon(new WebLinksAddon());
|
||||||
terminal.open(target);
|
terminal.open(target);
|
||||||
fitAddon.fit();
|
fitAddon.fit();
|
||||||
terminal.focus();
|
terminal.focus();
|
||||||
defineProperty(element, "terminal", { value: terminal });
|
defineProperties(element, {
|
||||||
|
terminal: { value: terminal },
|
||||||
|
process: {
|
||||||
|
value: async (code) => {
|
||||||
|
// this loop is the only way I could find to actually simulate
|
||||||
|
// the user input char after char in a way that works in both
|
||||||
|
// MicroPython and Pyodide
|
||||||
|
for (const line of code.split(/(?:\r|\n|\r\n)/)) {
|
||||||
|
terminal.paste(`${line}\n`);
|
||||||
|
do {
|
||||||
|
await new Promise((resolve) =>
|
||||||
|
setTimeout(resolve, 0),
|
||||||
|
);
|
||||||
|
} while (!readline.activeRead?.resolve);
|
||||||
|
readline.activeRead.resolve(line);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
return terminal;
|
return terminal;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -158,7 +227,7 @@ const pyTerminal = async () => {
|
|||||||
} else {
|
} else {
|
||||||
// in the main case, just bootstrap XTerm without
|
// in the main case, just bootstrap XTerm without
|
||||||
// allowing any input as that's not possible / awkward
|
// allowing any input as that's not possible / awkward
|
||||||
hooks.main.onReady.add(function main({ interpreter, io, run }) {
|
hooks.main.onReady.add(function main({ interpreter, io, run, type }) {
|
||||||
console.warn("py-terminal is read only on main thread");
|
console.warn("py-terminal is read only on main thread");
|
||||||
hooks.main.onReady.delete(main);
|
hooks.main.onReady.delete(main);
|
||||||
|
|
||||||
@@ -171,13 +240,22 @@ const pyTerminal = async () => {
|
|||||||
run("from js import __py_terminal__ as __terminal__");
|
run("from js import __py_terminal__ as __terminal__");
|
||||||
delete globalThis.__py_terminal__;
|
delete globalThis.__py_terminal__;
|
||||||
|
|
||||||
// This part is inevitably duplicated as external scope
|
io.stderr = (error) => {
|
||||||
// can't be reached by workers out of the box.
|
readline.write(String(error.message || error));
|
||||||
// The detail is that here we use readline here, not sync.
|
};
|
||||||
const decoder = new TextDecoder();
|
|
||||||
|
if (type === "mpy") {
|
||||||
|
interpreter.setStdin = Object; // as no-op
|
||||||
|
interpreter.setStderr = Object; // as no-op
|
||||||
|
interpreter.setStdout = ({ write }) => {
|
||||||
|
io.stdout = write;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
let data = "";
|
let data = "";
|
||||||
|
const decoder = new TextDecoder();
|
||||||
const generic = {
|
const generic = {
|
||||||
isatty: true,
|
isatty: false,
|
||||||
write(buffer) {
|
write(buffer) {
|
||||||
data = decoder.decode(buffer);
|
data = decoder.decode(buffer);
|
||||||
readline.write(data);
|
readline.write(data);
|
||||||
@@ -187,20 +265,33 @@ const pyTerminal = async () => {
|
|||||||
interpreter.setStdout(generic);
|
interpreter.setStdout(generic);
|
||||||
interpreter.setStderr(generic);
|
interpreter.setStderr(generic);
|
||||||
interpreter.setStdin({
|
interpreter.setStdin({
|
||||||
isatty: true,
|
isatty: false,
|
||||||
stdin: () => readline.read(data),
|
stdin: () => readline.read(data),
|
||||||
});
|
});
|
||||||
|
|
||||||
io.stderr = (error) => {
|
|
||||||
readline.write(`${error.message || error}\n`);
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const mo = new MutationObserver(pyTerminal);
|
for (const key of TYPES.keys()) {
|
||||||
mo.observe(document, { childList: true, subtree: true });
|
const selector = `script[type="${key}"][terminal],${key}-script[terminal]`;
|
||||||
|
SELECTORS.push(selector);
|
||||||
|
customObserver.set(selector, async (element) => {
|
||||||
|
// we currently support only one terminal on main as in "classic"
|
||||||
|
const terminals = document.querySelectorAll(SELECTORS.join(","));
|
||||||
|
if ([].filter.call(terminals, onceOnMain).length > 1)
|
||||||
|
notifyAndThrow("You can use at most 1 main terminal");
|
||||||
|
|
||||||
// try to check the current document ASAP
|
// import styles lazily
|
||||||
export default pyTerminal();
|
if (addStyle) {
|
||||||
|
addStyle = false;
|
||||||
|
document.head.append(
|
||||||
|
Object.assign(document.createElement("link"), {
|
||||||
|
rel: "stylesheet",
|
||||||
|
href: new URL("./xterm.css", import.meta.url),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await pyTerminal(element);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ const write = (base, literal) => {
|
|||||||
python.push(`_path = _Path("${base}/${key}")`);
|
python.push(`_path = _Path("${base}/${key}")`);
|
||||||
if (typeof value === "string") {
|
if (typeof value === "string") {
|
||||||
const code = JSON.stringify(value);
|
const code = JSON.stringify(value);
|
||||||
python.push(`_path.write_text(${code})`);
|
python.push(`_path.write_text(${code},encoding="utf-8")`);
|
||||||
} else {
|
} else {
|
||||||
// @see https://github.com/pyscript/pyscript/pull/1813#issuecomment-1781502909
|
// @see https://github.com/pyscript/pyscript/pull/1813#issuecomment-1781502909
|
||||||
python.push(`if not _os.path.exists("${base}/${key}"):`);
|
python.push(`if not _os.path.exists("${base}/${key}"):`);
|
||||||
|
|||||||
@@ -30,6 +30,7 @@
|
|||||||
# as it works transparently in both the main thread and worker cases.
|
# as it works transparently in both the main thread and worker cases.
|
||||||
|
|
||||||
from pyscript.display import HTML, display
|
from pyscript.display import HTML, display
|
||||||
|
from pyscript.fetch import fetch
|
||||||
from pyscript.magic_js import (
|
from pyscript.magic_js import (
|
||||||
RUNNING_IN_WORKER,
|
RUNNING_IN_WORKER,
|
||||||
PyWorker,
|
PyWorker,
|
||||||
|
|||||||
94
pyscript.core/src/stdlib/pyscript/fetch.py
Normal file
94
pyscript.core/src/stdlib/pyscript/fetch.py
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
import js
|
||||||
|
|
||||||
|
|
||||||
|
### wrap the response to grant Pythonic results
|
||||||
|
class _Response:
|
||||||
|
def __init__(self, response):
|
||||||
|
self._response = response
|
||||||
|
|
||||||
|
# grant access to response.ok and other fields
|
||||||
|
def __getattr__(self, attr):
|
||||||
|
return getattr(self._response, attr)
|
||||||
|
|
||||||
|
def _as_bytearray(self, buffer):
|
||||||
|
ui8a = js.Uint8Array.new(buffer)
|
||||||
|
size = ui8a.length
|
||||||
|
ba = bytearray(size)
|
||||||
|
for i in range(0, size):
|
||||||
|
ba[i] = ui8a[i]
|
||||||
|
return ba
|
||||||
|
|
||||||
|
# exposed methods with Pythonic results
|
||||||
|
async def arrayBuffer(self):
|
||||||
|
buffer = await self._response.arrayBuffer()
|
||||||
|
# works in Pyodide
|
||||||
|
if hasattr(buffer, "to_py"):
|
||||||
|
return buffer.to_py()
|
||||||
|
# shims in MicroPython
|
||||||
|
return memoryview(self._as_bytearray(buffer))
|
||||||
|
|
||||||
|
async def blob(self):
|
||||||
|
return await self._response.blob()
|
||||||
|
|
||||||
|
async def bytearray(self):
|
||||||
|
buffer = await self._response.arrayBuffer()
|
||||||
|
return self._as_bytearray(buffer)
|
||||||
|
|
||||||
|
async def json(self):
|
||||||
|
return json.loads(await self.text())
|
||||||
|
|
||||||
|
async def text(self):
|
||||||
|
return await self._response.text()
|
||||||
|
|
||||||
|
|
||||||
|
### allow direct await to _Response methods
|
||||||
|
class _DirectResponse:
|
||||||
|
@staticmethod
|
||||||
|
def setup(promise, response):
|
||||||
|
promise._response = _Response(response)
|
||||||
|
return promise._response
|
||||||
|
|
||||||
|
def __init__(self, promise):
|
||||||
|
self._promise = promise
|
||||||
|
promise._response = None
|
||||||
|
promise.arrayBuffer = self.arrayBuffer
|
||||||
|
promise.blob = self.blob
|
||||||
|
promise.bytearray = self.bytearray
|
||||||
|
promise.json = self.json
|
||||||
|
promise.text = self.text
|
||||||
|
|
||||||
|
async def _response(self):
|
||||||
|
if not self._promise._response:
|
||||||
|
await self._promise
|
||||||
|
return self._promise._response
|
||||||
|
|
||||||
|
async def arrayBuffer(self):
|
||||||
|
response = await self._response()
|
||||||
|
return await response.arrayBuffer()
|
||||||
|
|
||||||
|
async def blob(self):
|
||||||
|
response = await self._response()
|
||||||
|
return await response.blob()
|
||||||
|
|
||||||
|
async def bytearray(self):
|
||||||
|
response = await self._response()
|
||||||
|
return await response.bytearray()
|
||||||
|
|
||||||
|
async def json(self):
|
||||||
|
response = await self._response()
|
||||||
|
return await response.json()
|
||||||
|
|
||||||
|
async def text(self):
|
||||||
|
response = await self._response()
|
||||||
|
return await response.text()
|
||||||
|
|
||||||
|
|
||||||
|
def fetch(url, **kw):
|
||||||
|
# workaround Pyodide / MicroPython dict <-> js conversion
|
||||||
|
options = js.JSON.parse(json.dumps(kw))
|
||||||
|
awaited = lambda response, *args: _DirectResponse.setup(promise, response)
|
||||||
|
promise = js.fetch(url, options).then(awaited)
|
||||||
|
_DirectResponse(promise)
|
||||||
|
return promise
|
||||||
18
pyscript.core/src/stdlib/pyscript/ffi.py
Normal file
18
pyscript.core/src/stdlib/pyscript/ffi.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
try:
|
||||||
|
import js
|
||||||
|
from pyodide.ffi import create_proxy as _cp
|
||||||
|
from pyodide.ffi import to_js as _py_tjs
|
||||||
|
|
||||||
|
from_entries = js.Object.fromEntries
|
||||||
|
|
||||||
|
def _tjs(value, **kw):
|
||||||
|
if not hasattr(kw, "dict_converter"):
|
||||||
|
kw["dict_converter"] = from_entries
|
||||||
|
return _py_tjs(value, **kw)
|
||||||
|
|
||||||
|
except:
|
||||||
|
from jsffi import create_proxy as _cp
|
||||||
|
from jsffi import to_js as _tjs
|
||||||
|
|
||||||
|
create_proxy = _cp
|
||||||
|
to_js = _tjs
|
||||||
@@ -24,16 +24,33 @@ for name in globalThis.Reflect.ownKeys(js_modules):
|
|||||||
sys.modules["pyscript.js_modules"] = js_modules
|
sys.modules["pyscript.js_modules"] = js_modules
|
||||||
|
|
||||||
if RUNNING_IN_WORKER:
|
if RUNNING_IN_WORKER:
|
||||||
import js
|
|
||||||
import polyscript
|
import polyscript
|
||||||
|
|
||||||
PyWorker = NotSupported(
|
PyWorker = NotSupported(
|
||||||
"pyscript.PyWorker",
|
"pyscript.PyWorker",
|
||||||
"pyscript.PyWorker works only when running in the main thread",
|
"pyscript.PyWorker works only when running in the main thread",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
globalThis.SharedArrayBuffer.new(4)
|
||||||
|
import js
|
||||||
|
|
||||||
window = polyscript.xworker.window
|
window = polyscript.xworker.window
|
||||||
document = window.document
|
document = window.document
|
||||||
js.document = document
|
js.document = document
|
||||||
|
except:
|
||||||
|
globalThis.console.debug("SharedArrayBuffer is not available")
|
||||||
|
# in this scenario none of the utilities would work
|
||||||
|
# as expected so we better export these as NotSupported
|
||||||
|
window = NotSupported(
|
||||||
|
"pyscript.window",
|
||||||
|
"pyscript.window in workers works only via SharedArrayBuffer",
|
||||||
|
)
|
||||||
|
document = NotSupported(
|
||||||
|
"pyscript.document",
|
||||||
|
"pyscript.document in workers works only via SharedArrayBuffer",
|
||||||
|
)
|
||||||
|
|
||||||
sync = polyscript.xworker.sync
|
sync = polyscript.xworker.sync
|
||||||
|
|
||||||
# in workers the display does not have a default ID
|
# in workers the display does not have a default ID
|
||||||
|
|||||||
@@ -125,6 +125,14 @@ class Element(BaseElement):
|
|||||||
def html(self, value):
|
def html(self, value):
|
||||||
self._js.innerHTML = value
|
self._js.innerHTML = value
|
||||||
|
|
||||||
|
@property
|
||||||
|
def text(self):
|
||||||
|
return self._js.textContent
|
||||||
|
|
||||||
|
@text.setter
|
||||||
|
def text(self, value):
|
||||||
|
self._js.textContent = value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def content(self):
|
def content(self):
|
||||||
# TODO: This breaks with with standard template elements. Define how to best
|
# TODO: This breaks with with standard template elements. Define how to best
|
||||||
|
|||||||
19
pyscript.core/test/code-a-part.html
Normal file
19
pyscript.core/test/code-a-part.html
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<link rel="stylesheet" href="../dist/core.css">
|
||||||
|
<script type="module">
|
||||||
|
import { hooks } from "../dist/core.js";
|
||||||
|
hooks.main.codeBeforeRun.add('print(0)');
|
||||||
|
hooks.main.codeAfterRun.add('print(2)');
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<script type="py">
|
||||||
|
# raise an error instead to see it on line 1
|
||||||
|
print(1)
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
95
pyscript.core/test/fetch.html
Normal file
95
pyscript.core/test/fetch.html
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<link rel="stylesheet" href="../dist/core.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<script type="module">
|
||||||
|
import fetch from 'https://esm.run/@webreflection/fetch';
|
||||||
|
|
||||||
|
globalThis.fetch_text = await fetch("config.json").text();
|
||||||
|
globalThis.fetch_json = JSON.stringify(await fetch("config.json").json());
|
||||||
|
globalThis.fetch_buffer = new Uint8Array((await fetch("config.json").arrayBuffer())).length;
|
||||||
|
|
||||||
|
document.head.appendChild(
|
||||||
|
Object.assign(
|
||||||
|
document.createElement('script'),
|
||||||
|
{
|
||||||
|
type: 'module',
|
||||||
|
src: '../dist/core.js'
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
</script>
|
||||||
|
<script type="mpy" async>
|
||||||
|
import js, json
|
||||||
|
from pyscript import document, fetch
|
||||||
|
|
||||||
|
fetch_text = await (await fetch("config.json")).text()
|
||||||
|
if (fetch_text != js.fetch_text):
|
||||||
|
raise Exception("fetch_text")
|
||||||
|
|
||||||
|
fetch_text = await fetch("config.json").text()
|
||||||
|
if (fetch_text != js.fetch_text):
|
||||||
|
raise Exception("fetch_text")
|
||||||
|
|
||||||
|
fetch_json = await (await fetch("config.json")).json()
|
||||||
|
if (json.dumps(fetch_json).replace(" ", "") != js.fetch_json):
|
||||||
|
raise Exception("fetch_json")
|
||||||
|
|
||||||
|
fetch_json = await fetch("config.json").json()
|
||||||
|
if (json.dumps(fetch_json).replace(" ", "") != js.fetch_json):
|
||||||
|
raise Exception("fetch_json")
|
||||||
|
|
||||||
|
fetch_buffer = await (await fetch("config.json")).arrayBuffer()
|
||||||
|
if (len(fetch_buffer) != js.fetch_buffer):
|
||||||
|
raise Exception("fetch_buffer")
|
||||||
|
|
||||||
|
fetch_buffer = await fetch("config.json").arrayBuffer()
|
||||||
|
if (len(fetch_buffer) != js.fetch_buffer):
|
||||||
|
raise Exception("fetch_buffer")
|
||||||
|
|
||||||
|
print(await (await fetch("config.json")).bytearray())
|
||||||
|
print(await (await fetch("config.json")).blob())
|
||||||
|
|
||||||
|
if (await fetch("shenanigans.nope")).ok == False:
|
||||||
|
document.documentElement.classList.add('mpy')
|
||||||
|
</script>
|
||||||
|
<script type="py" async>
|
||||||
|
import js, json
|
||||||
|
from pyscript import document, fetch
|
||||||
|
|
||||||
|
fetch_text = await (await fetch("config.json")).text()
|
||||||
|
if (fetch_text != js.fetch_text):
|
||||||
|
raise Exception("fetch_text")
|
||||||
|
|
||||||
|
fetch_text = await fetch("config.json").text()
|
||||||
|
if (fetch_text != js.fetch_text):
|
||||||
|
raise Exception("fetch_text")
|
||||||
|
|
||||||
|
fetch_json = await (await fetch("config.json")).json()
|
||||||
|
if (json.dumps(fetch_json).replace(" ", "") != js.fetch_json):
|
||||||
|
raise Exception("fetch_json")
|
||||||
|
|
||||||
|
fetch_json = await fetch("config.json").json()
|
||||||
|
if (json.dumps(fetch_json).replace(" ", "") != js.fetch_json):
|
||||||
|
raise Exception("fetch_json")
|
||||||
|
|
||||||
|
fetch_buffer = await (await fetch("config.json")).arrayBuffer()
|
||||||
|
if (len(fetch_buffer) != js.fetch_buffer):
|
||||||
|
raise Exception("fetch_buffer")
|
||||||
|
|
||||||
|
fetch_buffer = await fetch("config.json").arrayBuffer()
|
||||||
|
if (len(fetch_buffer) != js.fetch_buffer):
|
||||||
|
raise Exception("fetch_buffer")
|
||||||
|
|
||||||
|
print(await (await fetch("config.json")).bytearray())
|
||||||
|
print(await (await fetch("config.json")).blob())
|
||||||
|
|
||||||
|
if (await fetch("shenanigans.nope")).ok == False:
|
||||||
|
document.documentElement.classList.add('py')
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
26
pyscript.core/test/ffi.html
Normal file
26
pyscript.core/test/ffi.html
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>PyScript FFI</title>
|
||||||
|
<link rel="stylesheet" href="../dist/core.css">
|
||||||
|
<script type="module" src="../dist/core.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<script type="mpy">
|
||||||
|
from pyscript import document
|
||||||
|
from pyscript.ffi import to_js
|
||||||
|
document.documentElement.classList.add(
|
||||||
|
to_js({"ok": "mpy"}).ok
|
||||||
|
)
|
||||||
|
</script>
|
||||||
|
<script type="py">
|
||||||
|
from pyscript import document
|
||||||
|
from pyscript.ffi import to_js
|
||||||
|
document.documentElement.classList.add(
|
||||||
|
to_js({"ok": "py"}).ok
|
||||||
|
)
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -78,3 +78,13 @@ test('Pyodide + multiple terminals via Worker', async ({ page }) => {
|
|||||||
await page.goto('http://localhost:8080/test/py-terminals.html');
|
await page.goto('http://localhost:8080/test/py-terminals.html');
|
||||||
await page.waitForSelector('html.first.second');
|
await page.waitForSelector('html.first.second');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('MicroPython + Pyodide fetch', async ({ page }) => {
|
||||||
|
await page.goto('http://localhost:8080/test/fetch.html');
|
||||||
|
await page.waitForSelector('html.mpy.py');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('MicroPython + Pyodide ffi', async ({ page }) => {
|
||||||
|
await page.goto('http://localhost:8080/test/ffi.html');
|
||||||
|
await page.waitForSelector('html.mpy.py');
|
||||||
|
});
|
||||||
|
|||||||
23
pyscript.core/test/no_sab/index.html
Normal file
23
pyscript.core/test/no_sab/index.html
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<link rel="stylesheet" href="../../dist/core.css">
|
||||||
|
<script type="module">
|
||||||
|
import { PyWorker } from '../../dist/core.js';
|
||||||
|
const { sync } = await PyWorker(
|
||||||
|
'./worker.py',
|
||||||
|
{
|
||||||
|
config: {
|
||||||
|
sync_main_only: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
document.documentElement.classList.add(
|
||||||
|
await sync.get_class()
|
||||||
|
);
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
</html>
|
||||||
3
pyscript.core/test/no_sab/worker.py
Normal file
3
pyscript.core/test/no_sab/worker.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
from pyscript import sync
|
||||||
|
|
||||||
|
sync.get_class = lambda: "ok"
|
||||||
2
pyscript.core/test/py-editor/config.toml
Normal file
2
pyscript.core/test/py-editor/config.toml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
[js_modules.worker]
|
||||||
|
"https://cdn.jsdelivr.net/npm/html-escaper/+esm" = "html_escaper"
|
||||||
41
pyscript.core/test/py-editor/index.html
Normal file
41
pyscript.core/test/py-editor/index.html
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<link rel="stylesheet" href="../../dist/core.css">
|
||||||
|
<script type="module" src="../../dist/core.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!-- a setup node with a config for an env -->
|
||||||
|
<script type="mpy-editor" src="task1.py" config="./config.toml" env="task1" setup></script>
|
||||||
|
<script type="mpy-editor" env="task1">
|
||||||
|
from pyscript.js_modules.html_escaper import escape, unescape
|
||||||
|
print(unescape(escape("<OK>")))
|
||||||
|
a = 1
|
||||||
|
</script>
|
||||||
|
<!-- a share-nothing micropython editor -->
|
||||||
|
<script type="mpy-editor" config="./config.toml">
|
||||||
|
from pyscript.js_modules.html_escaper import escape, unescape
|
||||||
|
print(unescape(escape("<OK>")))
|
||||||
|
b = 2
|
||||||
|
try:
|
||||||
|
print(a)
|
||||||
|
except:
|
||||||
|
print("all good")
|
||||||
|
</script>
|
||||||
|
<!-- a config once micropython env -->
|
||||||
|
<script type="mpy-editor" env="task2" config="./config.toml">
|
||||||
|
from pyscript.js_modules.html_escaper import escape, unescape
|
||||||
|
print(unescape(escape("<OK>")))
|
||||||
|
c = 3
|
||||||
|
try:
|
||||||
|
print(b)
|
||||||
|
except:
|
||||||
|
print("all good")
|
||||||
|
</script>
|
||||||
|
<script type="mpy-editor" env="task2">
|
||||||
|
print(c)
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
5
pyscript.core/test/py-editor/task1.py
Normal file
5
pyscript.core/test/py-editor/task1.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
from pyscript import window
|
||||||
|
|
||||||
|
window.console.log("OK")
|
||||||
|
|
||||||
|
a = 1
|
||||||
@@ -9,24 +9,10 @@
|
|||||||
<style>.xterm { padding: .5rem; }</style>
|
<style>.xterm { padding: .5rem; }</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<script type="py">
|
<script type="mpy" worker terminal>
|
||||||
def greetings(event):
|
print("µpython")
|
||||||
print('hello world')
|
import code
|
||||||
|
code.interact()
|
||||||
</script>
|
</script>
|
||||||
<py-script worker terminal>
|
|
||||||
# works on both worker and main scripts
|
|
||||||
print("__terminal__", __terminal__)
|
|
||||||
|
|
||||||
import sys
|
|
||||||
from pyscript import display, document
|
|
||||||
display("Hello", "PyScript Next - PyTerminal", append=False)
|
|
||||||
print("this should go to the terminal")
|
|
||||||
print("another line")
|
|
||||||
|
|
||||||
# this works as expected
|
|
||||||
print("this goes to stderr", file=sys.stderr)
|
|
||||||
document.addEventListener('click', lambda event: print(event.type));
|
|
||||||
</py-script>
|
|
||||||
<button id="my-button" py-click="greetings">Click me</button>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -98,6 +98,8 @@
|
|||||||
<p class="collection"></p>
|
<p class="collection"></p>
|
||||||
<div class="collection"></div>
|
<div class="collection"></div>
|
||||||
<h3 class="collection"></h3>
|
<h3 class="collection"></h3>
|
||||||
|
|
||||||
|
<div id="element_attribute_tests"></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -163,6 +163,30 @@ class TestElement:
|
|||||||
|
|
||||||
assert called
|
assert called
|
||||||
|
|
||||||
|
def test_html_attribute(self):
|
||||||
|
# GIVEN an existing element on the page with a known empty text content
|
||||||
|
div = pydom["#element_attribute_tests"][0]
|
||||||
|
|
||||||
|
# WHEN we set the html attribute
|
||||||
|
div.html = "<b>New Content</b>"
|
||||||
|
|
||||||
|
# EXPECT the element html and underlying JS Element innerHTML property
|
||||||
|
# to match what we expect and what
|
||||||
|
assert div.html == div._js.innerHTML == "<b>New Content</b>"
|
||||||
|
assert div.text == div._js.textContent == "New Content"
|
||||||
|
|
||||||
|
def test_text_attribute(self):
|
||||||
|
# GIVEN an existing element on the page with a known empty text content
|
||||||
|
div = pydom["#element_attribute_tests"][0]
|
||||||
|
|
||||||
|
# WHEN we set the html attribute
|
||||||
|
div.text = "<b>New Content</b>"
|
||||||
|
|
||||||
|
# EXPECT the element html and underlying JS Element innerHTML property
|
||||||
|
# to match what we expect and what
|
||||||
|
assert div.html == div._js.innerHTML == "<b>New Content</b>"
|
||||||
|
assert div.text == div._js.textContent == "<b>New Content</b>"
|
||||||
|
|
||||||
|
|
||||||
class TestCollection:
|
class TestCollection:
|
||||||
def test_iter_eq_children(self):
|
def test_iter_eq_children(self):
|
||||||
|
|||||||
0
pyscript.core/test/test.html
Normal file
0
pyscript.core/test/test.html
Normal file
4
pyscript.core/types/3rd-party/xterm_addon-web-links.d.ts
vendored
Normal file
4
pyscript.core/types/3rd-party/xterm_addon-web-links.d.ts
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
declare var r: any;
|
||||||
|
declare var n: any;
|
||||||
|
declare var t: {};
|
||||||
|
export { r as WebLinksAddon, n as __esModule, t as default };
|
||||||
24
pyscript.core/types/core.d.ts
vendored
24
pyscript.core/types/core.d.ts
vendored
@@ -1,16 +1,29 @@
|
|||||||
|
import stdlib from "./stdlib.js";
|
||||||
import TYPES from "./types.js";
|
import TYPES from "./types.js";
|
||||||
/**
|
/**
|
||||||
* A `Worker` facade able to bootstrap on the worker thread only a PyScript module.
|
* A `Worker` facade able to bootstrap on the worker thread only a PyScript module.
|
||||||
* @param {string} file the python file to run ina worker.
|
* @param {string} file the python file to run ina worker.
|
||||||
* @param {{config?: string | object, async?: boolean}} [options] optional configuration for the worker.
|
* @param {{config?: string | object, async?: boolean}} [options] optional configuration for the worker.
|
||||||
* @returns {Worker & {sync: ProxyHandler<object>}}
|
* @returns {Promise<Worker & {sync: object}>}
|
||||||
*/
|
*/
|
||||||
declare function exportedPyWorker(file: string, options?: {
|
declare function exportedPyWorker(file: string, options?: {
|
||||||
config?: string | object;
|
config?: string | object;
|
||||||
async?: boolean;
|
async?: boolean;
|
||||||
}): Worker & {
|
}): Promise<Worker & {
|
||||||
sync: ProxyHandler<object>;
|
sync: object;
|
||||||
};
|
}>;
|
||||||
|
/**
|
||||||
|
* A `Worker` facade able to bootstrap on the worker thread only a PyScript module.
|
||||||
|
* @param {string} file the python file to run ina worker.
|
||||||
|
* @param {{config?: string | object, async?: boolean}} [options] optional configuration for the worker.
|
||||||
|
* @returns {Promise<Worker & {sync: object}>}
|
||||||
|
*/
|
||||||
|
declare function exportedMPWorker(file: string, options?: {
|
||||||
|
config?: string | object;
|
||||||
|
async?: boolean;
|
||||||
|
}): Promise<Worker & {
|
||||||
|
sync: object;
|
||||||
|
}>;
|
||||||
declare const exportedHooks: {
|
declare const exportedHooks: {
|
||||||
main: {
|
main: {
|
||||||
onWorker: Set<Function>;
|
onWorker: Set<Function>;
|
||||||
@@ -38,5 +51,4 @@ declare const exportedHooks: {
|
|||||||
};
|
};
|
||||||
declare const exportedConfig: {};
|
declare const exportedConfig: {};
|
||||||
declare const exportedWhenDefined: (type: string) => Promise<any>;
|
declare const exportedWhenDefined: (type: string) => Promise<any>;
|
||||||
import sync from "./sync.js";
|
export { stdlib, TYPES, exportedPyWorker as PyWorker, exportedMPWorker as MPWorker, exportedHooks as hooks, exportedConfig as config, exportedWhenDefined as whenDefined };
|
||||||
export { TYPES, exportedPyWorker as PyWorker, exportedHooks as hooks, exportedConfig as config, exportedWhenDefined as whenDefined };
|
|
||||||
|
|||||||
3
pyscript.core/types/fetch.d.ts
vendored
3
pyscript.core/types/fetch.d.ts
vendored
@@ -8,5 +8,4 @@
|
|||||||
* @returns {Promise<Response>}
|
* @returns {Promise<Response>}
|
||||||
*/
|
*/
|
||||||
export function robustFetch(url: string, options?: Request): Promise<Response>;
|
export function robustFetch(url: string, options?: Request): Promise<Response>;
|
||||||
export { getText };
|
export function getText(response: Response): Promise<string>;
|
||||||
import { getText } from "polyscript/exports";
|
|
||||||
|
|||||||
3
pyscript.core/types/plugins/py-terminal.d.ts
vendored
3
pyscript.core/types/plugins/py-terminal.d.ts
vendored
@@ -1,2 +1 @@
|
|||||||
declare const _default: Promise<void>;
|
export {};
|
||||||
export default _default;
|
|
||||||
|
|||||||
2
pyscript.core/types/stdlib/pyscript.d.ts
vendored
2
pyscript.core/types/stdlib/pyscript.d.ts
vendored
@@ -3,6 +3,8 @@ declare namespace _default {
|
|||||||
"__init__.py": string;
|
"__init__.py": string;
|
||||||
"display.py": string;
|
"display.py": string;
|
||||||
"event_handling.py": string;
|
"event_handling.py": string;
|
||||||
|
"fetch.py": string;
|
||||||
|
"ffi.py": string;
|
||||||
"magic_js.py": string;
|
"magic_js.py": string;
|
||||||
"util.py": string;
|
"util.py": string;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user