Skip to content

Commit ceacdb8

Browse files
committed
Fix broken links, add documentation updates.
1 parent 6d14e95 commit ceacdb8

File tree

6 files changed

+81
-19
lines changed

6 files changed

+81
-19
lines changed

docs/beginning-pyscript.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

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: 63 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,53 @@ 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. The
182+
`*` is replaced by the name of event, in a similar fashion to how all
183+
[event handlers on elements](https://html.spec.whatwg.org/multipage/webappapis.html#event-handlers-on-elements,-document-objects,-and-window-objects)
184+
start with `on` (e.g. `onclick`) in standard HTML. The rule of thumb is to
185+
simply replace `on` with `py-` and then reference the name of a Python
186+
function.
187+
188+
```html title="A py-click event on an HTML button element."
189+
<button py-click="handle_click" id="my_button">Click me!</button>
190+
```
191+
192+
```python title="The related Python function."
193+
from pyscript import window
194+
195+
196+
def handle_click(event):
197+
"""
198+
Simply log the click event to the browser's console.
199+
"""
200+
window.console.log(event)
201+
```
202+
203+
Under the hood, the [`pyscript.when`](#pyscriptwhen) decorator is used to
204+
enable this behaviour.
205+
206+
!!! note
207+
208+
In earlier versions of PyScript, the value associated with the attribute
209+
was simply evaluated by the Python interpreter. This was unsafe:
210+
manipulation of the attribute's value could have resulted in the evaluation
211+
of arbitrary code.
212+
213+
This is why we changed to the current behaviour of just supplying the name
214+
of the Python function to be evaluated.

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/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

0 commit comments

Comments
 (0)