Skip to content

Commit e09fa17

Browse files
authored
Cleanup PyScript renderer & use Bun for building JS (#254)
- Switch to `Bun` for building JS - Minor cleanup to pyscript renderer - Revert pyscript version to 0.5 due to [bugs](pyscript/pyscript#2228)
1 parent 7948ac4 commit e09fa17

23 files changed

+135
-3575
lines changed

.editorconfig

+4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ end_of_line = lf
1414
indent_size = 4
1515
max_line_length = 120
1616

17+
[*.yml]
18+
indent_size = 4
19+
1720
[*.md]
1821
indent_size = 4
1922

2023
[*.html]
24+
indent_size = 4
2125
max_line_length = off
2226

2327
[*.js]

.github/workflows/publish-develop-docs.yml

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ jobs:
1111
- uses: actions/checkout@v4
1212
with:
1313
fetch-depth: 0
14+
- uses: oven-sh/setup-bun@v2
15+
with:
16+
bun-version: latest
1417
- uses: actions/setup-python@v5
1518
with:
1619
python-version: 3.x

.github/workflows/publish-py.yml

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ jobs:
1212
runs-on: ubuntu-latest
1313
steps:
1414
- uses: actions/checkout@v4
15+
- uses: oven-sh/setup-bun@v2
16+
with:
17+
bun-version: latest
1518
- name: Set up Python
1619
uses: actions/setup-python@v5
1720
with:

.github/workflows/publish-release-docs.yml

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ jobs:
1111
- uses: actions/checkout@v4
1212
with:
1313
fetch-depth: 0
14+
- uses: oven-sh/setup-bun@v2
15+
with:
16+
bun-version: latest
1417
- uses: actions/setup-python@v5
1518
with:
1619
python-version: 3.x

.github/workflows/test-docs.yml

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ jobs:
1717
- uses: actions/checkout@v4
1818
with:
1919
fetch-depth: 0
20+
- uses: oven-sh/setup-bun@v2
21+
with:
22+
bun-version: latest
2023
- uses: actions/setup-python@v5
2124
with:
2225
python-version: 3.x

.github/workflows/test-src.yml

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ jobs:
1818
python-version: ["3.9", "3.10", "3.11", "3.12"]
1919
steps:
2020
- uses: actions/checkout@v4
21+
- uses: oven-sh/setup-bun@v2
22+
with:
23+
bun-version: latest
2124
- name: Use Python ${{ matrix.python-version }}
2225
uses: actions/setup-python@v5
2326
with:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% pyscript_setup config='{"interpreter":"/static/pyodide/pyodide.mjs"}' %}

docs/src/about/code.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
If you plan to make code changes to this repository, you will need to install the following dependencies first:
2020

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

2425
Once done, you should clone this repository:

docs/src/reference/template-tag.md

+14
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,17 @@ You can optionally use this tag to configure the current PyScript environment. F
391391
```python
392392
{% include "../../examples/python/pyscript-setup-config-object.py" %}
393393
```
394+
395+
??? question "Can I use a local interpreter for PyScript?"
396+
397+
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).
398+
399+
To summarize,
400+
401+
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`).
402+
2. Extract the contents of the bundle to your project's static files.
403+
3. Configure your `#!jinja {% pyscript_setup %}` template tag to use `pyodide` as an interpreter.
404+
405+
```jinja linenums="0"
406+
{% include "../../examples/html/pyscript-setup-local-interpreter.html" %}
407+
```

noxfile.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ def test_python(session: Session) -> None:
3131
settings_files = glob(settings_glob)
3232
assert settings_files, f"No Django settings files found at '{settings_glob}'!"
3333
for settings_file in settings_files:
34-
settings_module = settings_file.strip(".py").replace("/", ".").replace("\\", ".")
34+
settings_module = (
35+
settings_file.strip(".py").replace("/", ".").replace("\\", ".")
36+
)
3537
session.run(
3638
"python",
3739
"manage.py",
3840
"test",
3941
*posargs,
40-
"-v",
41-
"2",
4242
"--settings",
4343
settings_module,
4444
)
@@ -62,8 +62,8 @@ def test_style(session: Session) -> None:
6262
def test_javascript(session: Session) -> None:
6363
install_requirements_file(session, "test-env")
6464
session.chdir(ROOT_DIR / "src" / "js")
65-
session.run("python", "-m", "nodejs.npm", "install", external=True)
66-
session.run("python", "-m", "nodejs.npm", "run", "check")
65+
session.run("bun", "install", external=True)
66+
session.run("bun", "run", "check", external=True)
6767

