Skip to content

Commit e4a5315

Browse files
committed
Updated API source.
1 parent 8f4fbd5 commit e4a5315

13 files changed

Lines changed: 54 additions & 54 deletions

File tree

pyscript/__init__.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
This is the main `pyscript` namespace. It provides the primary Pythonic API
33
for users to interact with the
4-
[browser's own API](https://developer.mozilla.org/en-US/docs/Web/API). It
4+
[browser's own API](https://developer.mozilla.org/en-US/docs/Web/API). It
55
includes utilities for common activities such as displaying content, handling
66
events, fetching resources, managing local storage, and coordinating with
77
web workers.
@@ -47,7 +47,7 @@
4747
4848
4949
!!! Note
50-
Some notes about the naming conventions and the relationship between
50+
Some notes about the naming conventions and the relationship between
5151
various similar-but-different names found within this code base.
5252
5353
```python
@@ -63,17 +63,18 @@
6363
```
6464
6565
The `_pyscript` module is an internal API implemented in JS. **End users
66-
should not use it directly**. For its implementation, grep for
66+
should not use it directly**. For its implementation, grep for
6767
`interpreter.registerJsModule("_pyscript",...)` in `core.js`.
6868
6969
```python
7070
import js
7171
```
7272
73-
The `js` object is the JS `globalThis`, as exported by Pyodide and/or
74-
Micropython's foreign function interface (FFI). As such, it contains
75-
different things in the main thread or in a worker, as defined by web
76-
standards.
73+
The `js` object is
74+
[the JS `globalThis`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis),
75+
as exported by Pyodide and/or Micropython's foreign function interface
76+
(FFI). As such, it contains different things in the main thread or in a
77+
worker, as defined by web standards.
7778
7879
```python
7980
import pyscript.context
@@ -85,7 +86,7 @@
8586
mostly for internal PyScript use or advanced users. In particular, it
8687
defines `window` and `document` in such a way that these names work in
8788
both cases: in the main thread, they are the "real" objects, in a worker
88-
they are proxies which work thanks to
89+
they are proxies which work thanks to
8990
[coincident](https://github.com/WebReflection/coincident).
9091
9192
```python

pyscript/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
This module handles the differences between running in the
55
[main browser thread](https://developer.mozilla.org/en-US/docs/Glossary/Main_thread)
6-
versus running in a
6+
versus running in a
77
[Web Worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers),
88
providing a consistent API regardless of the execution context.
99

pyscript/events.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __init__(self):
4949

5050
def trigger(self, result):
5151
"""
52-
Trigger the event and notify all listeners with the given result.
52+
Trigger the event and notify all listeners with the given `result`.
5353
"""
5454
for listener in self._listeners:
5555
if is_awaitable(listener):
@@ -61,7 +61,7 @@ def add_listener(self, listener):
6161
"""
6262
Add a function to be called when this event is triggered.
6363
64-
The listener must be callable. It can be either a regular function
64+
The `listener` must be callable. It can be either a regular function
6565
or an async function. Duplicate listeners are ignored.
6666
"""
6767
if not callable(listener):
@@ -72,7 +72,7 @@ def add_listener(self, listener):
7272

7373
def remove_listener(self, *listeners):
7474
"""
75-
Remove specified listeners. If none specified, remove all listeners.
75+
Remove specified `listeners`. If none specified, remove all listeners.
7676
"""
7777
if listeners:
7878
for listener in listeners:
@@ -166,7 +166,7 @@ def decorator(func):
166166

167167
def _get_elements(selector):
168168
"""
169-
Convert various selector types into a list of DOM elements.
169+
Convert various `selector` types into a list of DOM elements.
170170
"""
171171
from pyscript.web import Element, ElementCollection
172172

@@ -184,7 +184,7 @@ def _get_elements(selector):
184184

185185
def _create_wrapper(func):
186186
"""
187-
Create an appropriate wrapper for the given function.
187+
Create an appropriate wrapper for the given function, `func`.
188188
189189
The wrapper handles both sync and async functions, and respects whether
190190
the function expects to receive event arguments.

pyscript/fetch.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
This module provides a Python-friendly interface to the
2+
This module provides a Python-friendly interface to the
33
[browser's fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API),
44
returning native Python data types and supporting directly awaiting the promise
55
and chaining method calls directly on the promise.
@@ -156,15 +156,13 @@ def fetch(url, **options):
156156
157157
The function takes a `url` and optional fetch `options` as keyword
158158
arguments. The `options` correspond to the JavaScript fetch API's
159-
RequestInit dictionary, and commonly include:
159+
[RequestInit dictionary](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit),
160+
and commonly include:
160161
161162
- `method`: HTTP method (e.g., `"GET"`, `"POST"`, `"PUT"` etc.)
162163
- `headers`: Dict of request headers.
163164
- `body`: Request body (string, dict for JSON, etc.)
164165
165-
See [this documentation](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit)
166-
for more details of these web standards.
167-
168166
The function returns a promise that resolves to a Response-like object
169167
with Pythonic methods to extract data:
170168

pyscript/ffi.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""
2-
This module provides a unified Foreign Function Interface (FFI) layer that
3-
works consistently across both Pyodide and MicroPython, and in worker or main
4-
thread contexts, abstracting away the differences in their JavaScript interop
5-
APIs.
2+
This module provides a unified
3+
[Foreign Function Interface (FFI)](https://en.wikipedia.org/wiki/Foreign_function_interface)
4+
layer for Python/JavaScript interactions, that works consistently across both
5+
Pyodide and MicroPython, and in a worker or main thread context, abstracting
6+
away the differences in their JavaScript interop APIs.
67
78
The following utilities work on both the main thread and in worker contexts:
89
@@ -74,7 +75,7 @@ def to_js(value, **kw):
7475
"""
7576
Convert Python objects to JavaScript objects.
7677
77-
This ensures a Python `dict` becomes a
78+
This ensures a Python `dict` becomes a
7879
[proper JavaScript object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
7980
rather a JavaScript [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map),
8081
which is more intuitive for most use cases.
@@ -86,7 +87,7 @@ def to_js(value, **kw):
8687
from pyscript import ffi
8788
import js
8889
89-
90+
9091
note = {
9192
"body": "This is a notification",
9293
"icon": "icon.png"
@@ -111,7 +112,7 @@ def is_none(value):
111112
from pyscript import ffi
112113
import js
113114
114-
115+
115116
val1 = None
116117
val2 = js.null
117118
val3 = 42

pyscript/flatted.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
This module is a Python implementation of the
2+
This module is a Python implementation of the
33
[Flatted JavaScript library](https://www.npmjs.com/package/flatted), which
44
provides a light and fast way to serialize and deserialize JSON structures
55
that contain circular references.
@@ -157,7 +157,7 @@ def parse(value, *args, **kwargs):
157157
```python
158158
from pyscript import flatted
159159
160-
160+
161161
# Parse a Flatted JSON string.
162162
json_string = '[{"name": "1", "self": "0"}, "parent"]'
163163
obj = flatted.parse(json_string)
@@ -202,7 +202,7 @@ def stringify(value, *args, **kwargs):
202202
```python
203203
from pyscript import flatted
204204
205-
205+
206206
# Create an object with a circular reference.
207207
parent = {"name": "parent", "children": []}
208208
child = {"name": "child", "parent": parent}

pyscript/fs.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
!!! warning
77
**This API only works in Chromium-based browsers** (Chrome, Edge,
8-
Opera, Brave, etc.) that support the
8+
Vivaldi, Brave, etc.) that support the
99
[File System Access API](https://wicg.github.io/file-system-access/).
1010
1111
The module maintains a `mounted` dictionary that tracks all currently mounted
@@ -78,7 +78,7 @@ async def mount(path, mode="readwrite", root="", id="pyscript"):
7878
```python
7979
from pyscript import fs
8080
81-
81+
8282
# Basic mount with default settings.
8383
await fs.mount("/local")
8484
@@ -162,7 +162,7 @@ async def sync(path):
162162
```python
163163
from pyscript import fs
164164
165-
165+
166166
await fs.mount("/local")
167167
168168
# Make changes to files.
@@ -194,7 +194,7 @@ async def unmount(path):
194194
```python
195195
from pyscript import fs
196196
197-
197+
198198
await fs.mount("/local")
199199
# ... work with files ...
200200
await fs.unmount("/local")
@@ -227,7 +227,7 @@ async def revoke(path, id="pyscript"):
227227
```python
228228
from pyscript import fs
229229
230-
230+
231231
await fs.mount("/local", id="my-app")
232232
# ... work with files ...
233233

pyscript/media.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Device:
4141
"""
4242
Represents a media input or output device.
4343
44-
This class wraps a browser
44+
This class wraps a browser
4545
[MediaDeviceInfo object](https://developer.mozilla.org/en-US/docs/Web/API/MediaDeviceInfo),
4646
providing Pythonic access to device properties like `ID`, `label`, and
4747
`kind` (audio/video, input/output).
@@ -52,7 +52,7 @@ class Device:
5252
```python
5353
from pyscript.media import list_devices
5454
55-
55+
5656
# Get all available devices.
5757
devices = await list_devices()
5858
@@ -135,7 +135,7 @@ async def request_stream(cls, audio=False, video=True):
135135
from pyscript import document
136136
from pyscript.media import Device
137137
138-
138+
139139
# Get default video stream.
140140
stream = await Device.request_stream()
141141
@@ -185,7 +185,7 @@ async def get_stream(self):
185185
```python
186186
from pyscript.media import list_devices
187187
188-
188+
189189
# List all devices.
190190
devices = await list_devices()
191191
@@ -219,7 +219,7 @@ async def list_devices():
219219
```python
220220
from pyscript.media import list_devices
221221
222-
222+
223223
# Get all devices.
224224
devices = await list_devices()
225225

pyscript/storage.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class Storage(dict):
109109
```python
110110
from pyscript import storage
111111
112-
112+
113113
# Open a storage.
114114
prefs = await storage("preferences")
115115
@@ -130,7 +130,7 @@ class Storage(dict):
130130
```python
131131
from pyscript import storage, Storage, window
132132
133-
133+
134134
class LoggingStorage(Storage):
135135
def __setitem__(self, key, value):
136136
window.console.log(f"Setting {key} = {value}")
@@ -220,7 +220,7 @@ async def storage(name="", storage_class=Storage):
220220
```python
221221
from pyscript import storage
222222
223-
223+
224224
# Basic usage.
225225
user_data = await storage("user-profile")
226226
user_data["name"] = "Alice"

pyscript/util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def is_awaitable(obj):
5656
"""
5757
Returns a boolean indication if the passed in obj is an awaitable
5858
function. This is interpreter agnostic.
59-
60-
!!! info
59+
60+
!!! info
6161
MicroPython treats awaitables as generator functions, and if
6262
the object is a closure containing an async function or a bound method
6363
we need to work carefully.

0 commit comments

Comments
 (0)