Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup PyScript renderer & use Bun for building JS #254

Merged
merged 9 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ end_of_line = lf
indent_size = 4
max_line_length = 120

[*.yml]
indent_size = 4

[*.md]
indent_size = 4

[*.html]
indent_size = 4
max_line_length = off

[*.js]
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/publish-develop-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: oven-sh/setup-bun@v2
with:
bun-version: latest
- uses: actions/setup-python@v5
with:
python-version: 3.x
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/publish-py.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v2
with:
bun-version: latest
- name: Set up Python
uses: actions/setup-python@v5
with:
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/publish-release-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: oven-sh/setup-bun@v2
with:
bun-version: latest
- uses: actions/setup-python@v5
with:
python-version: 3.x
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/test-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: oven-sh/setup-bun@v2
with:
bun-version: latest
- uses: actions/setup-python@v5
with:
python-version: 3.x
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/test-src.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ jobs:
python-version: ["3.9", "3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v2
with:
bun-version: latest
- name: Use Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
Expand Down
1 change: 1 addition & 0 deletions docs/examples/html/pyscript-setup-local-interpreter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% pyscript_setup config='{"interpreter":"/static/pyodide/pyodide.mjs"}' %}
1 change: 1 addition & 0 deletions docs/src/about/code.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
If you plan to make code changes to this repository, you will need to install the following dependencies first:

- [Python 3.9+](https://www.python.org/downloads/)
- [Bun](https://bun.sh/)
- [Git](https://git-scm.com/downloads)

Once done, you should clone this repository:
Expand Down
14 changes: 14 additions & 0 deletions docs/src/reference/template-tag.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,17 @@ You can optionally use this tag to configure the current PyScript environment. F
```python
{% include "../../examples/python/pyscript-setup-config-object.py" %}
```

??? question "Can I use a local interpreter for PyScript?"

Yes, you can set up a local interpreter by following PyScript's [standard documentation](https://docs.pyscript.net/latest/user-guide/offline/#local-pyodide-packages).

To summarize,

1. Download the latest Pyodide bundle from the [Pyodide GitHub releases page](https://github.com/pyodide/pyodide/releases) (for example `pyodide-0.26.3.tar.bz2`).
2. Extract the contents of the bundle to your project's static files.
3. Configure your `#!jinja {% pyscript_setup %}` template tag to use `pyodide` as an interpreter.

```jinja linenums="0"
{% include "../../examples/html/pyscript-setup-local-interpreter.html" %}
```
10 changes: 5 additions & 5 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ def test_python(session: Session) -> None:
settings_files = glob(settings_glob)
assert settings_files, f"No Django settings files found at '{settings_glob}'!"
for settings_file in settings_files:
settings_module = settings_file.strip(".py").replace("/", ".").replace("\\", ".")
settings_module = (
settings_file.strip(".py").replace("/", ".").replace("\\", ".")
)
session.run(
"python",
"manage.py",
"test",
*posargs,
"-v",
"2",
"--settings",
settings_module,
)
Expand All @@ -62,8 +62,8 @@ def test_style(session: Session) -> None:
def test_javascript(session: Session) -> None:
install_requirements_file(session, "test-env")
session.chdir(ROOT_DIR / "src" / "js")
session.run("python", "-m", "nodejs.npm", "install", external=True)
session.run("python", "-m", "nodejs.npm", "run", "check")
session.run("bun", "install", external=True)
session.run("bun", "run", "check", external=True)


def install_requirements_file(session: Session, name: str) -> None:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["setuptools>=42", "wheel", "nodejs-bin==18.4.0a4"]
requires = ["setuptools>=42", "wheel"]
build-backend = "setuptools.build_meta"

[tool.mypy]
Expand Down
1 change: 0 additions & 1 deletion requirements/test-env.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ twisted
channels[daphne]>=4.0.0
tblib
whitenoise
nodejs-bin==18.4.0a4
45 changes: 33 additions & 12 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from __future__ import annotations, print_function

import shutil
import subprocess
import sys
import traceback
from distutils import log
from logging import getLogger
from pathlib import Path

from nodejs import npm
from setuptools import find_namespace_packages, setup
from setuptools.command.develop import develop
from setuptools.command.sdist import sdist

log = getLogger(__name__)

# -----------------------------------------------------------------------------
# Basic Constants
# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -97,19 +99,44 @@
# ----------------------------------------------------------------------------
# Build Javascript
# ----------------------------------------------------------------------------
def copy_js_files(source_dir: Path, destination: Path) -> None:
if destination.exists():
shutil.rmtree(destination)
destination.mkdir()

for file in source_dir.iterdir():
if file.is_file():
shutil.copy(file, destination / file.name)
else:
copy_js_files(file, destination / file.name)


def build_javascript_first(build_cls: type):
class Command(build_cls):
def run(self):

log.info("Installing Javascript...")
result = npm.call(["install"], cwd=str(js_dir))
result = subprocess.run(
["bun", "install"], cwd=str(js_dir), check=True
).returncode
if result != 0:
log.error(traceback.format_exc())
log.error("Failed to install Javascript")
raise RuntimeError("Failed to install Javascript")

log.info("Building Javascript...")
result = npm.call(["run", "build"], cwd=str(js_dir))
result = subprocess.run(
[
"bun",
"build",
"./src/index.tsx",
"--outfile",
str(static_dir / "client.js"),
"--minify",
],
cwd=str(js_dir),
check=True,
).returncode
if result != 0:
log.error(traceback.format_exc())
log.error("Failed to build Javascript")
Expand All @@ -118,18 +145,12 @@ def run(self):
log.info("Copying @pyscript/core distribution")
pyscript_dist = js_dir / "node_modules" / "@pyscript" / "core" / "dist"
pyscript_static_dir = static_dir / "pyscript"
if not pyscript_static_dir.exists():
pyscript_static_dir.mkdir()
for file in pyscript_dist.iterdir():
shutil.copy(file, pyscript_static_dir / file.name)
copy_js_files(pyscript_dist, pyscript_static_dir)

log.info("Copying Morphdom distribution")
morphdom_dist = js_dir / "node_modules" / "morphdom" / "dist"
morphdom_static_dir = static_dir / "morphdom"
if not morphdom_static_dir.exists():
morphdom_static_dir.mkdir()
for file in morphdom_dist.iterdir():
shutil.copy(file, morphdom_static_dir / file.name)
copy_js_files(morphdom_dist, morphdom_static_dir)

log.info("Successfully built Javascript")
super().run()
Expand Down
Binary file added src/js/bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions src/js/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default [{}];
Loading
Loading