Skip to content

Commit ca653c9

Browse files
authored
Merge pull request #52 from pyscript/2023-12-1
2023.12.1 documentation changes
2 parents 6d14e95 + e75c01b commit ca653c9

16 files changed

+193
-30
lines changed

docs/assets/images/pyeditor1.gif

256 KB
Loading

docs/assets/images/pyterm2.gif

-46.1 KB
Loading

docs/assets/images/pyterm3.gif

58.5 KB
Loading

docs/beginning-pyscript.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ level.
7070

7171
You can see this application embedded into the page below:
7272

73-
<iframe src="https://ntoll.pyscriptapps.com/piratical/v3" style="border: 1px solid black; width:100%;min-height: 400px; border-radius: 0.2rem; box-shadow: var(--md-shadow-z1);"></iframe>
73+
<iframe src="https://ntoll.pyscriptapps.com/piratical/v4/" style="border: 1px solid black; width:100%;min-height: 400px; border-radius: 0.2rem; box-shadow: var(--md-shadow-z1);"></iframe>
7474

7575
Let's explore each of the three files that make this app work.
7676

@@ -106,7 +106,7 @@ module in the document's `<head>` tag:
106106
<meta charset="utf-8" />
107107
<meta name="viewport" content="width=device-width,initial-scale=1" />
108108
<title>🦜 Polyglot - Piratical PyScript</title>
109-
<script type="module" src="https://pyscript.net/releases/2023.11.2/core.js"></script>
109+
<script type="module" src="https://pyscript.net/releases/2023.12.1/core.js"></script>
110110
</head>
111111
<body>
112112

@@ -139,7 +139,8 @@ application's output.
139139

