Skip to content

Commit 109138d

Browse files
authored
Merge pull request #130 from pyscript/2024-6-2
2024 6 2
2 parents e3c26f1 + ad9a2e7 commit 109138d

File tree

7 files changed

+140
-14
lines changed

7 files changed

+140
-14
lines changed

docs/beginning-pyscript.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ module in the document's `<head>` tag:
112112
<meta charset="utf-8" />
113113
<meta name="viewport" content="width=device-width,initial-scale=1" />
114114
<title>🦜 Polyglot - Piratical PyScript</title>
115-
<link rel="stylesheet" href="https://pyscript.net/releases/2024.6.1/core.css">
116-
<script type="module" src="https://pyscript.net/releases/2024.6.1/core.js"></script>
115+
<link rel="stylesheet" href="https://pyscript.net/releases/2024.6.2/core.css">
116+
<script type="module" src="https://pyscript.net/releases/2024.6.2/core.js"></script>
117117
</head>
118118
<body>
119119

@@ -163,8 +163,8 @@ In the end, our HTML should look like this:
163163
<meta charset="utf-8" />
164164
<meta name="viewport" content="width=device-width,initial-scale=1" />
165165
<title>🦜 Polyglot - Piratical PyScript</title>
166-
<link rel="stylesheet" href="https://pyscript.net/releases/2024.6.1/core.css">
167-
<script type="module" src="https://pyscript.net/releases/2024.6.1/core.js"></script>
166+
<link rel="stylesheet" href="https://pyscript.net/releases/2024.6.2/core.css">
167+
<script type="module" src="https://pyscript.net/releases/2024.6.2/core.js"></script>
168168
</head>
169169
<body>
170170
<h1>Polyglot 🦜 💬 🇬🇧 ➡️ 🏴‍☠️</h1>

docs/user-guide/builtins.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,80 @@ The following code demonstrates a `pyscript.WebSocket` in action.
372372
ws = WebSocket(url="ws://example.com/socket", onmessage=onmessage)
373373
```
374374

375+
### `pyscript.storage`
376+
377+
The `pyscript.storage` API wraps the browser's built-in
378+
[IndexDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API)
379+
persistent storage in a synchronous Pythonic API.
380+
381+
!!! info
382+
383+
The storage API is persistent per user tab, page, or domain, in the same
384+
way IndexedDB persists.
385+
386+
This API **is not** saving files in the interpreter's virtual file system
387+
nor onto the user's hard drive.
388+
389+
```python
390+
from pyscript import storage
391+
392+
393+
# Each store must have a meaningful name.
394+
store = await storage("my-storage-name")
395+
396+
# store is a dictionary and can now be used as such.
397+
```
398+
399+
The returned dictionary automatically loads the current state of the referenced
400+
IndexDB. All changes are automatically queued in the background.
401+
402+
```python
403+
# This is a write operation.
404+
store["key"] = value
405+
406+
# This is also a write operation (it changes the stored data).
407+
del store["key"]
408+
```
409+
410+
Should you wish to be certain changes have been synchronized to the underlying
411+
IndexDB, just `await store.sync()`.
412+
413+
Common types of value can be stored via this API: `bool`, `float`, `int`, `str`
414+
and `None`. In addition, data structures like `list`, `dict` and `tuple` can
415+
be stored.
416+
417+
!!! warning
418+
419+
Because of the way the underlying data structure are stored in IndexDB,
420+
a Python `tuple` will always be returned as a Python `list`.
421+
422+
It is even possible to store arbitrary data via a `bytearray` or
423+
`memoryview` object. However, there is a limitation that **such values must be
424+
stored as a single key/value pair, and not as part of a nested data
425+
structure**.
426+
427+
Sometimes you may need to modify the behaviour of the `dict` like object
428+
returned by `pyscript.storage`. To do this, create a new class that inherits
429+
from `pyscript.Storage`, then pass in your class to `pyscript.storage` as the
430+
`storage_class` argument:
431+
432+
```python
433+
from pyscript import window, storage, Storage
434+
435+
436+
class MyStorage(Storage):
437+
438+
def __setitem__(self, key, value):
439+
super().__setitem__(key, value)
440+
window.console.log(key, value)
441+
...
442+
443+
444+
store = await storage("my-data-store", storage_class=MyStorage)
445+
446+
# The store object is now an instance of MyStorage.
447+
```
448+
375449
### `pyscript.ffi.to_js`
376450

377451
A utility function to convert Python references into their JavaScript

docs/user-guide/editor.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,58 @@ If a `setup` editor is present, that's the only PyEditor that needs a config.
173173
Any subsequent related editor will reuse the config parsed and bootstrapped for
174174
the `setup` editor.
175175

176+
## Run via keyboard
177+
178+
Depending on your operating system, a combination of either `Ctrl-Enter`,
179+
`Cmd-Enter` or `Shift-Enter` will execute the code in the editor (no need to
180+
move the mouse to click the run button).
181+
182+
## Override run
183+
184+
Sometimes you just need to override the way the editor runs code.
185+
186+
The editor's `handleEvent` can be overridden to achieve this:
187+
188+
```html title="Overriding execution via handleEvent."
189+
<script type="mpy-editor" id="foreign">
190+
print(6 * 7)
191+
</script>
192+
193+
<script type="mpy">
194+
from pyscript import document
195+
196+
def handle_event(event):
197+
# will log `print(6 * 7)`
198+
print(event.code)
199+
# prevent default execution
200+
return False
201+
202+
# Grab reference to the editor
203+
foreign = document.getElementById("foreign")
204+
# Override handleEvent with your own customisation.
205+
foreign.handleEvent = handle_event
206+
</script>
207+
```
208+
209+
This
210+
[live example](https://agiammarchi.pyscriptapps.com/pyeditor-iot-example/latest/)
211+
shows how the editor can be used to execute code via a USB serial connection to
212+
a connected MicroPython microcontroller.
213+
214+
## Tab behavior
215+
216+
We currently trap the `tab` key in a way that reflects what a regular code
217+
editor would do: the code is simply indented, rather than focus moving to
218+
another element.
219+
220+
We are fully aware of the implications this might have around accessibility so
221+
we followed
222+
[this detailed advice from Codemirror's documentation](https://codemirror.net/examples/tab/)
223+
We have an *escape hatch* to move focus outside the editor. Press `esc` before
224+
`tab` to move focus to the next focusable element. Otherwise `tab` indents
225+
code.
226+
227+
176228
## Still missing
177229

178230
The PyEditor is currently under active development and refinement, so features

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/2024.6.1/core.css">
23+
<link rel="stylesheet" href="https://pyscript.net/releases/2024.6.2/core.css">
2424
<!-- This script tag bootstraps PyScript -->
25-
<script type="module" src="https://pyscript.net/releases/2024.6.1/core.js"></script>
25+
<script type="module" src="https://pyscript.net/releases/2024.6.2/core.js"></script>
2626
</head>
2727
<body>
2828
<!-- your code goes here... -->

docs/user-guide/plugins.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ For example, this will work because all references are contained within the
9999
registered function:
100100

101101
```js
102-
import { hooks } from "https://pyscript.net/releases/2024.6.1/core.js";
102+
import { hooks } from "https://pyscript.net/releases/2024.6.2/core.js";
103103

