Skip to content

Commit b39b2d5

Browse files
committed
Merge branch 'main' into fpliger/154_move_py_code_out
2 parents 822ae0d + 204c099 commit b39b2d5

20 files changed

+1024
-206
lines changed

.pre-commit-config.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
default_stages: [commit]
2+
repos:
3+
- repo: https://github.com/psf/black
4+
rev: 22.3.0
5+
hooks:
6+
- id: black
7+
- repo: https://github.com/pycqa/isort
8+
rev: 5.10.1
9+
hooks:
10+
- id: isort
11+
name: isort (python)

GETTING-STARTED.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ This page will guide you through getting started with PyScript.
55
## Development setup
66

77
PyScript does not require any development environment other
8-
than a web browser. We recommend using Chrome.
8+
than a web browser. We recommend using [Chrome](https://www.google.com/chrome/).
99

10-
If you're using [VSCode](https://code.visualstudio.com/) the
10+
If you're using [VSCode](https://code.visualstudio.com/), the
1111
[Live Server extension](https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer)
1212
can be used to reload the page as you edit the HTML file.
1313

@@ -16,16 +16,16 @@ can be used to reload the page as you edit the HTML file.
1616
There is no installation required. In this document we'll use
1717
the PyScript assets served on https://pyscript.net.
1818

19-
If you want to download the source and build it yourself follow
19+
If you want to download the source and build it yourself, follow
2020
the instructions in the README.md file.
2121

2222
## Your first PyScript HTML file
2323

24-
Here's a "Hello, world!" example using PyScript
24+
Here's a "Hello, world!" example using PyScript.
2525

2626
Using your favorite editor create a new file called `hello.html` in
27-
the same directory as your PyScript JavaScript and CSS files with the
28-
following content and open the file in your web browser. You can typically
27+
the same directory as your PyScript, JavaScript, and CSS files with the
28+
following content, and open the file in your web browser. You can typically
2929
open an HTML by double clicking it in your file explorer.
3030

3131
```html
@@ -124,7 +124,8 @@ HTML head. You can also link to `.whl` files directly on disk like in our [toga
124124
</py-env>
125125
```
126126

127-
If your `.whl` is not a pure Python wheel then open a PR or issue with [pyodide](https://github.com/pyodide/pyodide) to get it added here https://github.com/pyodide/pyodide/tree/main/packages. If there's enough popular demand the pyodide team will likely work on supporting your package, regardless things will likely move faster if you make the PR and consult with the team to get unblocked.
127+
If your `.whl` is not a pure Python wheel, then open a PR or issue with [pyodide](https://github.com/pyodide/pyodide) to get it added [here](https://github.com/pyodide/pyodide/tree/main/packages).
128+
If there's enough popular demand the pyodide team will likely work on supporting your package, regardless things will likely move faster if you make the PR and consult with the team to get unblocked.
128129

129130
For example, NumPy and Matplotlib are available. Notice here we're using `<py-script output="plot">`
130131
as a shortcut, which takes the expression on the last line of the script and runs `pyscript.write('plot', fig)`.

MAINTAINERS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This document lists the Maintainers of the Project. Maintainers may be added onc
99
| Philipp Rudiger | Anaconda, Inc |
1010
| Peter Wang | Anaconda, Inc |
1111
| Kevin Goldsmith | Anaconda, Inc |
12+
| Mariana Meireles | Anaconda, Inc |
1213
| --- | --- |
1314

1415
---

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## What is PyScript
44

5-
### tl;dr
5+
### Summary
66
PyScript is a Pythonic alternative to Scratch, JSFiddle or other "easy to use" programming frameworks, making the web a friendly, hackable, place where anyone can author interesting and interactive applications.
77

88
To get started see [GETTING-STARTED](GETTING-STARTED.md).

pyscriptjs/Makefile

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
tag := latest
2+
git_hash ?= $(shell git log -1 --pretty=format:%h)
3+
4+
base_dir ?= $(shell git rev-parse --show-toplevel)
5+
src_dir ?= $(base_dir)/src
6+
examples ?= $(base_dir)/examples
7+
app_dir ?= $(shell git rev-parse --show-prefix)
8+
9+
CONDA_ENV ?= ./env
10+
env := $(CONDA_ENV)
11+
conda_run := conda run -p $(env)
12+
13+
.PHONY: setup
14+
setup:
15+
@if [ -z "$${CONDA_SHLVL:+x}" ]; then echo "Conda is not installed." && exit 1; fi
16+
$(CONDA_EXE) env $(shell [ -d $(env) ] && echo update || echo create) -p $(env) --file environment.yml
17+
18+
.PHONY: clean
19+
clean:
20+
find . -name \*.py[cod] -delete
21+
rm -rf .pytest_cache .coverage coverage.xml
22+
23+
clean-all: clean
24+
rm -rf $(env) *.egg-info
25+
26+
.PHONY: shell
27+
shell:
28+
@export CONDA_ENV_PROMPT='<{name}>'
29+
@echo 'conda activate $(env)'
30+
31+
.PHONY: dev
32+
dev:
33+
npm run dev
34+
35+
.PHONY: build
36+
build:
37+
npm run build
38+
39+
.PHONY: test
40+
test:
41+
@echo "Tests are coming :( this is a placeholder and it's meant to fail!"
42+
$(conda_run) pytest -vv $(ARGS) tests/ --log-cli-level=warning
43+
44+
.PHONY: test-py
45+
test-py:
46+
@echo "Tests are coming :( this is a placeholder and it's meant to fail!"
47+
$(conda_run) pytest -vv $(ARGS) tests/ --log-cli-level=warning
48+
49+
.PHONY: test-ts
50+
test-ts:
51+
@echo "Tests are coming :( this is a placeholder and it's meant to fail!"
52+
npm run tests
53+
54+
.PHONY: fmt
55+
fmt: fmt-py fmt-ts
56+
@echo "Format completed"
57+
58+
.PHONY: fmt-check
59+
fmt-check: fmt-ts-check fmt-py
60+
@echo "Format check completed"
61+
62+
.PHONY: fmt-ts
63+
fmt-ts:
64+
npm run format
65+
66+
.PHONY: fmt-ts-check
67+
fmt-ts-check:
68+
npm run format:check
69+
70+
.PHONY: fmt-py
71+
fmt-py:
72+
$(conda_run) black -l 88 .
73+
74+
.PHONY: fmt-py-check
75+
fmt-py-check:
76+
$(conda_run) black -l 88 --check .
77+
78+
.PHONY: lint
79+
lint: lint-ts
80+
@echo "Format check completed"
81+
82+
.PHONY: lint-ts
83+
lint-ts:
84+
$(conda_run) npm run lint

pyscriptjs/environment.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
channels:
2+
- defaults
3+
- conda-forge
4+
dependencies:
5+
- python=3.9
6+
- pip=20.2.2
7+
- pytest=7
8+
- nodejs=16
9+
- black
10+
- isort
11+
- codespell

pyscriptjs/examples/antigravity.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
import random
22
import sys
33

4-
from js import document, DOMParser, setInterval
4+
from js import DOMParser, document, setInterval
55
from pyodide import create_proxy
66
from pyodide.http import open_url
77

8-
class Antigravity():
98

10-
url = './antigravity.svg'
11-
9+
class Antigravity:
10+
11+
url = "./antigravity.svg"
12+
1213
def __init__(self, target=None, interval=10, append=True, fly=False):
1314
target = target or sys.stdout._out
14-
self.target = document.getElementById(target) if isinstance(target, str) else target
15-
doc = DOMParser.new().parseFromString(open_url(self.url).read(), "image/svg+xml")
15+
self.target = (
16+
document.getElementById(target) if isinstance(target, str) else target
17+
)
18+
doc = DOMParser.new().parseFromString(
19+
open_url(self.url).read(), "image/svg+xml"
20+
)
1621
self.node = doc.documentElement
1722
if append:
1823
self.target.append(self.node)
@@ -27,13 +32,14 @@ def fly(self):
2732
setInterval(create_proxy(self.move), self.interval)
2833

2934
def move(self):
30-
char = self.node.getElementsByTagName('g')[1]
31-
char.setAttribute('transform', f'translate({self.xoffset}, {-self.yoffset})')
32-
self.xoffset += random.normalvariate(0, 1)/20
35+
char = self.node.getElementsByTagName("g")[1]
36+
char.setAttribute("transform", f"translate({self.xoffset}, {-self.yoffset})")
37+
self.xoffset += random.normalvariate(0, 1) / 20
3338
if self.yoffset < 50:
3439
self.yoffset += 0.1
3540
else:
36-
self.yoffset += random.normalvariate(0, 1)/20
41+
self.yoffset += random.normalvariate(0, 1) / 20
42+
3743

3844
_auto = Antigravity(append=True)
3945
fly = _auto.fly

pyscriptjs/examples/fractals.py

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
from typing import Tuple
2+
23
import numpy as np
34
from numpy.polynomial import Polynomial
45

5-
def mandelbrot(width: int, height: int, *,
6-
x: float = -0.5, y: float = 0, zoom: int = 1, max_iterations: int = 100) -> np.array:
6+
7+
def mandelbrot(
8+
width: int,
9+
height: int,
10+
*,
11+
x: float = -0.5,
12+
y: float = 0,
13+
zoom: int = 1,
14+
max_iterations: int = 100
15+
) -> np.array:
716
"""
817
From https://www.learnpythonwithrune.org/numpy-compute-mandelbrot-set-by-vectorization/.
918
"""
1019
# To make navigation easier we calculate these values
11-
x_width, y_height = 1.5, 1.5*height/width
12-
x_from, x_to = x - x_width/zoom, x + x_width/zoom
13-
y_from, y_to = y - y_height/zoom, y + y_height/zoom
20+
x_width, y_height = 1.5, 1.5 * height / width
21+
x_from, x_to = x - x_width / zoom, x + x_width / zoom
22+
y_from, y_to = y - y_height / zoom, y + y_height / zoom
1423

1524
# Here the actual algorithm starts
1625
x = np.linspace(x_from, x_to, width).reshape((1, width))
1726
y = np.linspace(y_from, y_to, height).reshape((height, 1))
18-
c = x + 1j*y
27+
c = x + 1j * y
1928

2029
# Initialize z to all zero
2130
z = np.zeros(c.shape, dtype=np.complex128)
@@ -26,27 +35,38 @@ def mandelbrot(width: int, height: int, *,
2635
# To keep track on which points did not converge so far
2736
m = np.full(c.shape, True, dtype=bool)
2837
for i in range(max_iterations):
29-
z[m] = z[m]**2 + c[m]
30-
diverged = np.greater(np.abs(z), 2, out=np.full(c.shape, False), where=m) # Find diverging
31-
div_time[diverged] = i # set the value of the diverged iteration number
32-
m[np.abs(z) > 2] = False # to remember which have diverged
38+
z[m] = z[m] ** 2 + c[m]
39+
diverged = np.greater(
40+
np.abs(z), 2, out=np.full(c.shape, False), where=m
41+
) # Find diverging
42+
div_time[diverged] = i # set the value of the diverged iteration number
43+
m[np.abs(z) > 2] = False # to remember which have diverged
3344

3445
return div_time
3546

36-
def julia(width: int, height: int, *,
37-
c: complex = -0.4 + 0.6j, x: float = 0, y: float = 0, zoom: int = 1, max_iterations: int = 100) -> np.array:
47+
48+
def julia(
49+
width: int,
50+
height: int,
51+
*,
52+
c: complex = -0.4 + 0.6j,
53+
x: float = 0,
54+
y: float = 0,
55+
zoom: int = 1,
56+
max_iterations: int = 100
57+
) -> np.array:
3858
"""
3959
From https://www.learnpythonwithrune.org/numpy-calculate-the-julia-set-with-vectorization/.
4060
"""
4161
# To make navigation easier we calculate these values
42-
x_width, y_height = 1.5, 1.5*height/width
43-
x_from, x_to = x - x_width/zoom, x + x_width/zoom
44-
y_from, y_to = y - y_height/zoom, y + y_height/zoom
62+
x_width, y_height = 1.5, 1.5 * height / width
63+
x_from, x_to = x - x_width / zoom, x + x_width / zoom
64+
y_from, y_to = y - y_height / zoom, y + y_height / zoom
4565

4666
# Here the actual algorithm starts
4767
x = np.linspace(x_from, x_to, width).reshape((1, width))
4868
y = np.linspace(y_from, y_to, height).reshape((height, 1))
49-
z = x + 1j*y
69+
z = x + 1j * y
5070

5171
# Initialize z to all zero
5272
c = np.full(z.shape, c)
@@ -57,16 +77,26 @@ def julia(width: int, height: int, *,
5777
# To keep track on which points did not converge so far
5878
m = np.full(c.shape, True, dtype=bool)
5979
for i in range(max_iterations):
60-
z[m] = z[m]**2 + c[m]
80+
z[m] = z[m] ** 2 + c[m]
6181
m[np.abs(z) > 2] = False
6282
div_time[m] = i
6383

6484
return div_time
6585

86+
6687
Range = Tuple[float, float]
6788

68-
def newton(width: int, height: int, *,
69-
p: Polynomial, a: complex, xr: Range = (-2.5, 1), yr: Range = (-1, 1), max_iterations: int = 100) -> (np.array, np.array):
89+
90+
def newton(
91+
width: int,
92+
height: int,
93+
*,
94+
p: Polynomial,
95+
a: complex,
96+
xr: Range = (-2.5, 1),
97+
yr: Range = (-1, 1),
98+
max_iterations: int = 100
99+
) -> (np.array, np.array):
70100
""" """
71101
# To make navigation easier we calculate these values
72102
x_from, x_to = xr
@@ -75,7 +105,7 @@ def newton(width: int, height: int, *,
75105
# Here the actual algorithm starts
76106
x = np.linspace(x_from, x_to, width).reshape((1, width))
77107
y = np.linspace(y_from, y_to, height).reshape((height, 1))
78-
z = x + 1j*y
108+
z = x + 1j * y
79109

80110
# Compute the derivative
81111
dp = p.deriv()
@@ -97,10 +127,12 @@ def newton(width: int, height: int, *,
97127
r = np.full(a.shape, 0, dtype=int)
98128

99129
for i in range(max_iterations):
100-
z[m] = z[m] - a[m]*p(z[m])/dp(z[m])
130+
z[m] = z[m] - a[m] * p(z[m]) / dp(z[m])
101131

102132
for j, root in enumerate(roots):
103-
converged = (np.abs(z.real - root.real) < epsilon) & (np.abs(z.imag - root.imag) < epsilon)
133+
converged = (np.abs(z.real - root.real) < epsilon) & (
134+
np.abs(z.imag - root.imag) < epsilon
135+
)
104136
m[converged] = False
105137
r[converged] = j + 1
106138

0 commit comments

Comments
 (0)