140140
There's something strange about the `<button>` tag: it has a `py-click`
141141
attribute with the value `translate_english`. This is, in fact, the name of a
142-
Python function we'll run whenever the button is clicked.
142+
Python function we'll run whenever the button is clicked. Such `py-*` style
143+
attributes are [built into PyScript](user-guide/builtins.md#html-attributes).
143144

144145
We put all this together in the `script` tag at the end of the `<body>`. This
145146
tells the browser we're using PyScript (`type="py"`), and where PyScript
@@ -155,7 +156,7 @@ In the end, our HTML should look like this:
155156
<meta charset="utf-8" />
156157
<meta name="viewport" content="width=device-width,initial-scale=1" />
157158
<title>🦜 Polyglot - Piratical PyScript</title>
158-
<script type="module" src="https://pyscript.net/releases/2023.11.2/core.js"></script>
159+
<script type="module" src="https://pyscript.net/releases/2023.12.1/core.js"></script>
159160
</head>
160161
<body>
161162
<h1>Polyglot 🦜 💬 🇬🇧 ➡️ 🏴‍☠️</h1>

docs/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
<dt><strong>I want to contribute...</strong></dt>
3535
<dd>
3636
<p>Welcome, friend!
37-
PyScript is an <a href="/license/">open source project</a>, we expect participants to act in
38-
the spirit of our <a href="/conduct/">code of conduct</a> and we have many
39-
ways in which <a href="/contributing/"><u>you</u> can contribute</a>.
40-
Our <a href="/developers/">developer guide</a> explains how to set
37+
PyScript is an <a href="license/">open source project</a>, we expect participants to act in
38+
the spirit of our <a href="conduct/">code of conduct</a> and we have many
39+
ways in which <a href="contributing/"><u>you</u> can contribute</a>.
40+
Our <a href="developers/">developer guide</a> explains how to set
4141
up a working development environment for PyScript.</p>
4242
</dd>
4343
<dt><strong>Just show me...</strong></dt>

docs/user-guide/architecture.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ Here's how PyScript unfolds through time:
129129
attribute of a `<script>`, `<py-script>` or `<mpy-script>` tag).
130130
3. Given the detected configuration, download the required interpreter.
131131
4. Setup the interpreter's environment. This includes any
132-
[plugins](configuration.md/#plugins), [packages](configuration.md/#packages) or [files](configuration.md/#files) that need
132+
[plugins](configuration.md/#plugins), [packages](configuration.md/#packages), [files](configuration.md/#files) or [JavaScript modules](configuration.md/#javascript-modules)that need
133133
to be loaded.
134134
5. Make available various
135135
[builtin helper objects and functions](builtins.md) to the

docs/user-guide/builtins.md

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
# Builtin helpers
22

33
PyScript makes available convenience objects and functions inside
4-
Python. This is done via the `pyscript` module:
4+
Python as well as custom attributes in HTML.
5+
6+
In Python this is done via the `pyscript` module:
57

68
```python title="Accessing the document object via the pyscript module"
79
from pyscript import document
810
```
911

12+
In HTML this is done via `py-*` attributes:
13+
14+
```html title="An example of a py-click handler"
15+
<button id="foo" py-click="handler_defined_in_python">Click me</button>
16+
```
17+
1018
## Common features
1119

12-
These objects / functions are available in both the main thread and in code
13-
running on a web worker:
20+
These Python objects / functions are available in both the main thread and in
21+
code running on a web worker:
1422

1523
### `pyscript.window`
1624

@@ -94,6 +102,8 @@ def click_handler(event):
94102
display("I've been clicked!")
95103
```
96104

105+
This functionality is related to the [HTML py-*](#html-attributes) attributes.
106+
97107
### `pyscript.js_modules`
98108

99109
It is possible to [define JavaScript modules to use within your Python code](configuration.md#javascript-modules).
@@ -152,3 +162,54 @@ from pyscript import sync
152162

153163
sync.hello("PyScript")
154164
```
165+
166+
## HTML attributes
167+
168+
As a convenience, and to ensure backwards compatibility, PyScript allows the
169+
use of inline event handlers via custom HTML attributes.
170+
171+
!!! warning
172+
173+
This classic pattern of coding (inline event handlers) is no longer
174+
considered good practice in web development circles.
175+
176+
We include this behaviour for historic reasons, but the folks at
177+
Mozilla [have a good explanation](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these)
178+
of why this is currently considered bad practice.
179+
180+
These attributes are expressed as `py-*` attributes of an HTML element that
181+
reference the name of a Python function to run when the event is fired. You
182+
should replace the `*` with the _actual name of an event_ (e.g. `py-click`).
183+
This is similar to how all
184+
[event handlers on elements](https://html.spec.whatwg.org/multipage/webappapis.html#event-handlers-on-elements,-document-objects,-and-window-objects)
185+
start with `on` in standard HTML (e.g. `onclick`). The rule of thumb is to
186+
simply replace `on` with `py-` and then reference the name of a Python
187+
function.
188+
189+
```html title="A py-click event on an HTML button element."
190+
<button py-click="handle_click" id="my_button">Click me!</button>
191+
```
192+
193+
```python title="The related Python function."
194+
from pyscript import window
195+
196+
197+
def handle_click(event):
198+
"""
199+
Simply log the click event to the browser's console.
200+
"""
201+
window.console.log(event)
202+
```
203+
204+
Under the hood, the [`pyscript.when`](#pyscriptwhen) decorator is used to
205+
enable this behaviour.
206+
207+
!!! note
208+
209+
In earlier versions of PyScript, the value associated with the attribute
210+
was simply evaluated by the Python interpreter. This was unsafe:
211+
manipulation of the attribute's value could have resulted in the evaluation
212+
of arbitrary code.
213+
214+
This is why we changed to the current behaviour: just supply the name
215+
of the Python function to be evaluated, and PyScript will do this safely.

docs/user-guide/configuration.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ specification of configuration information via a _single_ `<py-config>` or
7979

8080
## Options
8181

82-
There are four core options ([`interpreter`](#interpreter), [`files`](#files),
83-
[`packages`](#packages) and [`plugins`](#plugins)). The user is free to define
82+
There are five core options ([`interpreter`](#interpreter), [`files`](#files),
83+
[`packages`](#packages), [`plugins`](#plugins) and
84+
[`js_modules`](#javascript-modules).) The user is free to define
8485
arbitrary additional configuration options that plugins or an app may require
8586
for their own reasons.
8687

@@ -288,7 +289,7 @@ The `plugins` option lists plugins enabled by PyScript to add extra
288289
functionality to the platform.
289290

290291
Each plugin should be included on the web page, as described in the
291-
[plugins](#plugins_1) section below. Then the plugin's name should be listed.
292+
[plugins](plugins.md) section. Then the plugin's name should be listed.
292293

293294
```TOML title="A list of plugins in TOML"
294295
plugins = ["custom_plugin", "!error"]

docs/user-guide/editor.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Python editor
2+
3+
The PyEditor is a core plugin.
4+
5+
!!! warning
6+
7+
Work on the Python editor is in its early stages. We have made it available
8+
in this version of PyScript to give the community an opportunity to play,
9+
experiment and provide feedback.
10+
11+
Future versions of PyScript will include a more refined, robust and perhaps
12+
differently behaving version of the Python editor.
13+
14+
If you specify the type of a `<script>` tag as either `py-editor` (for Pyodide)
15+
or `mpy-editor` (for MicroPython), the plugin creates a visual code editor,
16+
with code highlighting and a "run" button to execute the editable code
17+
contained therein in a non-blocking worker.
18+
19+
The interpreter is not loaded onto the page until the run button is clicked. By
20+
default each editor has its own independent instance of the specified
21+
interpreter:
22+
23+
```html title="Two editors, one with Pyodide, the other with MicroPython."
24+
<script type="py-editor">
25+
import sys
26+
print(sys.version)
27+
</script>
28+
<script type="mpy-editor">
29+
import sys
30+
print(sys.version)
31+
a = 42
32+
print(a)
33+
</script>
34+
```
35+
36+
However, different editors can share the same interpreter if they share the
37+
same `env` attribute value.
38+
39+
```html title="Two editors sharing the same MicroPython environment."
40+
<script type="mpy-editor" env="shared">
41+
if not 'a' in globals():
42+
a = 1
43+
else:
44+
a += 1
45+
print(a)
46+
</script>
47+
<script type="mpy-editor" env="shared">
48+
# doubled a
49+
print(a * 2)
50+
</script>
51+
```
52+
53+
The outcome of these code fragments should look something like this:
54+
55+
<img src="../../assets/images/pyeditor1.gif" style="border: 1px solid black; border-radius: 0.2rem; box-shadow: var(--md-shadow-z1);"/>
56+
57+
!!! info
58+
59+
Notice that the interpreter type, and optional environment name is shown
60+
at the top right above the Python editor.
61+
62+
Hovering over the Python editor reveals the "run" button.
63+
64+
Finally, the `target` attribute allows you to specify a node into which the
65+
editor will be rendered:
66+
67+
```html title="Specify a target for the Python editor."
68+
<script type="mpy-editor" target="editor">
69+
import sys
70+
print(sys.version)
71+
</script>
72+
<div id="editor"></div> <!-- will eventually contain the Python editor -->
73+
```

docs/user-guide/examples.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
11
# Example applications
22

3-
** TODO: fill this with references and descriptions to examples on PyScript.com **
3+
A curated list of example applications that demonstrate various features of
4+
PyScript can be found [on PyScript.com](https://pyscript.com/@examples).
5+
6+
The examples are (links take you to the code):
7+
8+
* [Hello world](https://pyscript.com/@examples/hello-world/latest)
9+
* [A simple clock](https://pyscript.com/@examples/simple-clock/latest)
10+
* [Simple slider panel](https://pyscript.com/@examples/simple-panel/latest)
11+
* [Streaming data panel](https://pyscript.com/@examples/streaming-in-panel/latest)
12+
* [WebGL Icosahedron](https://pyscript.com/@examples/webgl-icosahedron/latest)
13+
* [KMeans in a panel](https://pyscript.com/@examples/kmeans-in-panel/latest)
14+
* [New York Taxi panel (WebGL)](https://pyscript.com/@examples/nyc-taxi-panel-deckgl/latest)
15+
* [Pandas dataframe fun](https://pyscript.com/@examples/pandas/latest)
16+
* [Matplotlib example](https://pyscript.com/@examples/matplotlib/latest)
17+
* [Numpy fractals](https://pyscript.com/@examples/fractals-with-numpy-and-canvas/latest)
18+
* [Folium geographical data](https://pyscript.com/@examples/folium/latest)
19+
* [Bokeh data plotting](https://pyscript.com/@examples/bokeh/latest)
20+
* [Import antigravity](https://pyscript.com/@examples/antigravity/latest)
21+
* [Altair data plotting](https://pyscript.com/@examples/altair/latest)
22+

docs/user-guide/features.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
<dl>
44
<dt><em>All the web</em></dt>
55
<dd>
6-
<p>Pyscript gives you <a href="#the-dom">full access to the DOM</a> and all
6+
<p>Pyscript gives you <a href="../dom">full access to the DOM</a> and all
77
the <a href="https://developer.mozilla.org/en-US/docs/Web/API">web
88
APIs implemented by your browser</a>.</p>
99

10-
<p>Thanks to the <a href="#ffi">foreign
10+
<p>Thanks to the <a href="../dom#ffi">foreign
1111
function interface</a> (FFI), Python just works with all the browser has to
1212
offer, including any third party JavaScript libraries that may be included
1313
in the page.</p>
@@ -19,10 +19,10 @@
1919
<dd>
2020
<p>PyScript brings you two Python interpreters:</p>
2121
<ol>
22-
<li><a href="#pyodide">Pyodide</a> - the original standard
22+
<li><a href="../architecture#pyodide">Pyodide</a> - the original standard
2323
CPython interpreter you know and love, but compiled to WebAssembly.
2424
</li>
25-
<li><a href="#micropython">MicroPython</a> - a lean and
25+
<li><a href="../architecture#micropython">MicroPython</a> - a lean and
2626
efficient reimplementation of Python3 that includes a comprehensive
2727
subset of the standard library, compiled to WebAssembly.</li>
2828
</ol>
@@ -38,7 +38,7 @@
3838
library and efficiently exposes the expressiveness of Python to the
3939
browser.</p>
4040
<p>Both Python interpreters supported by PyScript implement the
41-
<a href="#ffi">same FFI</a> to bridge the gap between the worlds of Python
41+
<a href="../dom#ffi">same FFI</a> to bridge the gap between the worlds of Python
4242
and the browser.</p>
4343
</dd>
4444

@@ -62,15 +62,15 @@
6262
expensive and blocking computation can run somewhere other than the main
6363
application thread controlling the user interface. When such work is done
6464
on the main thread, the browser appears frozen; web workers ensure
65-
expensive blocking computation <a href="#workers">happens elsewhere</a>.
65+
expensive blocking computation <a href="../workers">happens elsewhere</a>.
6666
Think of workers as independent subprocesses in your web page.</dd>
6767

6868
<dt><em>Rich and powerful plugins</em></dt>
6969
<dd>
7070
<p>PyScript has a small, efficient yet powerful core called
7171
<a href="https://github.com/pyscript/polyscript">PolyScript</a>. Most of
7272
the functionality of PyScript is actually implemented through PolyScript's
73-
<a href="#plugins_1">plugin system</a>.</p>
73+
<a href="../plugins">plugin system</a>.</p>
7474

7575
<p>This approach ensures a clear separation of concerns: PolyScript
7676
can focus on being small, efficient and powerful, whereas the PyScript

docs/user-guide/first-steps.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ CSS:
2020
<meta charset="UTF-8">
2121
<meta name="viewport" content="width=device-width,initial-scale=1.0">
2222
<!-- PyScript CSS -->
23-
<link rel="stylesheet" href="https://pyscript.net/releases/2023.11.2/core.css">
23+
<link rel="stylesheet" href="https://pyscript.net/releases/2023.12.1/core.css">
2424
<!-- This script tag bootstraps PyScript -->
25-
<script type="module" src="https://pyscript.net/releases/2023.11.2/core.js"></script>
25+
<script type="module" src="https://pyscript.net/releases/2023.12.1/core.js"></script>
2626
</head>
2727
<body>
2828
<!-- your code goes here... -->

docs/user-guide/plugins.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Here's an example of how a PyScript plugin looks like:
1414

1515
```js
1616
// import the hooks from PyScript first...
17-
import { hooks } from "https://pyscript.net/releases/2023.11.2/core.js";
17+
import { hooks } from "https://pyscript.net/releases/2023.12.1/core.js";
1818

1919
// Use the `main` attribute on hooks do define plugins that run on the main thread
2020
hooks.main.onReady.add((wrap, element) => {

docs/user-guide/terminal.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,17 @@ name = input("What is your name? ")
4545
print(f"Hello, {name}")
4646
</script>
4747
```
48+
<img src="../../assets/images/pyterm2.gif" style="border: 1px solid black; border-radius: 0.2rem; box-shadow: var(--md-shadow-z1);"/>
4849

49-
Once the Python code has finished you are automatically dropped into the
50-
Python REPL, like this:
50+
To use the interactive Python REPL in the terminal, use Python's
51+
[code](https://docs.python.org/3/library/code.html) module like this:
5152

52-
<img src="../../assets/images/pyterm2.gif" style="border: 1px solid black; border-radius: 0.2rem; box-shadow: var(--md-shadow-z1);"/>
53+
```python
54+
import code
55+
56+
code.interact()
57+
```
5358

59+
The end result should look something like this:
5460

61+
<img src="../../assets/images/pyterm3.gif" style="border: 1px solid black; border-radius: 0.2rem; box-shadow: var(--md-shadow-z1);"/>

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,6 @@ nav:
7575
- Workers: user-guide/workers.md
7676
- Builtin helpers: user-guide/builtins.md
7777
- Python terminal: user-guide/terminal.md
78+
- Python editor: user-guide/editor.md
7879
- Plugins: user-guide/plugins.md
7980
- Example apps: user-guide/examples.md

version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"version": "2023.11.2"
2+
"version": "2023.12.1"
33
}

0 commit comments

Comments
 (0)