6868

6969
def install_requirements_file(session: Session, name: str) -> None:

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[build-system]
2-
requires = ["setuptools>=42", "wheel", "nodejs-bin==18.4.0a4"]
2+
requires = ["setuptools>=42", "wheel"]
33
build-backend = "setuptools.build_meta"
44

55
[tool.mypy]

requirements/test-env.txt

-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ twisted
33
channels[daphne]>=4.0.0
44
tblib
55
whitenoise
6-
nodejs-bin==18.4.0a4

setup.py

+33-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
from __future__ import annotations, print_function
22

33
import shutil
4+
import subprocess
45
import sys
56
import traceback
6-
from distutils import log
7+
from logging import getLogger
78
from pathlib import Path
89

9-
from nodejs import npm
1010
from setuptools import find_namespace_packages, setup
1111
from setuptools.command.develop import develop
1212
from setuptools.command.sdist import sdist
1313

14+
log = getLogger(__name__)
15+
1416
# -----------------------------------------------------------------------------
1517
# Basic Constants
1618
# -----------------------------------------------------------------------------
@@ -97,19 +99,44 @@
9799
# ----------------------------------------------------------------------------
98100
# Build Javascript
99101
# ----------------------------------------------------------------------------
102+
def copy_js_files(source_dir: Path, destination: Path) -> None:
103+
if destination.exists():
104+
shutil.rmtree(destination)
105+
destination.mkdir()
106+
107+
for file in source_dir.iterdir():
108+
if file.is_file():
109+
shutil.copy(file, destination / file.name)
110+
else:
111+
copy_js_files(file, destination / file.name)
112+
113+
100114
def build_javascript_first(build_cls: type):
101115
class Command(build_cls):
102116
def run(self):
103117

104118
log.info("Installing Javascript...")
105-
result = npm.call(["install"], cwd=str(js_dir))
119+
result = subprocess.run(
120+
["bun", "install"], cwd=str(js_dir), check=True
121+
).returncode
106122
if result != 0:
107123
log.error(traceback.format_exc())
108124
log.error("Failed to install Javascript")
109125
raise RuntimeError("Failed to install Javascript")
110126

111127
log.info("Building Javascript...")
112-
result = npm.call(["run", "build"], cwd=str(js_dir))
128+
result = subprocess.run(
129+
[
130+
"bun",
131+
"build",
132+
"./src/index.tsx",
133+
"--outfile",
134+
str(static_dir / "client.js"),
135+
"--minify",
136+
],
137+
cwd=str(js_dir),
138+
check=True,
139+
).returncode
113140
if result != 0:
114141
log.error(traceback.format_exc())
115142
log.error("Failed to build Javascript")
@@ -118,18 +145,12 @@ def run(self):
118145
log.info("Copying @pyscript/core distribution")
119146
pyscript_dist = js_dir / "node_modules" / "@pyscript" / "core" / "dist"
120147
pyscript_static_dir = static_dir / "pyscript"
121-
if not pyscript_static_dir.exists():
122-
pyscript_static_dir.mkdir()
123-
for file in pyscript_dist.iterdir():
124-
shutil.copy(file, pyscript_static_dir / file.name)
148+
copy_js_files(pyscript_dist, pyscript_static_dir)
125149

126150
log.info("Copying Morphdom distribution")
127151
morphdom_dist = js_dir / "node_modules" / "morphdom" / "dist"
128152
morphdom_static_dir = static_dir / "morphdom"
129-
if not morphdom_static_dir.exists():
130-
morphdom_static_dir.mkdir()
131-
for file in morphdom_dist.iterdir():
132-
shutil.copy(file, morphdom_static_dir / file.name)
153+
copy_js_files(morphdom_dist, morphdom_static_dir)
133154

134155
log.info("Successfully built Javascript")
135156
super().run()

src/js/bun.lockb

99 KB
Binary file not shown.

src/js/eslint.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default [{}];

0 commit comments

Comments
 (0)