mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-23 22:00:25 -05:00
Compare commits
36 Commits
reverted-4
...
pygame-ce
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c0fcfa7a07 | ||
|
|
d143b229ed | ||
|
|
0d74a60227 | ||
|
|
ce923a354f | ||
|
|
7e65836423 | ||
|
|
796373cfa6 | ||
|
|
0a1d3bb678 | ||
|
|
4e43d3e92d | ||
|
|
5acc2afaf3 | ||
|
|
56c64cbee7 | ||
|
|
4ff02a24d1 | ||
|
|
a5dc94792b | ||
|
|
0db79e0f02 | ||
|
|
283eabdb30 | ||
|
|
3f19e00410 | ||
|
|
9233d5e45a | ||
|
|
fe580cd90b | ||
|
|
00e6cfed29 | ||
|
|
6b1330d28a | ||
|
|
5d751493f6 | ||
|
|
c3517f7973 | ||
|
|
b1c33b7f79 | ||
|
|
722abda895 | ||
|
|
8061bc0143 | ||
|
|
993e812e7b | ||
|
|
a6b6dd8479 | ||
|
|
febbb031ac | ||
|
|
f827efe2fc | ||
|
|
8c6bfecbff | ||
|
|
f562860e49 | ||
|
|
62c78b0f42 | ||
|
|
4fe6ad80af | ||
|
|
89a0a556f3 | ||
|
|
af06bc4826 | ||
|
|
6fab9a1c26 | ||
|
|
386966ebd2 |
8
.github/PULL_REQUEST_TEMPLATE.md
vendored
8
.github/PULL_REQUEST_TEMPLATE.md
vendored
@@ -4,11 +4,9 @@
|
||||
|
||||
## Changes
|
||||
|
||||
<!-- List the changes done to fix a bug or introduce a new feature.Please note both user-facing changes and changes to internal API's here -->
|
||||
<!-- List the technical changes done to fix a bug or introduce a new feature. -->
|
||||
|
||||
## Checklist
|
||||
|
||||
<!-- Note: Only user-facing changes require a changelog entry. Internal-only API changes do not require a changelog entry. Changes in documentation do not require a changelog entry. -->
|
||||
|
||||
- [ ] All tests pass locally
|
||||
- [ ] I have created / updated documentation for this (if applicable)
|
||||
- [ ] I have checked `make build` works locally.
|
||||
- [ ] I have created / updated documentation for this change (if applicable).
|
||||
|
||||
2
.github/workflows/prepare-release.yml
vendored
2
.github/workflows/prepare-release.yml
vendored
@@ -11,7 +11,7 @@ defaults:
|
||||
|
||||
jobs:
|
||||
prepare-release:
|
||||
runs-on: ubuntu-latest-4core
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
2
.github/workflows/publish-release.yml
vendored
2
.github/workflows/publish-release.yml
vendored
@@ -10,7 +10,7 @@ defaults:
|
||||
|
||||
jobs:
|
||||
publish-release:
|
||||
runs-on: ubuntu-latest-4core
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
id-token: write
|
||||
contents: read
|
||||
|
||||
2
.github/workflows/publish-snapshot.yml
vendored
2
.github/workflows/publish-snapshot.yml
vendored
@@ -14,7 +14,7 @@ defaults:
|
||||
|
||||
jobs:
|
||||
publish-snapshot:
|
||||
runs-on: ubuntu-latest-4core
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
id-token: write
|
||||
|
||||
2
.github/workflows/publish-unstable.yml
vendored
2
.github/workflows/publish-unstable.yml
vendored
@@ -12,7 +12,7 @@ on:
|
||||
|
||||
jobs:
|
||||
publish-unstable:
|
||||
runs-on: ubuntu-latest-4core
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
id-token: write
|
||||
contents: read
|
||||
|
||||
2
.github/workflows/test.yml
vendored
2
.github/workflows/test.yml
vendored
@@ -18,7 +18,7 @@ on:
|
||||
|
||||
jobs:
|
||||
BuildAndTest:
|
||||
runs-on: ubuntu-latest-4core
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
MINICONDA_PYTHON_VERSION: py38
|
||||
MINICONDA_VERSION: 4.11.0
|
||||
|
||||
@@ -4,10 +4,10 @@ ci:
|
||||
#skip: [eslint]
|
||||
autoupdate_schedule: monthly
|
||||
|
||||
default_stages: [commit]
|
||||
default_stages: [pre-commit]
|
||||
repos:
|
||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
rev: v4.6.0
|
||||
rev: v5.0.0
|
||||
hooks:
|
||||
- id: check-builtin-literals
|
||||
- id: check-case-conflict
|
||||
@@ -25,10 +25,10 @@ repos:
|
||||
- id: trailing-whitespace
|
||||
|
||||
- repo: https://github.com/psf/black
|
||||
rev: 24.8.0
|
||||
rev: 24.10.0
|
||||
hooks:
|
||||
- id: black
|
||||
exclude: core/src/stdlib/pyscript/__init__\.py
|
||||
args: ["-l", "88", "--skip-string-normalization"]
|
||||
|
||||
- repo: https://github.com/codespell-project/codespell
|
||||
rev: v2.3.0
|
||||
@@ -44,10 +44,3 @@ repos:
|
||||
- id: prettier
|
||||
exclude: core/test|core/dist|core/types|core/src/stdlib/pyscript.js|pyscript\.sw/|core/src/3rd-party
|
||||
args: [--tab-width, "4"]
|
||||
|
||||
- repo: https://github.com/pycqa/isort
|
||||
rev: 5.13.2
|
||||
hooks:
|
||||
- id: isort
|
||||
name: isort (python)
|
||||
args: [--profile, black]
|
||||
|
||||
181
CONTRIBUTING.md
181
CONTRIBUTING.md
@@ -1,181 +1,4 @@
|
||||
# Contributing to PyScript
|
||||
|
||||
Thank you for wanting to contribute to the PyScript project!
|
||||
|
||||
## Table of contents
|
||||
|
||||
- [Contributing to PyScript](#contributing-to-pyscript)
|
||||
- [Table of contents](#table-of-contents)
|
||||
- [Code of Conduct](#code-of-conduct)
|
||||
- [Contributing](#contributing)
|
||||
- [Reporting bugs](#reporting-bugs)
|
||||
- [Creating useful issues](#creating-useful-issues)
|
||||
- [Reporting security issues](#reporting-security-issues)
|
||||
- [Asking questions](#asking-questions)
|
||||
- [Setting up your local environment and developing](#setting-up-your-local-environment-and-developing)
|
||||
- [Developing](#developing)
|
||||
- [Rebasing changes](#rebasing-changes)
|
||||
- [Building the docs](#building-the-docs)
|
||||
- [Places to start](#places-to-start)
|
||||
- [Setting up your local environment and developing](#setting-up-your-local-environment-and-developing)
|
||||
- [Submitting a change](#submitting-a-change)
|
||||
- [License terms for contributions](#license-terms-for-contributions)
|
||||
- [Becoming a maintainer](#becoming-a-maintainer)
|
||||
- [Trademarks](#trademarks)
|
||||
|
||||
# Code of Conduct
|
||||
|
||||
The [PyScript Code of Conduct](https://github.com/pyscript/governance/blob/main/CODE-OF-CONDUCT.md) governs the project and everyone participating in it. By participating, you are expected to uphold this code. Please report unacceptable behavior to the maintainers or administrators as described in that document.
|
||||
|
||||
# Contributing
|
||||
|
||||
## Reporting bugs
|
||||
|
||||
Bugs are tracked on the [project issues page](https://github.com/pyscript/pyscript/issues). Please check if your issue has already been filed by someone else by searching the existing issues before filing a new one. Once your issue is filed, it will be triaged by another contributor or maintainer. If there are questions raised about your issue, please respond promptly.
|
||||
|
||||
## Creating useful issues
|
||||
|
||||
- Use a clear and descriptive title.
|
||||
- Describe the specific steps that reproduce the problem with as many details as possible so that someone can verify the issue.
|
||||
- Describe the behavior you observed, and the behavior you had expected.
|
||||
- Include screenshots if they help make the issue clear.
|
||||
|
||||
## Reporting security issues
|
||||
|
||||
If you aren't confident that it is appropriate to submit a security issue using the above process, you can e-mail it to security@pyscript.net
|
||||
|
||||
## Asking questions
|
||||
|
||||
If you have questions about the project, using PyScript, or anything else, please ask in the [PyScript forum](https://community.anaconda.cloud/c/tech-topics/pyscript).
|
||||
|
||||
## Places to start
|
||||
|
||||
If you would like to contribute to PyScript, but you aren't sure where to begin, here are some suggestions:
|
||||
|
||||
- **Read over the existing documentation.** Are there things missing, or could they be clearer? Make some changes/additions to those documents.
|
||||
- **Review the open issues.** Are they clear? Can you reproduce them? You can add comments, clarifications, or additions to those issues. If you think you have an idea of how to address the issue, submit a fix!
|
||||
- **Look over the open pull requests.** Do you have comments or suggestions for the proposed changes? Add them.
|
||||
- **Check out the examples.** Is there a use case that would be good to have sample code for? Create an example for it.
|
||||
|
||||
## Setting up your local environment and developing
|
||||
|
||||
If you would like to contribute to PyScript, you will need to set up a local development environment. The [following instructions](https://docs.pyscript.net/latest/contributing/#set-up-your-development-environment) will help you get started.
|
||||
|
||||
You can also read about PyScript's [development process](https://docs.pyscript.net/latest/developers/) to learn how to contribute code to PyScript, how to run tests and what's the PR etiquette of the community!
|
||||
|
||||
## License terms for contributions
|
||||
|
||||
This Project welcomes contributions, suggestions, and feedback. All contributions, suggestions, and feedback you submitted are accepted under the [Apache 2.0](./LICENSE) license. You represent that if you do not own copyright in the code that you have the authority to submit it under the [Apache 2.0](./LICENSE) license. All feedback, suggestions, or contributions are not confidential.
|
||||
|
||||
## Becoming a maintainer
|
||||
|
||||
Contributors are invited to be maintainers of the project by demonstrating good decision making in their contributions, a commitment to the goals of the project, and consistent adherence to the [code of conduct](https://github.com/pyscript/governance/blob/main/CODE-OF-CONDUCT.md). New maintainers are invited by a 3/4 vote of the existing maintainers.
|
||||
|
||||
## Trademarks
|
||||
|
||||
The Project abides by the Organization's [trademark policy](https://github.com/pyscript/governance/blob/main/TRADEMARKS.md).
|
||||
|
||||
---
|
||||
|
||||
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/).
|
||||
|
||||
# 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.
|
||||
Please see our guide to contributing to PyScript
|
||||
[in our documentation](https://docs.pyscript.net/latest/contributing/).
|
||||
|
||||
14
Makefile
14
Makefile
@@ -9,6 +9,7 @@ PY_OK := $(shell python3 -c "print(int($(PY3_VER) >= $(MIN_PY3_VER)))")
|
||||
all:
|
||||
@echo "\nThere is no default Makefile target right now. Try:\n"
|
||||
@echo "make setup - check your environment and install the dependencies."
|
||||
@echo "make update - update dependencies."
|
||||
@echo "make clean - clean up auto-generated assets."
|
||||
@echo "make build - build PyScript."
|
||||
@echo "make precommit-check - run the precommit checks (run eslint)."
|
||||
@@ -39,7 +40,7 @@ check-python:
|
||||
|
||||
# Check the environment, install the dependencies.
|
||||
setup: check-node check-npm check-python
|
||||
cd core && npm install && cd ..
|
||||
cd core && npm ci && cd ..
|
||||
ifeq ($(VIRTUAL_ENV),)
|
||||
echo "\n\n\033[0;31mCannot install Python dependencies. Your virtualenv is not activated.\033[0m"
|
||||
false
|
||||
@@ -54,9 +55,13 @@ clean:
|
||||
rm -rf .pytest_cache .coverage coverage.xml
|
||||
|
||||
# Build PyScript.
|
||||
build:
|
||||
build: precommit-check
|
||||
cd core && npx playwright install chromium && npm run build
|
||||
|
||||
# Update the dependencies.
|
||||
update:
|
||||
python -m pip install -r requirements.txt --upgrade
|
||||
|
||||
# Run the precommit checks (run eslint).
|
||||
precommit-check:
|
||||
pre-commit run --all-files
|
||||
@@ -65,6 +70,10 @@ precommit-check:
|
||||
test:
|
||||
cd core && npm run test:integration
|
||||
|
||||
# Serve the repository with the correct headers.
|
||||
serve:
|
||||
npx mini-coi .
|
||||
|
||||
# Format the code.
|
||||
fmt: fmt-py
|
||||
@echo "Format completed"
|
||||
@@ -76,7 +85,6 @@ fmt-check: fmt-py-check
|
||||
# Format Python code.
|
||||
fmt-py:
|
||||
black -l 88 --skip-string-normalization .
|
||||
isort --profile black .
|
||||
|
||||
# Check the format of Python code.
|
||||
fmt-py-check:
|
||||
|
||||
136
README.md
136
README.md
@@ -1,97 +1,85 @@
|
||||
# PyScript
|
||||
|
||||
## What is PyScript
|
||||
## PyScript is an open source platform for Python in the browser.
|
||||
|
||||
### Summary
|
||||
Using PyScript is as simple as:
|
||||
|
||||
PyScript is a framework that allows users to create rich Python applications in the browser using HTML's interface and the power of [Pyodide](https://pyodide.org/en/stable/), [MicroPython](https://micropython.org/) and [WASM](https://webassembly.org/), and modern web technologies.
|
||||
```html
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
||||
<title>PyScript!</title>
|
||||
<link
|
||||
rel="stylesheet"
|
||||
href="https://pyscript.net/snapshots/2024.9.2/core.css"
|
||||
/>
|
||||
<script
|
||||
type="module"
|
||||
src="https://pyscript.net/snapshots/2024.9.2/core.js"
|
||||
></script>
|
||||
</head>
|
||||
<body>
|
||||
<!-- Use MicroPython to evaluate some Python -->
|
||||
<script type="mpy" terminal>
|
||||
print("Hello, world!")
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
To get started see the [Beginning PyScript tutorial](https://docs.pyscript.net/latest/beginning-pyscript/).
|
||||
PyScript enables the creation of rich Python applications in the browser using
|
||||
[Pyodide](https://pyodide.org/en/stable/) (a version of
|
||||
[CPython](https://python.org/)), [MicroPython](https://micropython.org/),
|
||||
[WASM](https://webassembly.org/), and modern web technologies. It means Python
|
||||
now runs anywhere a browser runs: desktop, laptop, mobile, tablet, or any other
|
||||
browser enabled device.
|
||||
|
||||
For examples see [here](https://pyscript.com/@examples).
|
||||
To start building, read the
|
||||
[Beginning PyScript tutorial](https://docs.pyscript.net/latest/beginning-pyscript/).
|
||||
|
||||
For example applications, see [here](https://pyscript.com/@examples).
|
||||
|
||||
Other useful resources:
|
||||
|
||||
- Our [Home Page](https://pyscript.net/) as an open source project.
|
||||
- The [official technical docs](https://docs.pyscript.net/).
|
||||
- Our current [Home Page](https://pyscript.net/) on the web.
|
||||
- A free-to-use [online editor](https://pyscript.com/) for trying PyScript.
|
||||
- Our community [Discord Channel](https://discord.gg/BYB2kvyFwm), to keep in touch .
|
||||
- A [YouTube channel](https://www.youtube.com/@PyScriptTV) with helpful videos
|
||||
and community content.
|
||||
- A free-to-use [online IDE](https://pyscript.com/) for trying PyScript.
|
||||
- Our community [Discord Channel](https://discord.gg/BYB2kvyFwm), to keep in
|
||||
touch .
|
||||
|
||||
Every Tuesday at 15:30 UTC there is the _PyScript Community Call_ on zoom, where we can talk about PyScript development in the open. Most of the maintainers regularly participate in the call, and everybody is welcome to join.
|
||||
Every Tuesday at 15:30 UTC there is the _PyScript Community Call_ on zoom,
|
||||
where we can talk about PyScript development in the open. Most of the
|
||||
maintainers regularly participate in the call, and everybody is welcome to
|
||||
join. This meeting is recorded and uploaded to our YouTube channel.
|
||||
|
||||
Every other Thursday at 16:00 UTC there is the _PyScript FUN_ call: this is a call in which everybody is encouraged to show what they did with PyScript.
|
||||
Every other Thursday at 16:00 UTC there is the _PyScript FUN_ call: the focus
|
||||
of this call is to share fun projects, goofy hacks or clever uses of PyScript.
|
||||
It's a supportive, energetic and entertaining meeting. This meeting is also
|
||||
recorded and uploaded to our YouTube channel.
|
||||
|
||||
For more details on how to join the calls and up to date schedule, consult the official calendar:
|
||||
For more details on how to join the calls and up to date schedule, consult the
|
||||
official calendar:
|
||||
|
||||
- [Google calendar](https://calendar.google.com/calendar/u/0/embed?src=d3afdd81f9c132a8c8f3290f5cc5966adebdf61017fca784eef0f6be9fd519e0@group.calendar.google.com&ctz=UTC) in UTC time;
|
||||
- [iCal format](https://calendar.google.com/calendar/ical/d3afdd81f9c132a8c8f3290f5cc5966adebdf61017fca784eef0f6be9fd519e0%40group.calendar.google.com/public/basic.ics).
|
||||
|
||||
### Longer Version
|
||||
## Contribute
|
||||
|
||||
PyScript is a meta project that aims to combine multiple open technologies into a framework that allows users to create sophisticated browser applications with Python. It integrates seamlessly with the way the DOM works in the browser and allows users to add Python logic in a way that feels natural both to web and Python developers.
|
||||
For technical details of the code, please see the [README](core/README.md) in
|
||||
the `core` directory.
|
||||
|
||||
## Try PyScript
|
||||
Read the [contributing guide](https://docs.pyscript.net/latest/contributing/)
|
||||
to learn about our development process, reporting bugs and improvements,
|
||||
creating issues and asking questions.
|
||||
|
||||
To try PyScript, import the appropriate pyscript files into the `<head>` tag of your html page:
|
||||
|
||||
```html
|
||||
<head>
|
||||
<link
|
||||
rel="stylesheet"
|
||||
href="https://pyscript.net/releases/2024.8.2/core.css"
|
||||
/>
|
||||
<script
|
||||
type="module"
|
||||
src="https://pyscript.net/releases/2024.8.2/core.js"
|
||||
></script>
|
||||
</head>
|
||||
<body>
|
||||
<script type="py" terminal>
|
||||
from pyscript import display
|
||||
display("Hello World!") # this goes to the DOM
|
||||
print("Hello terminal") # this goes to the terminal
|
||||
</script>
|
||||
</body>
|
||||
```
|
||||
|
||||
You can then use PyScript components in your html page. PyScript currently offers various ways of running Python code:
|
||||
|
||||
- `<script type="py">`: can be used to define python code that is executable within the web page.
|
||||
- `<script type="py" src="hello.py">`: same as above, but the python source is fetched from the given URL.
|
||||
- `<script type="py" terminal>`: same as above, but also creates a terminal where to display stdout and stderr (e.g., the output of `print()`); `input()` does not work.
|
||||
- `<script type="py" terminal worker>`: run Python inside a web worker: the terminal is fully functional and `input()` works.
|
||||
- `<py-script>`: same as `<script type="py">`, but it is not recommended because if the code contains HTML tags, they could be parsed wrongly.
|
||||
- `<script type="mpy">`: same as above but use MicroPython instead of Python.
|
||||
|
||||
Check out the [official docs](https://docs.pyscript.net/) for more detailed documentation.
|
||||
|
||||
## How to Contribute
|
||||
|
||||
Read the [contributing guide](https://docs.pyscript.net/latest/contributing/) to learn about our development process, reporting bugs and improvements, creating issues and asking questions.
|
||||
|
||||
Check out the [developing process](https://docs.pyscript.net/latest/developers/) documentation for more information on how to setup your development environment.
|
||||
|
||||
For technical details of the code, please see the [README](pyscript.core/README) in `pyscript.core`.
|
||||
Check out the [development process](https://docs.pyscript.net/latest/developers/)
|
||||
documentation for more information on how to setup your development environment.
|
||||
|
||||
## Governance
|
||||
|
||||
The [PyScript organization governance](https://github.com/pyscript/governance) is documented in a separate repository.
|
||||
|
||||
## Release
|
||||
|
||||
To cut a new release of PyScript simply
|
||||
[add a new release](https://github.com/pyscript/pyscript/releases) while
|
||||
remembering to write a comprehensive changelog. A [GitHub action](https://github.com/pyscript/pyscript/blob/main/.github/workflows/publish-release.yml)
|
||||
will kick in and ensure the release is described and deployed to a URL with the
|
||||
pattern: https://pyscript.net/releases/YYYY.M.v/ (year/month/version - as per
|
||||
our [CalVer](https://calver.org/) versioning scheme).
|
||||
|
||||
Then, the following three separate repositories need updating:
|
||||
|
||||
- [Documentation](https://github.com/pyscript/docs) - Change the `version.json`
|
||||
file in the root of the directory and then `node version-update.js`.
|
||||
- [Homepage](https://github.com/pyscript/pyscript.net) - Ensure the version
|
||||
referenced in `index.html` is the latest version.
|
||||
- [PSDC](https://pyscript.com) - Use discord or Anaconda Slack (if you work at
|
||||
Anaconda) to let the PSDC team know there's a new version, so they can update
|
||||
their project templates.
|
||||
The [PyScript organization governance](https://github.com/pyscript/governance)
|
||||
is documented in a separate repository.
|
||||
|
||||
193
core/README.md
193
core/README.md
@@ -1,83 +1,168 @@
|
||||
# @pyscript/core
|
||||
|
||||
We have moved and renamed previous _core_ module as [polyscript](https://github.com/pyscript/polyscript/#readme), which is the base module used in here to build up _PyScript Next_, now hosted in this folder.
|
||||
PyScript brings two Python interpreters to the browser:
|
||||
|
||||
## Documentation
|
||||
- [MicroPython](https://micropython.org/) - a lean and efficient implementation
|
||||
of the Python 3 programming language that includes a small subset of the
|
||||
Python standard library and is optimised to run on microcontrollers and in
|
||||
constrained environments (like the browser).
|
||||
- [Pyodide](https://pyodide.org)) - a port of all CPython to WebAssembly.
|
||||
|
||||
Please read [core documentation](https://docs.pyscript.net/) to know more about this project.
|
||||
These interpreters are compiled to [WebAssembly](https://webassembly.org/)
|
||||
(shortened to WASM). The browser provides a secure WASM computing sandbox. Both
|
||||
interpreters are compiled to web assembly with
|
||||
[Emscripten](https://emscripten.org/). PyScript core maintainers work closely
|
||||
with the core maintainers of both MicroPython and Pyodide (and CPython). We
|
||||
work hard to ensure PyScript works efficiently in browsers on all platforms:
|
||||
desktop, mobile, or elsewhere.
|
||||
|
||||
## Development
|
||||
Our technical documentation for using this project can be
|
||||
[found here](https://docs.pyscript.net/).
|
||||
|
||||
Clone this repository then run `npm install` within this folder.
|
||||
PyScript sits on two further projects (both written in JavaScript):
|
||||
|
||||
Use `npm run build` to create all artifacts and _dist_ files.
|
||||
1. [polyscript](https://github.com/pyscript/polyscript/#readme) - used to
|
||||
bootstrap WASM compiled interpreters in a browser.
|
||||
2. [coincident](https://github.com/WebReflection/coincident) - used to simplify
|
||||
worker based tasks.
|
||||
|
||||
Use `npm run server` to test locally, via the `http://localhost:8080/tests/` url, smoke tests or to test manually anything you'd like to check.
|
||||
PyScript itself is mostly written in JavaScript. The test suite for JavaScript
|
||||
is in two parts: automated tests run in [playwright](https://playwright.dev/),
|
||||
and manual tests you have to run in a browser and check yourself. PyScript also
|
||||
has a plugin system so third parties can extend its capabilities with
|
||||
JavaScript. Our built-in core plugins can be found in the `src/plugins`
|
||||
directory. We describe how to write third party plugins in our
|
||||
[developer documentation](https://docs.pyscript.net/latest/user-guide/plugins/).
|
||||
|
||||
### Artifacts
|
||||
We provide a `pyscript` namespace containing Python modules for common browser
|
||||
based APIs and features (i.e. you can `import pyscript` in Python code running
|
||||
inside PyScript, to access these features). The Python code for the `pyscript`
|
||||
namespace is in `src/stdlib/pyscript` with the associated test suite in
|
||||
`tests/python`. The tests use the browser friendly
|
||||
[uPyTest](https://github.com/ntoll/upytest) test framework for checking Python
|
||||
code running _within_ PyScript. All the Python tests are run in each each
|
||||
available interpreter in both the main thread and a web worker (i.e. the
|
||||
test suite is run four times, accounting for each combination of interpreter
|
||||
and main/worker context).
|
||||
|
||||
When you create a local build all the automated tests (JavaScript and Python)
|
||||
are run.
|
||||
|
||||
## Developer Guide
|
||||
|
||||
Full instructions for setting up a working development environment, how to
|
||||
build PyScript and how to test it can be
|
||||
[found in our official docs](https://docs.pyscript.net/latest/developers/).
|
||||
|
||||
The short version is:
|
||||
|
||||
- Ensure you have Python, node and npm installed.
|
||||
- Create a Python virtual environment.
|
||||
- In the root of this repository `make setup`.
|
||||
- `make build` to build PyScript.
|
||||
- As dependencies change over time, `make update` to keep in sync.
|
||||
|
||||
To start using the locally built version of PyScript, you'll need an HTML
|
||||
page something like this (note the relative paths to assets in the `dist`
|
||||
directory, in the `<head>` of the document):
|
||||
|
||||
```html
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Pure Python PyScript tests</title>
|
||||
<link rel="stylesheet" href="../../dist/core.css" />
|
||||
<script type="module" src="../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script type="mpy" src="./main.py" config="./conf.toml"></script>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
Once set up, you should be able to run the most common activities via the
|
||||
`make` command:
|
||||
|
||||
```
|
||||
$ make
|
||||
|
||||
There is no default Makefile target right now. Try:
|
||||
|
||||
make setup - check your environment and install the dependencies.
|
||||
make update - update dependencies.
|
||||
make clean - clean up auto-generated assets.
|
||||
make build - build PyScript.
|
||||
make precommit-check - run the precommit checks (run eslint).
|
||||
make test - run all automated tests in playwright.
|
||||
make fmt - format the code.
|
||||
make fmt-check - check the code formatting.
|
||||
```
|
||||
|
||||
## Artifacts
|
||||
|
||||
There are two main artifacts in this project:
|
||||
|
||||
- **stdlib** and its content, where `src/stdlib/pyscript.js` exposes as object literal all the _Python_ content within the folder (recursively)
|
||||
- **plugins** and its content, where `src/plugins.js` exposes all available _dynamic imports_, able to instrument the bundler to create files a part within the _dist/_ folder, so that by default _core_ remains as small as possible
|
||||
- **stdlib** and its content: `src/stdlib/pyscript.js` exposes, as a
|
||||
JavaScript object literal, all the _Python_ content within the folder
|
||||
(recursively).
|
||||
- **plugins** and its content: `src/plugins.js` exposes all available
|
||||
_dynamic imports_, and is able to instrument the bundler to create files
|
||||
apart from the `_dist/_` folder, so that by default _core_ remains as small
|
||||
as possible.
|
||||
|
||||
Accordingly, whenever a file contains this warning at its first line, please do not change such file directly before submitting a merge request, as that file will be overwritten at the next `npm run build` command, either here or in _CI_:
|
||||
Accordingly, whenever a file contains this warning at its first line, **please
|
||||
do not change such file directly before submitting a merge request**, as that
|
||||
file will be overwritten at the next `npm run build` command, either here or
|
||||
in _CI_:
|
||||
|
||||
```js
|
||||
// ⚠️ This file is an artifact: DO NOT MODIFY
|
||||
```
|
||||
|
||||
### Running tests
|
||||
## Plugins
|
||||
|
||||
Before running the tests, we need to create a tests environment first. To do so run the following command from the root folder of the project:
|
||||
While community or third party plugins don't need to be part of this repository
|
||||
and can be added just importing `@pyscript/core` as module, there are a few
|
||||
plugins that we would like to make available by default and these are
|
||||
considered _core plugins_.
|
||||
|
||||
```
|
||||
make setup
|
||||
```
|
||||
To add a _core plugin_ to this project define the plugin entry-point and name
|
||||
in the `src/plugins` folder (see the `error.js` example) and create, if
|
||||
necessary, a folder with the same name where extra files or dependencies can be
|
||||
added.
|
||||
|
||||
This will create a tests environment [in the root of the project, named `./env`] and install all the dependencies needed to run the tests.
|
||||
The _build_ command will include plugins by name as artifacts so that the
|
||||
bundler can create ad-hoc files within the `dist/` folder.
|
||||
|
||||
A lot of problems related to `make setup` are related to node and npm being outdated. Once npm and node are updated, `make setup` should work. You can follow the steps on the [npm documentation](https://docs.npmjs.com/try-the-latest-stable-version-of-npm) to update npm (the update command for Linux should work for Mac as well). Once npm has been updated you can continue to the instructions to update node below.
|
||||
## Python
|
||||
|
||||
To update Node run the following commands in order (most likely you'll be prompted for your user password, this is normal):
|
||||
The `pyscript` package available in _Python_ lives in the folder
|
||||
`src/stdlib/pyscript/`.
|
||||
|
||||
```
|
||||
sudo npm cache clean -f
|
||||
sudo npm install -g n
|
||||
sudo n stable
|
||||
```
|
||||
All _Python_ files will be embedded automatically whenever `npm run build`
|
||||
happens and reflected into the `src/stdlib/pyscript.js` file.
|
||||
|
||||
After the `make setup` command has completed, you can run the **automated tests** with
|
||||
the following command:
|
||||
Its _core_ responsibility is to ensure those files will be available through
|
||||
the filesystem in either the _main_ thread, or any _worker_.
|
||||
|
||||
```
|
||||
make test
|
||||
```
|
||||
## Release
|
||||
|
||||
(This essentially runs the `npm run test:integration` command in the right place. This is defined in PyScript's `package.json` file.)
|
||||
To cut a new release of PyScript simply
|
||||
[add a new release](https://github.com/pyscript/pyscript/releases) while
|
||||
remembering to write a comprehensive changelog. A
|
||||
[GitHub action](https://github.com/pyscript/pyscript/blob/main/.github/workflows/publish-release.yml)
|
||||
will kick in and ensure the release is described and deployed to a URL with the
|
||||
pattern: https://pyscript.net/releases/YYYY.M.v/ (year/month/version - as per
|
||||
our [CalVer](https://calver.org/) versioning scheme).
|
||||
|
||||
Tests are found in the `tests` directory. These are organised into three locations:
|
||||
Then, the following three separate repositories need updating:
|
||||
|
||||
1. `python` - the Python based test suite to exercise Python code **within** PyScript.
|
||||
2. `javascript` - JavaScript tests to exercise PyScript itself, in the browser.
|
||||
3. `manual` - containing tests to run manually in a browser, due to the complex nature of the tests.
|
||||
|
||||
We use [Playwright](https://playwright.dev/) to automate the running of the Python and JavaScript test suites. We use [uPyTest](https://github.com/ntoll/upytest) as a test framework for the Python test suite. uPyTest is a "PyTest inspired" framework for running tests in the browser on both MicroPython and Pyodide.
|
||||
|
||||
The automated (Playwright) tests are specified in the `tests/integration.spec.js` file.
|
||||
|
||||
## `pyscript` python package
|
||||
|
||||
The `pyscript` package available in _Python_ lives in the folder `src/stdlib/pyscript/`.
|
||||
|
||||
All _Python_ files will be embedded automatically whenever `npm run build` happens and reflected into the `src/stdlib/pyscript.js` file.
|
||||
|
||||
It is _core_ responsibility to ensure those files will be available through the Filesystem in either the _main_ thread, or any _worker_.
|
||||
|
||||
## JS plugins
|
||||
|
||||
While community or third party plugins don't need to be part of this repository and can be added just importing `@pyscript/core` as module, there are a few plugins that we would like to make available by default and these are considered _core plugins_.
|
||||
|
||||
To add a _core plugin_ to this project you can define your plugin entry-point and name in the `src/plugins` folder (see the `error.js` example) and create, if necessary, a folder with the same name where extra files or dependencies can be added.
|
||||
|
||||
The _build_ command will bring plugins by name as artifact so that the bundler can create ad-hoc files within the `dist/` folder.
|
||||
- [Documentation](https://github.com/pyscript/docs) - Change the `version.json`
|
||||
file in the root of the directory and then `node version-update.js`.
|
||||
- [Homepage](https://github.com/pyscript/pyscript.net) - Ensure the version
|
||||
referenced in `index.html` is the latest version.
|
||||
- [PSDC](https://pyscript.com) - Use discord or Anaconda Slack (if you work at
|
||||
Anaconda) to let the PSDC team know there's a new version, so they can update
|
||||
their project templates.
|
||||
|
||||
922
core/package-lock.json
generated
922
core/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pyscript/core",
|
||||
"version": "0.5.16",
|
||||
"version": "0.6.22",
|
||||
"type": "module",
|
||||
"description": "PyScript",
|
||||
"module": "./index.js",
|
||||
@@ -43,8 +43,8 @@
|
||||
"build:3rd-party": "node rollup/3rd-party.cjs",
|
||||
"build:tests-index": "node rollup/build_test_index.cjs",
|
||||
"clean:3rd-party": "rm src/3rd-party/*.js && rm src/3rd-party/*.css",
|
||||
"test:integration": "static-handler --coi . 2>/dev/null & SH_PID=$!; EXIT_CODE=0; playwright test tests/js_tests.spec.js tests/py_tests.spec.js || EXIT_CODE=$?; kill $SH_PID 2>/dev/null; exit $EXIT_CODE",
|
||||
"test:ws": "bun tests/ws/index.js & playwright test tests/ws.spec.js",
|
||||
"test:integration": "npm run test:ws; static-handler --coi . 2>/dev/null & SH_PID=$!; EXIT_CODE=0; playwright test tests/js_tests.spec.js tests/py_tests.spec.js || EXIT_CODE=$?; kill $SH_PID 2>/dev/null; exit $EXIT_CODE",
|
||||
"test:ws": "bun tests/javascript/ws/index.js & playwright test tests/javascript/ws/index.spec.js",
|
||||
"dev": "node dev.cjs",
|
||||
"release": "npm run build && npm run zip",
|
||||
"size": "echo -e \"\\033[1mdist/*.js file size\\033[0m\"; for js in $(ls dist/*.js); do cat $js | brotli > ._; echo -e \"\\033[2m$js:\\033[0m $(du -h --apparent-size ._ | sed -e 's/[[:space:]]*._//')\"; rm ._; done",
|
||||
@@ -59,41 +59,42 @@
|
||||
"license": "APACHE-2.0",
|
||||
"dependencies": {
|
||||
"@ungap/with-resolvers": "^0.1.0",
|
||||
"@webreflection/idb-map": "^0.3.1",
|
||||
"@webreflection/idb-map": "^0.3.2",
|
||||
"add-promise-listener": "^0.1.3",
|
||||
"basic-devtools": "^0.1.6",
|
||||
"polyscript": "^0.15.12",
|
||||
"sabayon": "^0.5.2",
|
||||
"polyscript": "^0.16.10",
|
||||
"sabayon": "^0.6.1",
|
||||
"sticky-module": "^0.1.1",
|
||||
"to-json-callback": "^0.1.1",
|
||||
"type-checked-collections": "^0.1.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@codemirror/commands": "^6.6.2",
|
||||
"@codemirror/commands": "^6.7.1",
|
||||
"@codemirror/lang-python": "^6.1.6",
|
||||
"@codemirror/language": "^6.10.3",
|
||||
"@codemirror/language": "^6.10.6",
|
||||
"@codemirror/state": "^6.4.1",
|
||||
"@codemirror/view": "^6.34.1",
|
||||
"@codemirror/view": "^6.35.0",
|
||||
"@playwright/test": "1.45.3",
|
||||
"@rollup/plugin-commonjs": "^28.0.0",
|
||||
"@rollup/plugin-commonjs": "^28.0.1",
|
||||
"@rollup/plugin-node-resolve": "^15.3.0",
|
||||
"@rollup/plugin-terser": "^0.4.4",
|
||||
"@webreflection/toml-j0.4": "^1.1.3",
|
||||
"@xterm/addon-fit": "^0.10.0",
|
||||
"@xterm/addon-web-links": "^0.11.0",
|
||||
"bun": "^1.1.29",
|
||||
"@xterm/xterm": "^5.5.0",
|
||||
"bun": "^1.1.38",
|
||||
"chokidar": "^4.0.1",
|
||||
"codedent": "^0.1.2",
|
||||
"codemirror": "^6.0.1",
|
||||
"eslint": "^9.11.1",
|
||||
"flatted": "^3.3.1",
|
||||
"rollup": "^4.22.5",
|
||||
"eslint": "^9.16.0",
|
||||
"flatted": "^3.3.2",
|
||||
"rollup": "^4.28.1",
|
||||
"rollup-plugin-postcss": "^4.0.2",
|
||||
"rollup-plugin-string": "^3.0.0",
|
||||
"static-handler": "^0.5.3",
|
||||
"string-width": "^7.2.0",
|
||||
"typescript": "^5.6.2",
|
||||
"xterm": "^5.3.0",
|
||||
"xterm-readline": "^1.1.1"
|
||||
"typescript": "^5.7.2",
|
||||
"xterm-readline": "^1.1.2"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@@ -46,7 +46,7 @@ const modules = {
|
||||
"toml.js": join(node_modules, "@webreflection", "toml-j0.4", "toml.js"),
|
||||
|
||||
// xterm
|
||||
"xterm.js": resolve("xterm"),
|
||||
"xterm.js": resolve("@xterm/xterm"),
|
||||
"xterm-readline.js": resolve("xterm-readline"),
|
||||
"xterm_addon-fit.js": fetch(`${CDN}/@xterm/addon-fit/+esm`).then((b) =>
|
||||
b.text(),
|
||||
@@ -54,9 +54,9 @@ const modules = {
|
||||
"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(
|
||||
(b) => b.text(),
|
||||
),
|
||||
"xterm.css": fetch(
|
||||
`${CDN}/@xterm/xterm@${v("@xterm/xterm")}/css/xterm.min.css`,
|
||||
).then((b) => b.text()),
|
||||
|
||||
// codemirror
|
||||
"codemirror.js": reBundle("codemirror"),
|
||||
|
||||
4
core/src/3rd-party/xterm-readline.js
vendored
4
core/src/3rd-party/xterm-readline.js
vendored
File diff suppressed because one or more lines are too long
4
core/src/3rd-party/xterm.css
vendored
4
core/src/3rd-party/xterm.css
vendored
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Minified by jsDelivr using clean-css v5.3.2.
|
||||
* Original file: /npm/xterm@5.3.0/css/xterm.css
|
||||
* Original file: /npm/@xterm/xterm@5.5.0/css/xterm.css
|
||||
*
|
||||
* Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
|
||||
*/
|
||||
.xterm{cursor:text;position:relative;user-select:none;-ms-user-select:none;-webkit-user-select:none}.xterm.focus,.xterm:focus{outline:0}.xterm .xterm-helpers{position:absolute;top:0;z-index:5}.xterm .xterm-helper-textarea{padding:0;border:0;margin:0;position:absolute;opacity:0;left:-9999em;top:0;width:0;height:0;z-index:-5;white-space:nowrap;overflow:hidden;resize:none}.xterm .composition-view{background:#000;color:#fff;display:none;position:absolute;white-space:nowrap;z-index:1}.xterm .composition-view.active{display:block}.xterm .xterm-viewport{background-color:#000;overflow-y:scroll;cursor:default;position:absolute;right:0;left:0;top:0;bottom:0}.xterm .xterm-screen{position:relative}.xterm .xterm-screen canvas{position:absolute;left:0;top:0}.xterm .xterm-scroll-area{visibility:hidden}.xterm-char-measure-element{display:inline-block;visibility:hidden;position:absolute;top:0;left:-9999em;line-height:normal}.xterm.enable-mouse-events{cursor:default}.xterm .xterm-cursor-pointer,.xterm.xterm-cursor-pointer{cursor:pointer}.xterm.column-select.focus{cursor:crosshair}.xterm .xterm-accessibility,.xterm .xterm-message{position:absolute;left:0;top:0;bottom:0;right:0;z-index:10;color:transparent;pointer-events:none}.xterm .live-region{position:absolute;left:-9999px;width:1px;height:1px;overflow:hidden}.xterm-dim{opacity:1!important}.xterm-underline-1{text-decoration:underline}.xterm-underline-2{text-decoration:double underline}.xterm-underline-3{text-decoration:wavy underline}.xterm-underline-4{text-decoration:dotted underline}.xterm-underline-5{text-decoration:dashed underline}.xterm-overline{text-decoration:overline}.xterm-overline.xterm-underline-1{text-decoration:overline underline}.xterm-overline.xterm-underline-2{text-decoration:overline double underline}.xterm-overline.xterm-underline-3{text-decoration:overline wavy underline}.xterm-overline.xterm-underline-4{text-decoration:overline dotted underline}.xterm-overline.xterm-underline-5{text-decoration:overline dashed underline}.xterm-strikethrough{text-decoration:line-through}.xterm-screen .xterm-decoration-container .xterm-decoration{z-index:6;position:absolute}.xterm-screen .xterm-decoration-container .xterm-decoration.xterm-decoration-top-layer{z-index:7}.xterm-decoration-overview-ruler{z-index:8;position:absolute;top:0;right:0;pointer-events:none}.xterm-decoration-top{z-index:2;position:relative}
|
||||
.xterm{cursor:text;position:relative;user-select:none;-ms-user-select:none;-webkit-user-select:none}.xterm.focus,.xterm:focus{outline:0}.xterm .xterm-helpers{position:absolute;top:0;z-index:5}.xterm .xterm-helper-textarea{padding:0;border:0;margin:0;position:absolute;opacity:0;left:-9999em;top:0;width:0;height:0;z-index:-5;white-space:nowrap;overflow:hidden;resize:none}.xterm .composition-view{background:#000;color:#fff;display:none;position:absolute;white-space:nowrap;z-index:1}.xterm .composition-view.active{display:block}.xterm .xterm-viewport{background-color:#000;overflow-y:scroll;cursor:default;position:absolute;right:0;left:0;top:0;bottom:0}.xterm .xterm-screen{position:relative}.xterm .xterm-screen canvas{position:absolute;left:0;top:0}.xterm .xterm-scroll-area{visibility:hidden}.xterm-char-measure-element{display:inline-block;visibility:hidden;position:absolute;top:0;left:-9999em;line-height:normal}.xterm.enable-mouse-events{cursor:default}.xterm .xterm-cursor-pointer,.xterm.xterm-cursor-pointer{cursor:pointer}.xterm.column-select.focus{cursor:crosshair}.xterm .xterm-accessibility:not(.debug),.xterm .xterm-message{position:absolute;left:0;top:0;bottom:0;right:0;z-index:10;color:transparent;pointer-events:none}.xterm .xterm-accessibility-tree:not(.debug) ::selection{color:transparent}.xterm .xterm-accessibility-tree{user-select:text;white-space:pre}.xterm .live-region{position:absolute;left:-9999px;width:1px;height:1px;overflow:hidden}.xterm-dim{opacity:1!important}.xterm-underline-1{text-decoration:underline}.xterm-underline-2{text-decoration:double underline}.xterm-underline-3{text-decoration:wavy underline}.xterm-underline-4{text-decoration:dotted underline}.xterm-underline-5{text-decoration:dashed underline}.xterm-overline{text-decoration:overline}.xterm-overline.xterm-underline-1{text-decoration:overline underline}.xterm-overline.xterm-underline-2{text-decoration:overline double underline}.xterm-overline.xterm-underline-3{text-decoration:overline wavy underline}.xterm-overline.xterm-underline-4{text-decoration:overline dotted underline}.xterm-overline.xterm-underline-5{text-decoration:overline dashed underline}.xterm-strikethrough{text-decoration:line-through}.xterm-screen .xterm-decoration-container .xterm-decoration{z-index:6;position:absolute}.xterm-screen .xterm-decoration-container .xterm-decoration.xterm-decoration-top-layer{z-index:7}.xterm-decoration-overview-ruler{z-index:8;position:absolute;top:0;right:0;pointer-events:none}.xterm-decoration-top{z-index:2;position:relative}
|
||||
|
||||
4
core/src/3rd-party/xterm.js
vendored
4
core/src/3rd-party/xterm.js
vendored
File diff suppressed because one or more lines are too long
@@ -56,7 +56,7 @@ const syntaxError = (type, url, { message }) => {
|
||||
const configs = new Map();
|
||||
|
||||
for (const [TYPE] of TYPES) {
|
||||
/** @type {Promise<[...any]>} A Promise wrapping any plugins which should be loaded. */
|
||||
/** @type {() => Promise<[...any]>} A Promise wrapping any plugins which should be loaded. */
|
||||
let plugins;
|
||||
|
||||
/** @type {any} The PyScript configuration parsed from the JSON or TOML object*. May be any of the return types of JSON.parse() or toml-j0.4's parse() ( {number | string | boolean | null | object | Array} ) */
|
||||
@@ -135,22 +135,24 @@ for (const [TYPE] of TYPES) {
|
||||
|
||||
// parse all plugins and optionally ignore only
|
||||
// those flagged as "undesired" via `!` prefix
|
||||
const toBeAwaited = [];
|
||||
for (const [key, value] of Object.entries(allPlugins)) {
|
||||
if (error) {
|
||||
if (key === "error") {
|
||||
// show on page the config is broken, meaning that
|
||||
// it was not possible to disable error plugin neither
|
||||
// as that part wasn't correctly parsed anyway
|
||||
value().then(({ notify }) => notify(error.message));
|
||||
plugins = async () => {
|
||||
const toBeAwaited = [];
|
||||
for (const [key, value] of Object.entries(allPlugins)) {
|
||||
if (error) {
|
||||
if (key === "error") {
|
||||
// show on page the config is broken, meaning that
|
||||
// it was not possible to disable error plugin neither
|
||||
// as that part wasn't correctly parsed anyway
|
||||
value().then(({ notify }) => notify(error.message));
|
||||
}
|
||||
} else if (!parsed?.plugins?.includes(`!${key}`)) {
|
||||
toBeAwaited.push(value().then(({ default: p }) => p));
|
||||
} else if (key === "error") {
|
||||
toBeAwaited.push(value().then(({ notOnDOM }) => notOnDOM()));
|
||||
}
|
||||
} else if (!parsed?.plugins?.includes(`!${key}`)) {
|
||||
toBeAwaited.push(value().then(({ default: p }) => p));
|
||||
}
|
||||
}
|
||||
|
||||
// assign plugins as Promise.all only if needed
|
||||
plugins = Promise.all(toBeAwaited);
|
||||
return await Promise.all(toBeAwaited);
|
||||
};
|
||||
|
||||
configs.set(TYPE, { config: parsed, configURL, plugins, error });
|
||||
}
|
||||
|
||||
@@ -73,3 +73,8 @@ mpy-config {
|
||||
background-color: #fff;
|
||||
animation: spinner 0.6s linear infinite;
|
||||
}
|
||||
|
||||
py-terminal span,
|
||||
mpy-terminal span {
|
||||
letter-spacing: 0 !important;
|
||||
}
|
||||
|
||||
@@ -34,9 +34,17 @@ import {
|
||||
inputFailure,
|
||||
} from "./hooks.js";
|
||||
|
||||
import codemirror from "./plugins/codemirror.js";
|
||||
export { codemirror };
|
||||
|
||||
import { stdlib, optional } from "./stdlib.js";
|
||||
export { stdlib, optional, inputFailure };
|
||||
|
||||
export const donkey = (options) =>
|
||||
import(/* webpackIgnore: true */ "./plugins/donkey.js").then((module) =>
|
||||
module.default(options),
|
||||
);
|
||||
|
||||
// generic helper to disambiguate between custom element and script
|
||||
const isScript = ({ tagName }) => tagName === "SCRIPT";
|
||||
|
||||
@@ -174,7 +182,7 @@ for (const [TYPE, interpreter] of TYPES) {
|
||||
// ensure plugins are bootstrapped already before custom type definition
|
||||
// NOTE: we cannot top-level await in here as plugins import other utilities
|
||||
// from core.js itself so that custom definition should not be blocking.
|
||||
plugins.then(() => {
|
||||
plugins().then(() => {
|
||||
// possible early errors sent by polyscript
|
||||
const errors = new Map();
|
||||
|
||||
@@ -215,6 +223,13 @@ for (const [TYPE, interpreter] of TYPES) {
|
||||
else element.after(show);
|
||||
}
|
||||
if (!show.id) show.id = getID();
|
||||
if (TYPE === "py") {
|
||||
const canvas2D = element.getAttribute("canvas2d") || element.getAttribute("canvas");
|
||||
if (canvas2D) {
|
||||
const canvas = queryTarget(document, canvas2D);
|
||||
wrap.interpreter.canvas.setCanvas2D(canvas);
|
||||
}
|
||||
}
|
||||
|
||||
// allows the code to retrieve the target element via
|
||||
// document.currentScript.target if needed
|
||||
|
||||
@@ -84,7 +84,19 @@ export const hooks = {
|
||||
},
|
||||
worker: {
|
||||
/** @type {Set<function>} */
|
||||
onReady: new SetFunction(),
|
||||
onReady: new SetFunction([
|
||||
(wrap, xworker) => {
|
||||
if (wrap.type === "py") {
|
||||
const { interpreter } = wrap;
|
||||
const element = wrap.run('from polyscript import currentScript;currentScript');
|
||||
const canvas2D = element.getAttribute("canvas2d") || element.getAttribute("canvas");
|
||||
if (canvas2D) {
|
||||
const canvas = element.ownerDocument.getElementById(canvas2D);
|
||||
interpreter.canvas.setCanvas2D(canvas);
|
||||
}
|
||||
}
|
||||
}
|
||||
]),
|
||||
/** @type {Set<function>} */
|
||||
onBeforeRun: new SetFunction(),
|
||||
/** @type {Set<function>} */
|
||||
|
||||
@@ -1,10 +1,20 @@
|
||||
// ⚠️ This file is an artifact: DO NOT MODIFY
|
||||
export default {
|
||||
codemirror: () =>
|
||||
import(
|
||||
/* webpackIgnore: true */
|
||||
"./plugins/codemirror.js"
|
||||
),
|
||||
["deprecations-manager"]: () =>
|
||||
import(
|
||||
/* webpackIgnore: true */
|
||||
"./plugins/deprecations-manager.js"
|
||||
),
|
||||
donkey: () =>
|
||||
import(
|
||||
/* webpackIgnore: true */
|
||||
"./plugins/donkey.js"
|
||||
),
|
||||
error: () =>
|
||||
import(
|
||||
/* webpackIgnore: true */
|
||||
|
||||
31
core/src/plugins/codemirror.js
Normal file
31
core/src/plugins/codemirror.js
Normal file
@@ -0,0 +1,31 @@
|
||||
// lazy loaded on-demand codemirror related files
|
||||
export default {
|
||||
get core() {
|
||||
return import(/* webpackIgnore: true */ "../3rd-party/codemirror.js");
|
||||
},
|
||||
get state() {
|
||||
return import(
|
||||
/* webpackIgnore: true */ "../3rd-party/codemirror_state.js"
|
||||
);
|
||||
},
|
||||
get python() {
|
||||
return import(
|
||||
/* webpackIgnore: true */ "../3rd-party/codemirror_lang-python.js"
|
||||
);
|
||||
},
|
||||
get language() {
|
||||
return import(
|
||||
/* webpackIgnore: true */ "../3rd-party/codemirror_language.js"
|
||||
);
|
||||
},
|
||||
get view() {
|
||||
return import(
|
||||
/* webpackIgnore: true */ "../3rd-party/codemirror_view.js"
|
||||
);
|
||||
},
|
||||
get commands() {
|
||||
return import(
|
||||
/* webpackIgnore: true */ "../3rd-party/codemirror_commands.js"
|
||||
);
|
||||
},
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
// PyScript Derepcations Plugin
|
||||
import { hooks } from "../core.js";
|
||||
import { notify } from "./error.js";
|
||||
import { hooks } from "../core.js";
|
||||
|
||||
// react lazily on PyScript bootstrap
|
||||
hooks.main.onReady.add(checkDeprecations);
|
||||
|
||||
114
core/src/plugins/donkey.js
Normal file
114
core/src/plugins/donkey.js
Normal file
@@ -0,0 +1,114 @@
|
||||
import addPromiseListener from "add-promise-listener";
|
||||
import { assign, dedent } from "polyscript/exports";
|
||||
|
||||
const { stringify } = JSON;
|
||||
|
||||
const invoke = (name, args) => `${name}(code, ${args.join(", ")})`;
|
||||
|
||||
const donkey = ({ type = "py", persistent, terminal, config }) => {
|
||||
const globals = terminal ? '{"__terminal__":__terminal__}' : "{}";
|
||||
const args = persistent ? ["globals()", "__locals__"] : [globals, "{}"];
|
||||
|
||||
const src = URL.createObjectURL(
|
||||
new Blob([
|
||||
[
|
||||
// this array is to better minify this code once in production
|
||||
"from pyscript import sync, config",
|
||||
'__message__ = lambda e,v: f"\x1b[31m\x1b[1m{e.__name__}\x1b[0m: {v}"',
|
||||
"__locals__ = {}",
|
||||
'if config["type"] == "py":',
|
||||
" import sys",
|
||||
" def __error__(_):",
|
||||
" info = sys.exc_info()",
|
||||
" return __message__(info[0], info[1])",
|
||||
"else:",
|
||||
" __error__ = lambda e: __message__(e.__class__, e.value)",
|
||||
"def execute(code):",
|
||||
` try: return ${invoke("exec", args)};`,
|
||||
" except Exception as e: print(__error__(e));",
|
||||
"def evaluate(code):",
|
||||
` try: return ${invoke("eval", args)};`,
|
||||
" except Exception as e: print(__error__(e));",
|
||||
"sync.execute = execute",
|
||||
"sync.evaluate = evaluate",
|
||||
].join("\n"),
|
||||
]),
|
||||
);
|
||||
|
||||
// create the script that exposes the code to execute or evaluate
|
||||
const script = assign(document.createElement("script"), { type, src });
|
||||
script.toggleAttribute("worker", true);
|
||||
script.toggleAttribute("terminal", true);
|
||||
if (terminal) script.setAttribute("target", terminal);
|
||||
if (config) {
|
||||
script.setAttribute(
|
||||
"config",
|
||||
typeof config === "string" ? config : stringify(config),
|
||||
);
|
||||
}
|
||||
|
||||
return addPromiseListener(
|
||||
document.body.appendChild(script),
|
||||
`${type}:done`,
|
||||
{ stopPropagation: true },
|
||||
).then(() => {
|
||||
URL.revokeObjectURL(src);
|
||||
return script;
|
||||
});
|
||||
};
|
||||
|
||||
const utils = async (options) => {
|
||||
const script = await donkey(options);
|
||||
const { xworker, process, terminal } = script;
|
||||
const { execute, evaluate } = xworker.sync;
|
||||
script.remove();
|
||||
return {
|
||||
xworker,
|
||||
process,
|
||||
terminal,
|
||||
execute,
|
||||
evaluate,
|
||||
};
|
||||
};
|
||||
|
||||
export default async (options = {}) => {
|
||||
let farmer = await utils(options);
|
||||
let working = false;
|
||||
const kill = () => {
|
||||
if (farmer) {
|
||||
farmer.xworker.terminate();
|
||||
farmer.terminal.dispose();
|
||||
farmer = null;
|
||||
}
|
||||
working = false;
|
||||
};
|
||||
const reload = async () => {
|
||||
kill();
|
||||
farmer = await utils(options);
|
||||
};
|
||||
const asyncTask = (method) => async (code) => {
|
||||
// race condition ... a new task has been
|
||||
// assigned while the previous one didn't finish
|
||||
if (working) await reload();
|
||||
working = true;
|
||||
try {
|
||||
return await farmer[method](dedent(code));
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
} finally {
|
||||
working = false;
|
||||
}
|
||||
};
|
||||
const asyncMethod = (method) => async () => {
|
||||
if (working) await reload();
|
||||
else farmer?.terminal[method]();
|
||||
};
|
||||
return {
|
||||
process: asyncTask("process"),
|
||||
execute: asyncTask("execute"),
|
||||
evaluate: asyncTask("evaluate"),
|
||||
clear: asyncMethod("clear"),
|
||||
reset: asyncMethod("reset"),
|
||||
kill,
|
||||
};
|
||||
};
|
||||
@@ -1,6 +1,12 @@
|
||||
// PyScript Error Plugin
|
||||
import { buffered } from "polyscript/exports";
|
||||
import { hooks } from "../core.js";
|
||||
|
||||
let dontBotherDOM = false;
|
||||
export function notOnDOM() {
|
||||
dontBotherDOM = true;
|
||||
}
|
||||
|
||||
hooks.main.onReady.add(function override(pyScript) {
|
||||
// be sure this override happens only once
|
||||
hooks.main.onReady.delete(override);
|
||||
@@ -8,13 +14,15 @@ hooks.main.onReady.add(function override(pyScript) {
|
||||
// trap generic `stderr` to propagate to it regardless
|
||||
const { stderr } = pyScript.io;
|
||||
|
||||
// override it with our own logic
|
||||
pyScript.io.stderr = (error, ...rest) => {
|
||||
const cb = (error, ...rest) => {
|
||||
notify(error.message || error);
|
||||
// let other plugins or stderr hook, if any, do the rest
|
||||
return stderr(error, ...rest);
|
||||
};
|
||||
|
||||
// override it with our own logic
|
||||
pyScript.io.stderr = pyScript.type === "py" ? cb : buffered(cb);
|
||||
|
||||
// be sure uncaught Python errors are also visible
|
||||
addEventListener("error", ({ message }) => {
|
||||
if (message.startsWith("Uncaught PythonError")) notify(message);
|
||||
@@ -30,6 +38,7 @@ hooks.main.onReady.add(function override(pyScript) {
|
||||
* @param {string} message
|
||||
*/
|
||||
export function notify(message) {
|
||||
if (dontBotherDOM) return;
|
||||
const div = document.createElement("div");
|
||||
div.className = "py-error";
|
||||
div.textContent = message;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import { Hook, XWorker, dedent, defineProperties } from "polyscript/exports";
|
||||
import { TYPES, offline_interpreter, relative_url, stdlib } from "../core.js";
|
||||
import { notify } from "./error.js";
|
||||
import codemirror from "./codemirror.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>`;
|
||||
|
||||
@@ -77,6 +78,19 @@ async function execute({ currentTarget }) {
|
||||
|
||||
const xworker = XWorker.call(new Hook(null, hooks), srcLink, details);
|
||||
|
||||
// expose xworker like in terminal or other workers to allow
|
||||
// creation and destruction of editors on the fly
|
||||
if (hasRunButton) {
|
||||
for (const type of TYPES.keys()) {
|
||||
const editor = currentTarget.closest(`.${type}-editor-box`);
|
||||
const script = editor?.parentNode?.previousElementSibling;
|
||||
if (script) {
|
||||
defineProperties(script, { xworker: { value: xworker } });
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const { sync } = xworker;
|
||||
const { promise, resolve } = Promise.withResolvers();
|
||||
envs.set(env, promise);
|
||||
@@ -181,14 +195,12 @@ const init = async (script, type, interpreter) => {
|
||||
{ keymap },
|
||||
{ defaultKeymap, indentWithTab },
|
||||
] = await Promise.all([
|
||||
import(/* webpackIgnore: true */ "../3rd-party/codemirror.js"),
|
||||
import(/* webpackIgnore: true */ "../3rd-party/codemirror_state.js"),
|
||||
import(
|
||||
/* webpackIgnore: true */ "../3rd-party/codemirror_lang-python.js"
|
||||
),
|
||||
import(/* webpackIgnore: true */ "../3rd-party/codemirror_language.js"),
|
||||
import(/* webpackIgnore: true */ "../3rd-party/codemirror_view.js"),
|
||||
import(/* webpackIgnore: true */ "../3rd-party/codemirror_commands.js"),
|
||||
codemirror.core,
|
||||
codemirror.state,
|
||||
codemirror.python,
|
||||
codemirror.language,
|
||||
codemirror.view,
|
||||
codemirror.commands,
|
||||
]);
|
||||
|
||||
let isSetup = script.hasAttribute("setup");
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// PyScript pyodide terminal plugin
|
||||
import { hooks, inputFailure } from "../../core.js";
|
||||
import { defineProperties } from "polyscript/exports";
|
||||
import { hooks, inputFailure } from "../../core.js";
|
||||
|
||||
const bootstrapped = new WeakSet();
|
||||
|
||||
@@ -34,6 +34,8 @@ const workerReady = ({ interpreter, io, run, type }, { sync }) => {
|
||||
pyterminal_write(String(error.message || error));
|
||||
};
|
||||
|
||||
sync.pyterminal_stream_write = () => {};
|
||||
|
||||
// tiny shim of the code module with only interact
|
||||
// to bootstrap a REPL like environment
|
||||
interpreter.registerJsModule("code", {
|
||||
@@ -71,6 +73,7 @@ export default async (element) => {
|
||||
disableStdin: false,
|
||||
cursorBlink: true,
|
||||
cursorStyle: "block",
|
||||
lineHeight: 1.2,
|
||||
};
|
||||
|
||||
let stream;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// PyScript py-terminal plugin
|
||||
import { hooks } from "../../core.js";
|
||||
import { defineProperties } from "polyscript/exports";
|
||||
import { hooks } from "../../core.js";
|
||||
|
||||
const bootstrapped = new WeakSet();
|
||||
|
||||
@@ -126,6 +126,7 @@ export default async (element) => {
|
||||
disableStdin: false,
|
||||
cursorBlink: true,
|
||||
cursorStyle: "block",
|
||||
lineHeight: 1.2,
|
||||
});
|
||||
|
||||
xworker.sync.is_pyterminal = () => true;
|
||||
@@ -136,6 +137,18 @@ export default async (element) => {
|
||||
// setup remote thread JS/Python code for whenever the
|
||||
// worker is ready to become a terminal
|
||||
hooks.worker.onReady.add(workerReady);
|
||||
|
||||
// @see https://github.com/pyscript/pyscript/issues/2246
|
||||
const patchInput = [
|
||||
"import builtins as _b",
|
||||
"from pyscript import sync as _s",
|
||||
"_b.input = _s.pyterminal_read",
|
||||
"del _b",
|
||||
"del _s",
|
||||
].join("\n");
|
||||
|
||||
hooks.worker.codeBeforeRun.add(patchInput);
|
||||
hooks.worker.codeBeforeRunAsync.add(patchInput);
|
||||
} else {
|
||||
// in the main case, just bootstrap XTerm without
|
||||
// allowing any input as that's not possible / awkward
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -30,8 +30,6 @@
|
||||
# as it works transparently in both the main thread and worker cases.
|
||||
|
||||
from polyscript import lazy_py_modules as py_import
|
||||
from pyscript.display import HTML, display
|
||||
from pyscript.fetch import fetch
|
||||
from pyscript.magic_js import (
|
||||
RUNNING_IN_WORKER,
|
||||
PyWorker,
|
||||
@@ -43,19 +41,11 @@ from pyscript.magic_js import (
|
||||
sync,
|
||||
window,
|
||||
)
|
||||
from pyscript.display import HTML, display
|
||||
from pyscript.fetch import fetch
|
||||
from pyscript.storage import Storage, storage
|
||||
from pyscript.websocket import WebSocket
|
||||
from pyscript.events import when, Event
|
||||
|
||||
if not RUNNING_IN_WORKER:
|
||||
from pyscript.workers import create_named_worker, workers
|
||||
|
||||
try:
|
||||
from pyscript.event_handling import when
|
||||
except:
|
||||
# TODO: should we remove this? Or at the very least, we should capture
|
||||
# the traceback otherwise it's very hard to debug
|
||||
from pyscript.util import NotSupported
|
||||
|
||||
when = NotSupported(
|
||||
"pyscript.when", "pyscript.when currently not available with this interpreter"
|
||||
)
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
import inspect
|
||||
|
||||
try:
|
||||
from pyodide.ffi.wrappers import add_event_listener
|
||||
|
||||
except ImportError:
|
||||
|
||||
def add_event_listener(el, event_type, func):
|
||||
el.addEventListener(event_type, func)
|
||||
|
||||
|
||||
from pyscript.magic_js import document
|
||||
|
||||
|
||||
def when(event_type=None, selector=None):
|
||||
"""
|
||||
Decorates a function and passes py-* events to the decorated function
|
||||
The events might or not be an argument of the decorated function
|
||||
"""
|
||||
|
||||
def decorator(func):
|
||||
|
||||
from pyscript.web import Element, ElementCollection
|
||||
|
||||
if isinstance(selector, str):
|
||||
elements = document.querySelectorAll(selector)
|
||||
# TODO: This is a hack that will be removed when pyscript becomes a package
|
||||
# and we can better manage the imports without circular dependencies
|
||||
elif isinstance(selector, Element):
|
||||
elements = [selector._dom_element]
|
||||
elif isinstance(selector, ElementCollection):
|
||||
elements = [el._dom_element for el in selector]
|
||||
else:
|
||||
if isinstance(selector, list):
|
||||
elements = selector
|
||||
else:
|
||||
elements = [selector]
|
||||
|
||||
try:
|
||||
sig = inspect.signature(func)
|
||||
# Function doesn't receive events
|
||||
if not sig.parameters:
|
||||
|
||||
# Function is async: must be awaited
|
||||
if inspect.iscoroutinefunction(func):
|
||||
|
||||
async def wrapper(*args, **kwargs):
|
||||
await func()
|
||||
|
||||
else:
|
||||
|
||||
def wrapper(*args, **kwargs):
|
||||
func()
|
||||
|
||||
else:
|
||||
wrapper = func
|
||||
|
||||
except AttributeError:
|
||||
# TODO: this is very ugly hack to get micropython working because inspect.signature
|
||||
# doesn't exist, but we need to actually properly replace inspect.signature.
|
||||
# It may be actually better to not try any magic for now and raise the error
|
||||
def wrapper(*args, **kwargs):
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
except TypeError as e:
|
||||
if "takes" in str(e) and "positional arguments" in str(e):
|
||||
return func()
|
||||
|
||||
raise
|
||||
|
||||
for el in elements:
|
||||
add_event_listener(el, event_type, wrapper)
|
||||
|
||||
return func
|
||||
|
||||
return decorator
|
||||
166
core/src/stdlib/pyscript/events.py
Normal file
166
core/src/stdlib/pyscript/events.py
Normal file
@@ -0,0 +1,166 @@
|
||||
import asyncio
|
||||
import inspect
|
||||
import sys
|
||||
|
||||
from functools import wraps
|
||||
from pyscript.magic_js import document
|
||||
from pyscript.ffi import create_proxy
|
||||
from pyscript.util import is_awaitable
|
||||
from pyscript import config
|
||||
|
||||
|
||||
class Event:
|
||||
"""
|
||||
Represents something that may happen at some point in the future.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._listeners = []
|
||||
|
||||
def trigger(self, result):
|
||||
"""
|
||||
Trigger the event with a result to pass into the handlers.
|
||||
"""
|
||||
for listener in self._listeners:
|
||||
if is_awaitable(listener):
|
||||
# Use create task to avoid making this an async function.
|
||||
asyncio.create_task(listener(result))
|
||||
else:
|
||||
listener(result)
|
||||
|
||||
def add_listener(self, listener):
|
||||
"""
|
||||
Add a callable/awaitable to listen to when this event is triggered.
|
||||
"""
|
||||
if is_awaitable(listener) or callable(listener):
|
||||
if listener not in self._listeners:
|
||||
self._listeners.append(listener)
|
||||
else:
|
||||
raise ValueError("Listener must be callable or awaitable.")
|
||||
|
||||
def remove_listener(self, *args):
|
||||
"""
|
||||
Clear the specified handler functions in *args. If no handlers
|
||||
provided, clear all handlers.
|
||||
"""
|
||||
if args:
|
||||
for listener in args:
|
||||
self._listeners.remove(listener)
|
||||
else:
|
||||
self._listeners = []
|
||||
|
||||
|
||||
def when(target, *args, **kwargs):
|
||||
"""
|
||||
Add an event listener to the target element(s) for the specified event type.
|
||||
|
||||
The target can be a string representing the event type, or an Event object.
|
||||
If the target is an Event object, the event listener will be added to that
|
||||
object. If the target is a string, the event listener will be added to the
|
||||
element(s) that match the (second) selector argument.
|
||||
|
||||
If a (third) handler argument is provided, it will be called when the event
|
||||
is triggered; thus allowing this to be used as both a function and a
|
||||
decorator.
|
||||
"""
|
||||
# If "when" is called as a function, try to grab the handler from the
|
||||
# arguments. If there's no handler, this must be a decorator based call.
|
||||
handler = None
|
||||
if args and (callable(args[0]) or is_awaitable(args[0])):
|
||||
handler = args[0]
|
||||
elif callable(kwargs.get("handler")) or is_awaitable(kwargs.get("handler")):
|
||||
handler = kwargs.pop("handler")
|
||||
# If the target is a string, it is the "older" use of `when` where it
|
||||
# represents the name of a DOM event.
|
||||
if isinstance(target, str):
|
||||
# Extract the selector from the arguments or keyword arguments.
|
||||
selector = args[0] if args else kwargs.pop("selector")
|
||||
if not selector:
|
||||
raise ValueError("No selector provided.")
|
||||
# Grab the DOM elements to which the target event will be attached.
|
||||
from pyscript.web import Element, ElementCollection
|
||||
|
||||
if isinstance(selector, str):
|
||||
elements = document.querySelectorAll(selector)
|
||||
elif isinstance(selector, Element):
|
||||
elements = [selector._dom_element]
|
||||
elif isinstance(selector, ElementCollection):
|
||||
elements = [el._dom_element for el in selector]
|
||||
else:
|
||||
elements = selector if isinstance(selector, list) else [selector]
|
||||
|
||||
def decorator(func):
|
||||
if config["type"] == "mpy": # Is MicroPython?
|
||||
if is_awaitable(func):
|
||||
|
||||
async def wrapper(*args, **kwargs):
|
||||
"""
|
||||
This is a very ugly hack to get micropython working because
|
||||
`inspect.signature` doesn't exist. It may be actually better
|
||||
to not try any magic for now and raise the error.
|
||||
"""
|
||||
try:
|
||||
return await func(*args, **kwargs)
|
||||
|
||||
except TypeError as e:
|
||||
if "takes" in str(e) and "positional arguments" in str(e):
|
||||
return await func()
|
||||
raise
|
||||
|
||||
else:
|
||||
|
||||
def wrapper(*args, **kwargs):
|
||||
"""
|
||||
This is a very ugly hack to get micropython working because
|
||||
`inspect.signature` doesn't exist. It may be actually better
|
||||
to not try any magic for now and raise the error.
|
||||
"""
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
|
||||
except TypeError as e:
|
||||
if "takes" in str(e) and "positional arguments" in str(e):
|
||||
return func()
|
||||
raise
|
||||
|
||||
else:
|
||||
sig = inspect.signature(func)
|
||||
if sig.parameters:
|
||||
if is_awaitable(func):
|
||||
|
||||
async def wrapper(event):
|
||||
return await func(event)
|
||||
|
||||
else:
|
||||
wrapper = func
|
||||
else:
|
||||
# Function doesn't receive events.
|
||||
if is_awaitable(func):
|
||||
|
||||
async def wrapper(*args, **kwargs):
|
||||
return await func()
|
||||
|
||||
else:
|
||||
|
||||
def wrapper(*args, **kwargs):
|
||||
return func()
|
||||
|
||||
wrapper = wraps(func)(wrapper)
|
||||
if isinstance(target, Event):
|
||||
# The target is a single Event object.
|
||||
target.add_listener(wrapper)
|
||||
elif isinstance(target, list) and all(isinstance(t, Event) for t in target):
|
||||
# The target is a list of Event objects.
|
||||
for evt in target:
|
||||
evt.add_listener(wrapper)
|
||||
else:
|
||||
# The target is a string representing an event type, and so a
|
||||
# DOM element or collection of elements is found in "elements".
|
||||
for el in elements:
|
||||
el.addEventListener(target, create_proxy(wrapper))
|
||||
return wrapper
|
||||
|
||||
# If "when" was called as a decorator, return the decorator function,
|
||||
# otherwise just call the internal decorator function with the supplied
|
||||
# handler.
|
||||
return decorator(handler) if handler else decorator
|
||||
@@ -45,6 +45,8 @@ if RUNNING_IN_WORKER:
|
||||
|
||||
window = polyscript.xworker.window
|
||||
document = window.document
|
||||
# weird + not worth it as it does not work anyway
|
||||
js.screen = window.screen
|
||||
js.document = document
|
||||
# this is the same as js_import on main and it lands modules on main
|
||||
js_import = window.Function(
|
||||
|
||||
91
core/src/stdlib/pyscript/media.py
Normal file
91
core/src/stdlib/pyscript/media.py
Normal file
@@ -0,0 +1,91 @@
|
||||
from pyscript import window
|
||||
from pyscript.ffi import to_js
|
||||
|
||||
|
||||
class Device:
|
||||
"""Device represents a media input or output device, such as a microphone,
|
||||
camera, or headset.
|
||||
"""
|
||||
|
||||
def __init__(self, device):
|
||||
self._dom_element = device
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
return self._dom_element.deviceId
|
||||
|
||||
@property
|
||||
def group(self):
|
||||
return self._dom_element.groupId
|
||||
|
||||
@property
|
||||
def kind(self):
|
||||
return self._dom_element.kind
|
||||
|
||||
@property
|
||||
def label(self):
|
||||
return self._dom_element.label
|
||||
|
||||
def __getitem__(self, key):
|
||||
return getattr(self, key)
|
||||
|
||||
@classmethod
|
||||
async def load(cls, audio=False, video=True):
|
||||
"""Load the device stream."""
|
||||
options = window.Object.new()
|
||||
options.audio = audio
|
||||
if isinstance(video, bool):
|
||||
options.video = video
|
||||
else:
|
||||
# TODO: Think this can be simplified but need to check it on the pyodide side
|
||||
|
||||
# TODO: this is pyodide specific. shouldn't be!
|
||||
options.video = window.Object.new()
|
||||
for k in video:
|
||||
setattr(options.video, k, to_js(video[k]))
|
||||
|
||||
stream = await window.navigator.mediaDevices.getUserMedia(options)
|
||||
return stream
|
||||
|
||||
async def get_stream(self):
|
||||
key = self.kind.replace("input", "").replace("output", "")
|
||||
options = {key: {"deviceId": {"exact": self.id}}}
|
||||
|
||||
return await self.load(**options)
|
||||
|
||||
|
||||
async def list_devices() -> list[dict]:
|
||||
"""
|
||||
Return the list of the currently available media input and output devices,
|
||||
such as microphones, cameras, headsets, and so forth.
|
||||
|
||||
Output:
|
||||
|
||||
list(dict) - list of dictionaries representing the available media devices.
|
||||
Each dictionary has the following keys:
|
||||
* deviceId: a string that is an identifier for the represented device
|
||||
that is persisted across sessions. It is un-guessable by other
|
||||
applications and unique to the origin of the calling application.
|
||||
It is reset when the user clears cookies (for Private Browsing, a
|
||||
different identifier is used that is not persisted across sessions).
|
||||
|
||||
* groupId: a string that is a group identifier. Two devices have the same
|
||||
group identifier if they belong to the same physical device — for
|
||||
example a monitor with both a built-in camera and a microphone.
|
||||
|
||||
* kind: an enumerated value that is either "videoinput", "audioinput"
|
||||
or "audiooutput".
|
||||
|
||||
* label: a string describing this device (for example "External USB
|
||||
Webcam").
|
||||
|
||||
Note: the returned list will omit any devices that are blocked by the document
|
||||
Permission Policy: microphone, camera, speaker-selection (for output devices),
|
||||
and so on. Access to particular non-default devices is also gated by the
|
||||
Permissions API, and the list will omit devices for which the user has not
|
||||
granted explicit permission.
|
||||
"""
|
||||
# https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/enumerateDevices
|
||||
return [
|
||||
Device(obj) for obj in await window.navigator.mediaDevices.enumerateDevices()
|
||||
]
|
||||
@@ -1,7 +1,13 @@
|
||||
import js
|
||||
import sys
|
||||
import inspect
|
||||
|
||||
|
||||
def as_bytearray(buffer):
|
||||
"""
|
||||
Given a JavaScript ArrayBuffer, convert it to a Python bytearray in a
|
||||
MicroPython friendly manner.
|
||||
"""
|
||||
ui8a = js.Uint8Array.new(buffer)
|
||||
size = ui8a.length
|
||||
ba = bytearray(size)
|
||||
@@ -31,3 +37,22 @@ class NotSupported:
|
||||
|
||||
def __call__(self, *args):
|
||||
raise TypeError(self.error)
|
||||
|
||||
|
||||
def is_awaitable(obj):
|
||||
"""
|
||||
Returns a boolean indication if the passed in obj is an awaitable
|
||||
function. (MicroPython treats awaitables as generator functions, and if
|
||||
the object is a closure containing an async function we need to work
|
||||
carefully.)
|
||||
"""
|
||||
from pyscript import config
|
||||
|
||||
if config["type"] == "mpy": # Is MicroPython?
|
||||
# MicroPython doesn't appear to have a way to determine if a closure is
|
||||
# an async function except via the repr. This is a bit hacky.
|
||||
if "<closure <generator>" in repr(obj):
|
||||
return True
|
||||
return inspect.isgeneratorfunction(obj)
|
||||
|
||||
return inspect.iscoroutinefunction(obj)
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
|
||||
# `when` is not used in this module. It is imported here save the user an additional
|
||||
# import (i.e. they can get what they need from `pyscript.web`).
|
||||
from pyscript import document, when # NOQA
|
||||
from pyscript import document, when, Event # NOQA
|
||||
from pyscript.ffi import create_proxy
|
||||
|
||||
|
||||
def wrap_dom_element(dom_element):
|
||||
@@ -68,6 +69,18 @@ class Element:
|
||||
type(self).get_tag_name()
|
||||
)
|
||||
|
||||
# HTML on_events attached to the element become pyscript.Event instances.
|
||||
self._on_events = {}
|
||||
|
||||
# Handle kwargs for handling named events with a default handler function.
|
||||
properties = {}
|
||||
for name, handler in kwargs.items():
|
||||
if name.startswith("on_"):
|
||||
ev = self.get_event(name) # Create the default Event instance.
|
||||
ev.add_listener(handler)
|
||||
else:
|
||||
properties[name] = handler
|
||||
|
||||
# A set-like interface to the element's `classList`.
|
||||
self._classes = Classes(self)
|
||||
|
||||
@@ -75,7 +88,7 @@ class Element:
|
||||
self._style = Style(self)
|
||||
|
||||
# Set any specified classes, styles, and DOM properties.
|
||||
self.update(classes=classes, style=style, **kwargs)
|
||||
self.update(classes=classes, style=style, **properties)
|
||||
|
||||
def __eq__(self, obj):
|
||||
"""Check for equality by comparing the underlying DOM element."""
|
||||
@@ -93,13 +106,21 @@ class Element:
|
||||
return self.find(key)
|
||||
|
||||
def __getattr__(self, name):
|
||||
"""
|
||||
Get an attribute from the element.
|
||||
|
||||
If the attribute is an event (e.g. "on_click"), we wrap it in an `Event`
|
||||
instance and return that. Otherwise, we return the attribute from the
|
||||
underlying DOM element.
|
||||
"""
|
||||
if name.startswith("on_"):
|
||||
return self.get_event(name)
|
||||
# This allows us to get attributes on the underlying DOM element that clash
|
||||
# with Python keywords or built-ins (e.g. the output element has an
|
||||
# attribute `for` which is a Python keyword, so you can access it on the
|
||||
# Element instance via `for_`).
|
||||
if name.endswith("_"):
|
||||
name = name[:-1]
|
||||
|
||||
return getattr(self._dom_element, name)
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
@@ -119,8 +140,33 @@ class Element:
|
||||
if name.endswith("_"):
|
||||
name = name[:-1]
|
||||
|
||||
if name.startswith("on_"):
|
||||
# Ensure on-events are cached in the _on_events dict if the
|
||||
# user is setting them directly.
|
||||
self._on_events[name] = value
|
||||
|
||||
setattr(self._dom_element, name, value)
|
||||
|
||||
def get_event(self, name):
|
||||
"""
|
||||
Get an `Event` instance for the specified event name.
|
||||
"""
|
||||
if not name.startswith("on_"):
|
||||
raise ValueError("Event names must start with 'on_'.")
|
||||
event_name = name[3:] # Remove the "on_" prefix.
|
||||
if not hasattr(self._dom_element, event_name):
|
||||
raise ValueError(f"Element has no '{event_name}' event.")
|
||||
if name in self._on_events:
|
||||
return self._on_events[name]
|
||||
# Such an on-event exists in the DOM element, but we haven't yet
|
||||
# wrapped it in an Event instance. Let's do that now. When the
|
||||
# underlying DOM element's event is triggered, the Event instance
|
||||
# will be triggered too.
|
||||
ev = Event()
|
||||
self._on_events[name] = ev
|
||||
self._dom_element.addEventListener(event_name, create_proxy(ev.trigger))
|
||||
return ev
|
||||
|
||||
@property
|
||||
def children(self):
|
||||
"""Return the element's children as an `ElementCollection`."""
|
||||
|
||||
File diff suppressed because one or more lines are too long
14
core/tests/javascript/mpy-error.html
Normal file
14
core/tests/javascript/mpy-error.html
Normal file
@@ -0,0 +1,14 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<script type="module" src="../../dist/core.js"></script>
|
||||
<script type="mpy">
|
||||
from pyscript import document
|
||||
import sys
|
||||
print("This is an error", file=sys.stderr)
|
||||
document.documentElement.classList.add("ok");
|
||||
</script>
|
||||
</head>
|
||||
</html>
|
||||
14
core/tests/javascript/mpy-no-error.html
Normal file
14
core/tests/javascript/mpy-no-error.html
Normal file
@@ -0,0 +1,14 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<script type="module" src="../../dist/core.js"></script>
|
||||
<script type="mpy" config="mpy-no-error.toml">
|
||||
from pyscript import document
|
||||
import sys
|
||||
print("This is an error", file=sys.stderr)
|
||||
document.documentElement.classList.add("ok");
|
||||
</script>
|
||||
</head>
|
||||
</html>
|
||||
1
core/tests/javascript/mpy-no-error.toml
Normal file
1
core/tests/javascript/mpy-no-error.toml
Normal file
@@ -0,0 +1 @@
|
||||
plugins = ["!error"]
|
||||
1
core/tests/javascript/pyodide-cache/cached.toml
Normal file
1
core/tests/javascript/pyodide-cache/cached.toml
Normal file
@@ -0,0 +1 @@
|
||||
packages = ["numpy", "matplotlib"]
|
||||
41
core/tests/javascript/pyodide-cache/index.html
Normal file
41
core/tests/javascript/pyodide-cache/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">
|
||||
import '../../../dist/core.js';
|
||||
|
||||
addEventListener('py:progress', ({ detail }) => {
|
||||
console.log(detail);
|
||||
});
|
||||
|
||||
test([]);
|
||||
|
||||
function test($) {
|
||||
const script = document.createElement('script');
|
||||
script.type = 'py';
|
||||
script.src = 'index.py';
|
||||
script.toggleAttribute('worker', true);
|
||||
script.setAttribute('config', `${$.length ? 'cached' : 'never'}.toml`);
|
||||
script.addEventListener(
|
||||
'py:done',
|
||||
() => {
|
||||
if ($.push(performance.now() - time) === 3) {
|
||||
const [_, justCDN, lockFile] = $;
|
||||
console.log({ justCDN, lockFile });
|
||||
document.body.textContent = justCDN > lockFile ? 'OK' : 'NO';
|
||||
document.documentElement.classList.add('done');
|
||||
}
|
||||
else setTimeout(test, 0, $);
|
||||
script.remove();
|
||||
},
|
||||
{ once: true },
|
||||
);
|
||||
document.body.append(script);
|
||||
const time = performance.now();
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
</html>
|
||||
6
core/tests/javascript/pyodide-cache/index.py
Normal file
6
core/tests/javascript/pyodide-cache/index.py
Normal file
@@ -0,0 +1,6 @@
|
||||
import numpy
|
||||
import matplotlib
|
||||
|
||||
# just do something with the packages
|
||||
print(len(dir(numpy)))
|
||||
print(len(dir(matplotlib)))
|
||||
2
core/tests/javascript/pyodide-cache/never.toml
Normal file
2
core/tests/javascript/pyodide-cache/never.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
packages_cache = "never"
|
||||
packages = ["numpy", "matplotlib"]
|
||||
17
core/tests/javascript/pyodide-lockfile/index.html
Normal file
17
core/tests/javascript/pyodide-lockfile/index.html
Normal file
@@ -0,0 +1,17 @@
|
||||
<!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>
|
||||
<script type="py" config='{"packages":["jsonpointer==3.0.0"]}'>
|
||||
import jsonpointer
|
||||
from pyscript import document
|
||||
document.documentElement.classList.add("done")
|
||||
document.body.append("OK")
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
18
core/tests/javascript/workers/create_named/index.html
Normal file
18
core/tests/javascript/workers/create_named/index.html
Normal file
@@ -0,0 +1,18 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>mpy using py named worker</title>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1.0">
|
||||
<script type="module" src="../../../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script type="mpy" config="../config.toml">
|
||||
from pyscript import create_named_worker
|
||||
await create_named_worker("../worker.py", name="pyodide_version", type="py")
|
||||
|
||||
from test import test
|
||||
await test("pyodide_version")
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,30 +0,0 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1.0">
|
||||
<script type="module" src="../../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script type="mpy" async>
|
||||
from pyscript import create_named_worker
|
||||
|
||||
await create_named_worker("./worker.py", name="micropython_version", type="mpy")
|
||||
</script>
|
||||
<script type="mpy" config="./config.toml" async>
|
||||
from test import test
|
||||
await test("mpy")
|
||||
</script>
|
||||
<script type="py" config="./config.toml" async>
|
||||
from test import test
|
||||
await test("py")
|
||||
</script>
|
||||
<script type="py" name="pyodide_version" worker>
|
||||
def pyodide_version():
|
||||
import sys
|
||||
return sys.version
|
||||
|
||||
__export__ = ['pyodide_version']
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
16
core/tests/javascript/workers/mpy/index.html
Normal file
16
core/tests/javascript/workers/mpy/index.html
Normal file
@@ -0,0 +1,16 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>mpy using py named worker</title>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1.0">
|
||||
<script type="module" src="../../../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script type="mpy" config="../config.toml">
|
||||
from test import test
|
||||
await test("pyodide_version")
|
||||
</script>
|
||||
<script type="py" src="../worker.py" name="pyodide_version" worker></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,29 +0,0 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
|
||||
<title>named workers</title>
|
||||
<script type="module" src="../../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script type="mpy" async>
|
||||
from pyscript import workers
|
||||
|
||||
await (await workers["mpy"]).greetings()
|
||||
await (await workers["py"]).greetings()
|
||||
</script>
|
||||
<script type="mpy" worker name="mpy">
|
||||
def greetings():
|
||||
print("micropython")
|
||||
|
||||
__export__ = ['greetings']
|
||||
</script>
|
||||
<script type="py" worker name="py">
|
||||
def greetings():
|
||||
print("pyodide")
|
||||
|
||||
__export__ = ['greetings']
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
16
core/tests/javascript/workers/py/index.html
Normal file
16
core/tests/javascript/workers/py/index.html
Normal file
@@ -0,0 +1,16 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>py using mpy named worker</title>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1.0">
|
||||
<script type="module" src="../../../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script type="py" config="../config.toml">
|
||||
from test import test
|
||||
await test("micropython_version")
|
||||
</script>
|
||||
<script type="mpy" src="../worker.py" name="micropython_version" worker></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,19 +1,13 @@
|
||||
from pyscript import document, workers
|
||||
|
||||
|
||||
async def test(interpreter):
|
||||
# accessed as item
|
||||
named = await workers.micropython_version
|
||||
async def test(name):
|
||||
# retrieve sync utilities from the named worker
|
||||
named = await workers[name]
|
||||
|
||||
version = await named.micropython_version()
|
||||
# invoke the runtime_version __export__ + show it
|
||||
version = await named.runtime_version()
|
||||
document.body.append(version)
|
||||
document.body.append(document.createElement("hr"))
|
||||
|
||||
# accessed as attribute
|
||||
named = await workers["pyodide_version"]
|
||||
|
||||
version = await named.pyodide_version()
|
||||
document.body.append(version)
|
||||
document.body.append(document.createElement("hr"))
|
||||
|
||||
document.documentElement.classList.add(interpreter)
|
||||
# flag the expectations around name done
|
||||
document.documentElement.classList.add(name)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
def micropython_version():
|
||||
def runtime_version():
|
||||
import sys
|
||||
|
||||
return sys.version
|
||||
|
||||
|
||||
__export__ = ["micropython_version"]
|
||||
__export__ = ['runtime_version']
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
<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>
|
||||
<link rel="stylesheet" href="../../../dist/core.css">
|
||||
<script type="module" src="../../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script type="mpy" worker>
|
||||
@@ -14,7 +14,7 @@ serve({
|
||||
const url = new URL(req.url);
|
||||
let { pathname } = url;
|
||||
if (pathname === '/') pathname = '/index.html';
|
||||
else if (/^\/dist\//.test(pathname)) pathname = `/../..${pathname}`;
|
||||
else if (/^\/dist\//.test(pathname)) pathname = `/../../..${pathname}`;
|
||||
else if (pathname === '/favicon.ico')
|
||||
return new Response('Not Found', { status: 404 });
|
||||
const response = new Response(file(`${dir}${pathname}`));
|
||||
@@ -95,9 +95,19 @@ test('MicroPython + JS Storage', async ({ page }) => {
|
||||
await page.waitForSelector('html.ok');
|
||||
});
|
||||
|
||||
test('MicroPython + workers', async ({ page }) => {
|
||||
await page.goto('http://localhost:8080/tests/javascript/workers/index.html');
|
||||
await page.waitForSelector('html.mpy.py');
|
||||
test('MicroPython using named Pyodide Worker', async ({ page }) => {
|
||||
await page.goto('http://localhost:8080/tests/javascript/workers/mpy/index.html');
|
||||
await page.waitForSelector('html.pyodide_version');
|
||||
});
|
||||
|
||||
test('MicroPython creating a named Pyodide Worker', async ({ page }) => {
|
||||
await page.goto('http://localhost:8080/tests/javascript/workers/create_named/index.html');
|
||||
await page.waitForSelector('html.pyodide_version');
|
||||
});
|
||||
|
||||
test('Pyodide using named MicroPython Worker', async ({ page }) => {
|
||||
await page.goto('http://localhost:8080/tests/javascript/workers/py/index.html');
|
||||
await page.waitForSelector('html.micropython_version');
|
||||
});
|
||||
|
||||
test('MicroPython Editor setup error', async ({ page }) => {
|
||||
@@ -121,3 +131,43 @@ test('Py and Mpy config["type"]', async ({ page }) => {
|
||||
await page.goto('http://localhost:8080/tests/javascript/config_type.html');
|
||||
await page.waitForSelector('html.mpy.py');
|
||||
});
|
||||
|
||||
test('Pyodide lockFileURL vs CDN', async ({ page }) => {
|
||||
await page.goto('http://localhost:8080/tests/javascript/pyodide-cache/');
|
||||
await page.waitForSelector('html.done');
|
||||
const body = await page.evaluate(() => document.body.textContent);
|
||||
await expect(body).toBe('OK');
|
||||
});
|
||||
|
||||
test('Pyodide pinned lockFileURL', async ({ page }) => {
|
||||
const logs = [];
|
||||
page.on('console', msg => {
|
||||
const text = msg.text();
|
||||
if (!text.startsWith('['))
|
||||
logs.push(text);
|
||||
});
|
||||
await page.goto('http://localhost:8080/tests/javascript/pyodide-lockfile/');
|
||||
await page.waitForSelector('html.done');
|
||||
let body = await page.evaluate(() => document.body.lastChild.textContent);
|
||||
await expect(body).toBe('OK');
|
||||
await expect(!!logs.splice(0).length).toBe(true);
|
||||
await page.reload();
|
||||
await page.waitForSelector('html.done');
|
||||
body = await page.evaluate(() => document.body.lastChild.textContent);
|
||||
await expect(body).toBe('OK');
|
||||
await expect(logs.splice(0).length).toBe(0);
|
||||
});
|
||||
|
||||
test('MicroPython buffered error', async ({ page }) => {
|
||||
await page.goto('http://localhost:8080/tests/javascript/mpy-error.html');
|
||||
await page.waitForSelector('html.ok');
|
||||
const body = await page.evaluate(() => document.body.textContent.trim());
|
||||
await expect(body).toBe('This is an error');
|
||||
});
|
||||
|
||||
test('MicroPython buffered NO error', async ({ page }) => {
|
||||
await page.goto('http://localhost:8080/tests/javascript/mpy-no-error.html');
|
||||
await page.waitForSelector('html.ok');
|
||||
const body = await page.evaluate(() => document.body.textContent.trim());
|
||||
await expect(body).toBe('');
|
||||
});
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
<script type="module" src="../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script type="py" src="camera.py" async></script>
|
||||
<script type="mpy" src="camera.py" async></script>
|
||||
|
||||
<label for="cars">Choose a device:</label>
|
||||
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
from pyodide.ffi import create_proxy
|
||||
from pyscript import display, document, when, window
|
||||
from pyweb import media, pydom
|
||||
from pyscript import display, document, media, when, window
|
||||
from pyscript.web import page
|
||||
|
||||
devicesSelect = pydom["#devices"][0]
|
||||
video = pydom["video"][0]
|
||||
devicesSelect = page["#devices"][0]
|
||||
video = page["video"][0]
|
||||
devices = {}
|
||||
|
||||
|
||||
@@ -20,7 +19,7 @@ async def list_media_devices(event=None):
|
||||
async def connect_to_device(e):
|
||||
"""Connect to the selected device."""
|
||||
device = devices[devicesSelect.value]
|
||||
video._js.srcObject = await device.get_stream()
|
||||
video.srcObject = await device.get_stream()
|
||||
|
||||
|
||||
@when("click", "#snap")
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
document.body.innerHTML += `<p>${message}</p>`;
|
||||
});
|
||||
</script>
|
||||
<link rel="stylesheet" href="../../dist/core.css">
|
||||
<script type="module" src="../../dist/core.js"></script>
|
||||
<link rel="stylesheet" href="../../../dist/core.css">
|
||||
<script type="module" src="../../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<py-config>name = "first"</py-config>
|
||||
@@ -9,8 +9,8 @@
|
||||
document.body.innerHTML += `<p>${message}</p>`;
|
||||
});
|
||||
</script>
|
||||
<link rel="stylesheet" href="../../dist/core.css">
|
||||
<script type="module" src="../../dist/core.js"></script>
|
||||
<link rel="stylesheet" href="../../../dist/core.css">
|
||||
<script type="module" src="../../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
OK
|
||||
@@ -9,8 +9,8 @@
|
||||
document.body.innerHTML += `<p>${message}</p>`;
|
||||
});
|
||||
</script>
|
||||
<link rel="stylesheet" href="../../dist/core.css">
|
||||
<script type="module" src="../../dist/core.js"></script>
|
||||
<link rel="stylesheet" href="../../../dist/core.css">
|
||||
<script type="module" src="../../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script type="py" config="first.toml"></script>
|
||||
@@ -9,8 +9,8 @@
|
||||
document.body.innerHTML += `<p>${message}</p>`;
|
||||
});
|
||||
</script>
|
||||
<link rel="stylesheet" href="../../dist/core.css">
|
||||
<script type="module" src="../../dist/core.js"></script>
|
||||
<link rel="stylesheet" href="../../../dist/core.css">
|
||||
<script type="module" src="../../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<py-config>name = "first"</py-config>
|
||||
15
core/tests/manual/donkey/index.html
Normal file
15
core/tests/manual/donkey/index.html
Normal file
@@ -0,0 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<link rel="stylesheet" href="../../../dist/core.css" />
|
||||
<script type="module" src="./index.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="container"></div>
|
||||
<button id="run" disabled>Run</button>
|
||||
<button id="clear" disabled>Clear</button>
|
||||
<button id="kill" disabled>Kill</button>
|
||||
</body>
|
||||
</html>
|
||||
43
core/tests/manual/donkey/index.js
Normal file
43
core/tests/manual/donkey/index.js
Normal file
@@ -0,0 +1,43 @@
|
||||
import { donkey } from '../../../dist/core.js';
|
||||
|
||||
const runButton = document.querySelector('#run');
|
||||
const clearButton = document.querySelector('#clear');
|
||||
const killButton = document.querySelector('#kill');
|
||||
|
||||
const {
|
||||
execute, // exec(expression)
|
||||
evaluate, // eval(expression)
|
||||
process, // process(code)
|
||||
clear,
|
||||
kill,
|
||||
} = await donkey({ terminal: '#container' });
|
||||
|
||||
clearButton.onclick = async () => {
|
||||
killButton.disabled = true;
|
||||
clearButton.disabled = true;
|
||||
await clear();
|
||||
runButton.disabled = false;
|
||||
};
|
||||
killButton.onclick = () => {
|
||||
killButton.disabled = true;
|
||||
clearButton.disabled = true;
|
||||
runButton.disabled = true;
|
||||
kill();
|
||||
};
|
||||
|
||||
runButton.disabled = false;
|
||||
runButton.onclick = async () => {
|
||||
killButton.disabled = false;
|
||||
clearButton.disabled = false;
|
||||
runButton.disabled = true;
|
||||
// multiline code
|
||||
await execute(`
|
||||
a = 1 + 2
|
||||
print(f'1 + 2 = {a}')
|
||||
`);
|
||||
// single expression evaluation
|
||||
const name = await evaluate('input("what is your name? ")');
|
||||
alert(`Hello ${name}`);
|
||||
killButton.disabled = true;
|
||||
runButton.disabled = false;
|
||||
};
|
||||
13
core/tests/manual/emoji.html
Normal file
13
core/tests/manual/emoji.html
Normal file
@@ -0,0 +1,13 @@
|
||||
<!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>
|
||||
<script type="mpy" src="emoji.py" terminal worker></script>
|
||||
<script type="py" src="emoji.py" terminal worker></script>
|
||||
</body>
|
||||
</html>
|
||||
17
core/tests/manual/emoji.py
Normal file
17
core/tests/manual/emoji.py
Normal file
@@ -0,0 +1,17 @@
|
||||
import sys
|
||||
|
||||
print(sys.version)
|
||||
RED = chr(0x1F534) # LARGE RED CIRCLE
|
||||
GREEN = chr(0x1F7E2) # LARGE GREEN CIRCLE
|
||||
MOUSE = chr(0x1F42D) # MOUSE FACE
|
||||
EARTH = chr(0x1F30E) # EARTH GLOBE AMERICAS
|
||||
FACE = chr(0x1F610) # NEUTRAL FACE
|
||||
BASMALA = chr(0xFDFD) # ARABIC LIGATURE BISMILLAH AR-RAHMAN AR-RAHEEM
|
||||
|
||||
print("[", RED, "]")
|
||||
print("[", MOUSE, "]")
|
||||
print("[", EARTH, "]")
|
||||
print("[", FACE, "]")
|
||||
print("[", FACE * 3, "]")
|
||||
print("[", BASMALA, "]")
|
||||
print("[", BASMALA + GREEN, "]")
|
||||
39
core/tests/manual/error/index.html
Normal file
39
core/tests/manual/error/index.html
Normal file
@@ -0,0 +1,39 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>PyScript Error Bug?</title>
|
||||
<link rel="stylesheet" href="../../../dist/core.css">
|
||||
<script type="module" src="../../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<py-config>
|
||||
plugins = ["!error"]
|
||||
</py-config>
|
||||
<script type="py">
|
||||
import sys
|
||||
print("This is normal content")
|
||||
print("This is error content", file=sys.stderr)
|
||||
</script>
|
||||
<!-- Attempt 2; inline config
|
||||
<script type="py" config='plugins=["!error"]'>
|
||||
import sys
|
||||
print("This is normal content")
|
||||
print("This is error content", file=sys.stderr)
|
||||
</script> -->
|
||||
<!-- Attempt 3; external pyscript.toml
|
||||
<script type="py" config="pyscript.toml">
|
||||
import sys
|
||||
print("This is normal content")
|
||||
print("This is error content", file=sys.stderr)
|
||||
</script> -->
|
||||
<!-- Attempt 4; micropython
|
||||
<script type="mpy">
|
||||
import sys
|
||||
print("This is normal content")
|
||||
print("This is error content", file=sys.stderr)
|
||||
</script> -->
|
||||
<div id="result"></div>
|
||||
</body>
|
||||
</html>
|
||||
1
core/tests/manual/error/pyscript.toml
Normal file
1
core/tests/manual/error/pyscript.toml
Normal file
@@ -0,0 +1 @@
|
||||
plugins = ["!error"]
|
||||
30
core/tests/manual/game/aliens.css
Normal file
30
core/tests/manual/game/aliens.css
Normal file
@@ -0,0 +1,30 @@
|
||||
/* (c) https://github.com/ryanking13/pyodide-pygame-demo/blob/main/examples/aliens.html */
|
||||
body {
|
||||
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
background-color: #f4f4f4;
|
||||
color: #333;
|
||||
}
|
||||
.demo {
|
||||
background-color: #fff;
|
||||
margin: 20px auto;
|
||||
max-width: 1000px;
|
||||
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
}
|
||||
.demo-header {
|
||||
background-color: #007bff;
|
||||
color: #fff;
|
||||
padding: 15px 20px;
|
||||
font-size: 20px;
|
||||
}
|
||||
.demo-content {
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
#canvas {
|
||||
margin: 0 auto;
|
||||
display: block;
|
||||
}
|
||||
394
core/tests/manual/game/aliens.py
Normal file
394
core/tests/manual/game/aliens.py
Normal file
@@ -0,0 +1,394 @@
|
||||
""" (c) https://github.com/ryanking13/pyodide-pygame-demo/blob/main/examples/aliens.html
|
||||
pygame.examples.aliens
|
||||
|
||||
Shows a mini game where you have to defend against aliens.
|
||||
|
||||
What does it show you about pygame?
|
||||
|
||||
* pygame.sprite, the difference between Sprite and Group.
|
||||
* dirty rectangle optimization for processing for speed.
|
||||
* music with pygame.mixer.music, including fadeout
|
||||
* sound effects with pygame.Sound
|
||||
* event processing, keyboard handling, QUIT handling.
|
||||
* a main loop frame limited with a game clock from the pygame.time module
|
||||
* fullscreen switching.
|
||||
|
||||
|
||||
Controls
|
||||
--------
|
||||
|
||||
* Left and right arrows to move.
|
||||
* Space bar to shoot.
|
||||
* f key to toggle between fullscreen.
|
||||
|
||||
"""
|
||||
import asyncio
|
||||
import random
|
||||
import os
|
||||
import pathlib
|
||||
|
||||
# import basic pygame modules
|
||||
import pygame
|
||||
|
||||
# see if we can load more than standard BMP
|
||||
if not pygame.image.get_extended():
|
||||
raise SystemExit("Sorry, extended image module required")
|
||||
|
||||
|
||||
# game constants
|
||||
MAX_SHOTS = 2 # most player bullets onscreen
|
||||
ALIEN_ODDS = 22 # chances a new alien appears
|
||||
BOMB_ODDS = 60 # chances a new bomb will drop
|
||||
ALIEN_RELOAD = 12 # frames between new aliens
|
||||
SCREENRECT = pygame.Rect(0, 0, 640, 480)
|
||||
SCORE = 0
|
||||
|
||||
|
||||
main_dir = str(pathlib.Path(pygame.__file__).parent / "examples")
|
||||
|
||||
def load_image(file):
|
||||
"""loads an image, prepares it for play"""
|
||||
file = os.path.join(main_dir, "data", file)
|
||||
try:
|
||||
surface = pygame.image.load(file)
|
||||
except pygame.error:
|
||||
raise SystemExit(f'Could not load image "{file}" {pygame.get_error()}')
|
||||
return surface.convert()
|
||||
|
||||
|
||||
def load_sound(file):
|
||||
"""because pygame can be be compiled without mixer."""
|
||||
if not pygame.mixer:
|
||||
return None
|
||||
file = os.path.join(main_dir, "data", file)
|
||||
try:
|
||||
sound = pygame.mixer.Sound(file)
|
||||
return sound
|
||||
except pygame.error:
|
||||
print(f"Warning, unable to load, {file}")
|
||||
return None
|
||||
|
||||
|
||||
# Each type of game object gets an init and an update function.
|
||||
# The update function is called once per frame, and it is when each object should
|
||||
# change its current position and state.
|
||||
#
|
||||
# The Player object actually gets a "move" function instead of update,
|
||||
# since it is passed extra information about the keyboard.
|
||||
|
||||
|
||||
class Player(pygame.sprite.Sprite):
|
||||
"""Representing the player as a moon buggy type car."""
|
||||
|
||||
speed = 10
|
||||
bounce = 24
|
||||
gun_offset = -11
|
||||
images = []
|
||||
|
||||
def __init__(self):
|
||||
pygame.sprite.Sprite.__init__(self, self.containers)
|
||||
self.image = self.images[0]
|
||||
self.rect = self.image.get_rect(midbottom=SCREENRECT.midbottom)
|
||||
self.reloading = False
|
||||
self.origtop = self.rect.top
|
||||
self.facing = -1
|
||||
|
||||
def move(self, direction):
|
||||
if direction:
|
||||
self.facing = direction
|
||||
self.rect.move_ip(direction * self.speed, 0)
|
||||
self.rect = self.rect.clamp(SCREENRECT)
|
||||
if direction < 0:
|
||||
self.image = self.images[0]
|
||||
elif direction > 0:
|
||||
self.image = self.images[1]
|
||||
self.rect.top = self.origtop - (self.rect.left // self.bounce % 2)
|
||||
|
||||
def gunpos(self):
|
||||
pos = self.facing * self.gun_offset + self.rect.centerx
|
||||
return pos, self.rect.top
|
||||
|
||||
|
||||
class Alien(pygame.sprite.Sprite):
|
||||
"""An alien space ship. That slowly moves down the screen."""
|
||||
|
||||
speed = 13
|
||||
animcycle = 12
|
||||
images = []
|
||||
|
||||
def __init__(self):
|
||||
pygame.sprite.Sprite.__init__(self, self.containers)
|
||||
self.image = self.images[0]
|
||||
self.rect = self.image.get_rect()
|
||||
self.facing = random.choice((-1, 1)) * Alien.speed
|
||||
self.frame = 0
|
||||
if self.facing < 0:
|
||||
self.rect.right = SCREENRECT.right
|
||||
|
||||
def update(self):
|
||||
self.rect.move_ip(self.facing, 0)
|
||||
if not SCREENRECT.contains(self.rect):
|
||||
self.facing = -self.facing
|
||||
self.rect.top = self.rect.bottom + 1
|
||||
self.rect = self.rect.clamp(SCREENRECT)
|
||||
self.frame = self.frame + 1
|
||||
self.image = self.images[self.frame // self.animcycle % 3]
|
||||
|
||||
|
||||
class Explosion(pygame.sprite.Sprite):
|
||||
"""An explosion. Hopefully the Alien and not the player!"""
|
||||
|
||||
defaultlife = 12
|
||||
animcycle = 3
|
||||
images = []
|
||||
|
||||
def __init__(self, actor):
|
||||
pygame.sprite.Sprite.__init__(self, self.containers)
|
||||
self.image = self.images[0]
|
||||
self.rect = self.image.get_rect(center=actor.rect.center)
|
||||
self.life = self.defaultlife
|
||||
|
||||
def update(self):
|
||||
"""called every time around the game loop.
|
||||
|
||||
Show the explosion surface for 'defaultlife'.
|
||||
Every game tick(update), we decrease the 'life'.
|
||||
|
||||
Also we animate the explosion.
|
||||
"""
|
||||
self.life = self.life - 1
|
||||
self.image = self.images[self.life // self.animcycle % 2]
|
||||
if self.life <= 0:
|
||||
self.kill()
|
||||
|
||||
|
||||
class Shot(pygame.sprite.Sprite):
|
||||
"""a bullet the Player sprite fires."""
|
||||
|
||||
speed = -11
|
||||
images = []
|
||||
|
||||
def __init__(self, pos):
|
||||
pygame.sprite.Sprite.__init__(self, self.containers)
|
||||
self.image = self.images[0]
|
||||
self.rect = self.image.get_rect(midbottom=pos)
|
||||
|
||||
def update(self):
|
||||
"""called every time around the game loop.
|
||||
|
||||
Every tick we move the shot upwards.
|
||||
"""
|
||||
self.rect.move_ip(0, self.speed)
|
||||
if self.rect.top <= 0:
|
||||
self.kill()
|
||||
|
||||
|
||||
class Bomb(pygame.sprite.Sprite):
|
||||
"""A bomb the aliens drop."""
|
||||
|
||||
speed = 9
|
||||
images = []
|
||||
|
||||
def __init__(self, alien):
|
||||
pygame.sprite.Sprite.__init__(self, self.containers)
|
||||
self.image = self.images[0]
|
||||
self.rect = self.image.get_rect(midbottom=alien.rect.move(0, 5).midbottom)
|
||||
|
||||
def update(self):
|
||||
"""called every time around the game loop.
|
||||
|
||||
Every frame we move the sprite 'rect' down.
|
||||
When it reaches the bottom we:
|
||||
|
||||
- make an explosion.
|
||||
- remove the Bomb.
|
||||
"""
|
||||
self.rect.move_ip(0, self.speed)
|
||||
if self.rect.bottom >= 470:
|
||||
Explosion(self)
|
||||
self.kill()
|
||||
|
||||
|
||||
class Score(pygame.sprite.Sprite):
|
||||
"""to keep track of the score."""
|
||||
|
||||
def __init__(self):
|
||||
pygame.sprite.Sprite.__init__(self)
|
||||
self.font = pygame.Font(None, 20)
|
||||
self.font.set_italic(1)
|
||||
self.color = "white"
|
||||
self.lastscore = -1
|
||||
self.update()
|
||||
self.rect = self.image.get_rect().move(10, 450)
|
||||
|
||||
def update(self):
|
||||
"""We only update the score in update() when it has changed."""
|
||||
if SCORE != self.lastscore:
|
||||
self.lastscore = SCORE
|
||||
msg = "Score: %d" % SCORE
|
||||
self.image = self.font.render(msg, 0, self.color)
|
||||
|
||||
|
||||
async def main(winstyle=0):
|
||||
# Initialize pygame
|
||||
pygame.mixer.pre_init(44100, 32, 2, 1024)
|
||||
pygame.init()
|
||||
if pygame.mixer and not pygame.mixer.get_init():
|
||||
print("Warning, no sound")
|
||||
pygame.mixer = None
|
||||
|
||||
fullscreen = False
|
||||
# Set the display mode
|
||||
winstyle = 0 # |FULLSCREEN
|
||||
screen = pygame.display.set_mode(SCREENRECT.size, winstyle)
|
||||
|
||||
# Load images, assign to sprite classes
|
||||
# (do this before the classes are used, after screen setup)
|
||||
img = load_image("player1.gif")
|
||||
Player.images = [img, pygame.transform.flip(img, 1, 0)]
|
||||
img = load_image("explosion1.gif")
|
||||
Explosion.images = [img, pygame.transform.flip(img, 1, 1)]
|
||||
Alien.images = [load_image(im) for im in ("alien1.gif", "alien2.gif", "alien3.gif")]
|
||||
Bomb.images = [load_image("bomb.gif")]
|
||||
Shot.images = [load_image("shot.gif")]
|
||||
|
||||
# decorate the game window
|
||||
icon = pygame.transform.scale(Alien.images[0], (32, 32))
|
||||
pygame.display.set_icon(icon)
|
||||
pygame.display.set_caption("Pygame Aliens")
|
||||
pygame.mouse.set_visible(0)
|
||||
|
||||
# create the background, tile the bgd image
|
||||
bgdtile = load_image("background.gif")
|
||||
background = pygame.Surface(SCREENRECT.size)
|
||||
for x in range(0, SCREENRECT.width, bgdtile.get_width()):
|
||||
background.blit(bgdtile, (x, 0))
|
||||
screen.blit(background, (0, 0))
|
||||
pygame.display.flip()
|
||||
|
||||
# load the sound effects
|
||||
boom_sound = load_sound("boom.wav")
|
||||
shoot_sound = load_sound("car_door.wav")
|
||||
if pygame.mixer:
|
||||
music = os.path.join(main_dir, "data", "house_lo.wav")
|
||||
pygame.mixer.music.load(music)
|
||||
pygame.mixer.music.play(-1)
|
||||
|
||||
# Initialize Game Groups
|
||||
aliens = pygame.sprite.Group()
|
||||
shots = pygame.sprite.Group()
|
||||
bombs = pygame.sprite.Group()
|
||||
all = pygame.sprite.RenderUpdates()
|
||||
lastalien = pygame.sprite.GroupSingle()
|
||||
|
||||
# assign default groups to each sprite class
|
||||
Player.containers = all
|
||||
Alien.containers = aliens, all, lastalien
|
||||
Shot.containers = shots, all
|
||||
Bomb.containers = bombs, all
|
||||
Explosion.containers = all
|
||||
Score.containers = all
|
||||
|
||||
# Create Some Starting Values
|
||||
global score
|
||||
alienreload = ALIEN_RELOAD
|
||||
clock = pygame.Clock()
|
||||
|
||||
# initialize our starting sprites
|
||||
global SCORE
|
||||
player = Player()
|
||||
Alien() # note, this 'lives' because it goes into a sprite group
|
||||
if pygame.font:
|
||||
all.add(Score())
|
||||
|
||||
# Run our main loop whilst the player is alive.
|
||||
while player.alive():
|
||||
# get input
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
return
|
||||
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
|
||||
return
|
||||
elif event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_f:
|
||||
if not fullscreen:
|
||||
print("Changing to FULLSCREEN")
|
||||
screen_backup = screen.copy()
|
||||
screen = pygame.display.set_mode(
|
||||
SCREENRECT.size, winstyle | pygame.FULLSCREEN, bestdepth
|
||||
)
|
||||
screen.blit(screen_backup, (0, 0))
|
||||
else:
|
||||
print("Changing to windowed mode")
|
||||
screen_backup = screen.copy()
|
||||
screen = pygame.display.set_mode(
|
||||
SCREENRECT.size, winstyle, bestdepth
|
||||
)
|
||||
screen.blit(screen_backup, (0, 0))
|
||||
pygame.display.flip()
|
||||
fullscreen = not fullscreen
|
||||
|
||||
keystate = pygame.key.get_pressed()
|
||||
|
||||
# clear/erase the last drawn sprites
|
||||
all.clear(screen, background)
|
||||
|
||||
# update all the sprites
|
||||
all.update()
|
||||
|
||||
# handle player input
|
||||
direction = keystate[pygame.K_RIGHT] - keystate[pygame.K_LEFT]
|
||||
player.move(direction)
|
||||
firing = keystate[pygame.K_SPACE]
|
||||
if not player.reloading and firing and len(shots) < MAX_SHOTS:
|
||||
Shot(player.gunpos())
|
||||
if pygame.mixer:
|
||||
shoot_sound.play()
|
||||
player.reloading = firing
|
||||
|
||||
# Create new alien
|
||||
if alienreload:
|
||||
alienreload = alienreload - 1
|
||||
elif not int(random.random() * ALIEN_ODDS):
|
||||
Alien()
|
||||
alienreload = ALIEN_RELOAD
|
||||
|
||||
# Drop bombs
|
||||
if lastalien and not int(random.random() * BOMB_ODDS):
|
||||
Bomb(lastalien.sprite)
|
||||
|
||||
# Detect collisions between aliens and players.
|
||||
for alien in pygame.sprite.spritecollide(player, aliens, 1):
|
||||
if pygame.mixer:
|
||||
boom_sound.play()
|
||||
Explosion(alien)
|
||||
Explosion(player)
|
||||
SCORE = SCORE + 1
|
||||
player.kill()
|
||||
|
||||
# See if shots hit the aliens.
|
||||
for alien in pygame.sprite.groupcollide(aliens, shots, 1, 1).keys():
|
||||
if pygame.mixer:
|
||||
boom_sound.play()
|
||||
Explosion(alien)
|
||||
SCORE = SCORE + 1
|
||||
|
||||
# See if alien bombs hit the player.
|
||||
for bomb in pygame.sprite.spritecollide(player, bombs, 1):
|
||||
if pygame.mixer:
|
||||
boom_sound.play()
|
||||
Explosion(player)
|
||||
Explosion(bomb)
|
||||
player.kill()
|
||||
|
||||
# draw the scene
|
||||
dirty = all.draw(screen)
|
||||
pygame.display.update(dirty)
|
||||
|
||||
# cap the framerate at 40fps. Also called 40HZ or 40 times per second.
|
||||
await asyncio.sleep(0.025)
|
||||
|
||||
if pygame.mixer:
|
||||
pygame.mixer.music.fadeout(1000)
|
||||
|
||||
main()
|
||||
24
core/tests/manual/game/index.html
Normal file
24
core/tests/manual/game/index.html
Normal file
@@ -0,0 +1,24 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<link rel="stylesheet" href="aliens.css" />
|
||||
<link rel="stylesheet" href="../../../dist/core.css" />
|
||||
<script type="module" src="../../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script
|
||||
type="py"
|
||||
src="aliens.py"
|
||||
canvas="canvas"
|
||||
config='{"packages":["pygame-ce"]}'
|
||||
></script>
|
||||
<div class="demo">
|
||||
<div class="demo-header">pygame.examples.aliens</div>
|
||||
<div class="demo-content">
|
||||
<canvas id="canvas"></canvas>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -11,11 +11,11 @@
|
||||
<script type="module" src="../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<py-script>
|
||||
input("what's your name?")
|
||||
<py-script worker terminal>
|
||||
print(input("what's your name? "))
|
||||
</py-script>
|
||||
<mpy-script>
|
||||
input("what's your name?")
|
||||
<mpy-script worker terminal>
|
||||
print(input("what's your name? "))
|
||||
</mpy-script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
<script type="module" src="../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script type="py" config='{"interpreter":"https://cdn.jsdelivr.net/pyodide/v0.23.4/full/pyodide.mjs"}' worker>
|
||||
<script type="py" config='{"interpreter":"https://cdn.jsdelivr.net/pyodide/v0.26.0/full/pyodide.mjs"}' worker>
|
||||
import pyodide
|
||||
print(pyodide.__version__)
|
||||
</script>
|
||||
|
||||
13
core/tests/manual/issue-2228/index.html
Normal file
13
core/tests/manual/issue-2228/index.html
Normal file
@@ -0,0 +1,13 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1.0">
|
||||
<script type="module" src="../../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script type="py" config='{"packages":["jsonpointer==3.0.0"]}'>
|
||||
print('Hello World')
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
11
core/tests/manual/issue-2246/index.html
Normal file
11
core/tests/manual/issue-2246/index.html
Normal file
@@ -0,0 +1,11 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1.0">
|
||||
<script type="module" src="../../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script type="py" src="./main.py" terminal worker></script>
|
||||
</body>
|
||||
</html>
|
||||
1
core/tests/manual/issue-2246/main.py
Normal file
1
core/tests/manual/issue-2246/main.py
Normal file
@@ -0,0 +1 @@
|
||||
print(input("What food would you like me to get from the shop? "))
|
||||
@@ -8,7 +8,7 @@
|
||||
<script src="https://cdn.bokeh.org/bokeh/release/bokeh-widgets-3.5.0.min.js"></script>
|
||||
<script src="https://cdn.bokeh.org/bokeh/release/bokeh-tables-3.5.0.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/@holoviz/panel@1.5.0-b.2/dist/panel.min.js"></script>
|
||||
<script type="module" src="../../dist/core.js"></script>
|
||||
<script type="module" src="../../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script type="py" src="main.py" config="config.toml" worker></script>
|
||||
@@ -3,9 +3,9 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="../../dist/core.css">
|
||||
<link rel="stylesheet" href="../../../dist/core.css">
|
||||
<script type="module">
|
||||
import { PyWorker } from '../../dist/core.js';
|
||||
import { PyWorker } from '../../../dist/core.js';
|
||||
const { sync } = await PyWorker(
|
||||
'./worker.py',
|
||||
{
|
||||
@@ -18,6 +18,8 @@
|
||||
document.documentElement.classList.add(
|
||||
await sync.get_class()
|
||||
);
|
||||
|
||||
document.body.textContent = await sync.get_class();
|
||||
</script>
|
||||
</head>
|
||||
</html>
|
||||
@@ -4,8 +4,8 @@
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
||||
<title>Arrr - Piratical PyScript</title>
|
||||
<link rel="stylesheet" href="../../dist/core.css" />
|
||||
<script type="module" src="../../dist/core.js"></script>
|
||||
<link rel="stylesheet" href="../../../dist/core.css" />
|
||||
<script type="module" src="../../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Arrr</h1>
|
||||
@@ -3,7 +3,7 @@
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>PyTerminal</title>
|
||||
<title>PyEditor Failure</title>
|
||||
<link rel="stylesheet" href="../../dist/core.css">
|
||||
<script type="module" src="../../dist/core.js"></script>
|
||||
</head>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>PyTerminal</title>
|
||||
<title>PyEditor</title>
|
||||
<link rel="stylesheet" href="../../dist/core.css">
|
||||
<script type="module" src="../../dist/core.js"></script>
|
||||
</head>
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="../../dist/core.css">
|
||||
<link rel="stylesheet" href="../../../dist/core.css">
|
||||
<script type="module">
|
||||
import '../../dist/core.js';
|
||||
import '../../../dist/core.js';
|
||||
|
||||
addEventListener('mpy-editor', async ({ target }) => {
|
||||
if (target.hasAttribute('setup')) {
|
||||
@@ -4,7 +4,7 @@
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>PyTerminal Prompt: NO REPL</title>
|
||||
<script type="module" src="../../dist/core.js"></script>
|
||||
<script type="module" src="../../../dist/core.js"></script>
|
||||
<style>.xterm { padding: .5rem; }</style>
|
||||
</head>
|
||||
<body>
|
||||
@@ -4,7 +4,7 @@
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>PyTerminal Prompt: REPL</title>
|
||||
<script type="module" src="../../dist/core.js"></script>
|
||||
<script type="module" src="../../../dist/core.js"></script>
|
||||
<style>.xterm { padding: .5rem; }</style>
|
||||
</head>
|
||||
<body>
|
||||
@@ -4,8 +4,8 @@
|
||||
<title>Service Worker</title>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<link rel="stylesheet" href="../../dist/core.css">
|
||||
<script type="module" src="../../dist/core.js"></script>
|
||||
<link rel="stylesheet" href="../../../dist/core.css">
|
||||
<script type="module" src="../../../dist/core.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script type="mpy" service-worker="./sabayon.js" worker>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user