104104
hooks.worker.onReady.add(() => {
105105
// NOT suggested, just an example!
@@ -113,7 +113,7 @@ hooks.worker.onReady.add(() => {
113113
However, due to the outer reference to the variable `i`, this will fail:
114114

115115
```js
116-
import { hooks } from "https://pyscript.net/releases/2024.6.1/core.js";
116+
import { hooks } from "https://pyscript.net/releases/2024.6.2/core.js";
117117

118118
// NO NO NO NO NO! ☠️
119119
let i = 0;
@@ -146,7 +146,7 @@ the page.
146146

147147
```js title="log.js - a plugin that simply logs to the console."
148148
// import the hooks from PyScript first...
149-
import { hooks } from "https://pyscript.net/releases/2024.6.1/core.js";
149+
import { hooks } from "https://pyscript.net/releases/2024.6.2/core.js";
150150

151151
// The `hooks.main` attribute defines plugins that run on the main thread.
152152
hooks.main.onReady.add((wrap, element) => {
@@ -196,8 +196,8 @@ hooks.worker.onAfterRun.add(() => {
196196
<!-- JS plugins should be available before PyScript bootstraps -->
197197
<script type="module" src="./log.js"></script>
198198
<!-- PyScript -->
199-
<link rel="stylesheet" href="https://pyscript.net/releases/2024.6.1/core.css">
200-
<script type="module" src="https://pyscript.net/releases/2024.6.1/core.js"></script>
199+
<link rel="stylesheet" href="https://pyscript.net/releases/2024.6.2/core.css">
200+
<script type="module" src="https://pyscript.net/releases/2024.6.2/core.js"></script>
201201
</head>
202202
<body>
203203
<script type="mpy">

docs/user-guide/workers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ Here's how:
164164
<meta charset="utf-8">
165165
<meta name="viewport" content="width=device-width,initial-scale=1">
166166
<!-- PyScript CSS -->
167-
<link rel="stylesheet" href="https://pyscript.net/releases/2024.6.1/core.css">
167+
<link rel="stylesheet" href="https://pyscript.net/releases/2024.6.2/core.css">
168168
<!-- This script tag bootstraps PyScript -->
169-
<script type="module" src="https://pyscript.net/releases/2024.6.1/core.js"></script>
169+
<script type="module" src="https://pyscript.net/releases/2024.6.2/core.js"></script>
170170
<title>PyWorker - mpy bootstrapping pyodide example</title>
171171
<!-- the async attribute is useful but not mandatory -->
172172
<script type="mpy" src="main.py" async></script>

version.json

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

0 commit comments

Comments
 (0)