Skip to content

Commit 3c28bb7

Browse files
Merge pull request #72 from pyscript/2024-3-1
Bump to 2024-3-1 and add docs for setup in editor, and new fetch in pyscript namespace.
2 parents 86c5be9 + 8378989 commit 3c28bb7

File tree

6 files changed

+97
-8
lines changed

6 files changed

+97
-8
lines changed

docs/beginning-pyscript.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ 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-
<link rel="stylesheet" href="https://pyscript.net/releases/2024.2.1/core.css">
110-
<script type="module" src="https://pyscript.net/releases/2024.2.1/core.js"></script>
109+
<link rel="stylesheet" href="https://pyscript.net/releases/2024.3.1/core.css">
110+
<script type="module" src="https://pyscript.net/releases/2024.3.1/core.js"></script>
111111
</head>
112112
<body>
113113

@@ -157,8 +157,8 @@ In the end, our HTML should look like this:
157157
<meta charset="utf-8" />
158158
<meta name="viewport" content="width=device-width,initial-scale=1" />
159159
<title>🦜 Polyglot - Piratical PyScript</title>
160-
<link rel="stylesheet" href="https://pyscript.net/releases/2024.2.1/core.css">
161-
<script type="module" src="https://pyscript.net/releases/2024.2.1/core.js"></script>
160+
<link rel="stylesheet" href="https://pyscript.net/releases/2024.3.1/core.css">
161+
<script type="module" src="https://pyscript.net/releases/2024.3.1/core.js"></script>
162162
</head>
163163
<body>
164164
<h1>Polyglot 🦜 💬 🇬🇧 ➡️ 🏴‍☠️</h1>

docs/user-guide/builtins.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,70 @@ Such named modules will always then be available under the
116116
Please see the documentation (linked above) about restrictions and gotchas
117117
when configuring how JavaScript modules are made available to PyScript.
118118

119+
### `pyscript.fetch`
120+
121+
A common task is to `fetch` data from the web via HTTP requests. The
122+
`pyscript.fetch` function provides a uniform way to achieve this in both
123+
Pyodide and MicroPython. It is closely modelled on the
124+
[Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) found
125+
in browsers with some important Pythonic differences.
126+
127+
The simple use case is to pass in a URL and `await` the response. Remember, in
128+
order to use `await` you must have the `async` attribute in the `script` tag
129+
that references your code. If this request is in a function, that function
130+
should also be defined as `async`.
131+
132+
```python title="A simple HTTP GET with pyscript.fetch"
133+
from pyscript import fetch
134+
135+
136+
response = await fetch("https://example.com").text()
137+
```
138+
139+
If the `fetch` operation _causes a response that is not deemed `OK`_ (the
140+
definition of which
141+
[can be found here](https://developer.mozilla.org/en-US/docs/Web/API/Response/ok))
142+
then an exception is raised.
143+
144+
Assuming an `OK` response, the following methods are available to you to access
145+
the data returned from the server:
146+
147+
* `await fetch("https://example.com").arrayBuffer()` returns a Python [memoryview](https://docs.python.org/3/library/stdtypes.html#memoryview) of the response. This is equivalent to the [`arrayBuffer()` method](https://developer.mozilla.org/en-US/docs/Web/API/Response/arrayBuffer) in the browser based `fetch` API.
148+
* `await fetch("https://example.com").blob()` returns a JavaScript [`blob`](https://developer.mozilla.org/en-US/docs/Web/API/Response/blob) version of the response. This is equivalent
149+
to the [`blob()` method](https://developer.mozilla.org/en-US/docs/Web/API/Response/blob) in the browser based `fetch` API.
150+
* `await fetch("https://example.com").bytearray()` returns a Python [`bytearray`](https://docs.python.org/3/library/stdtypes.html#bytearray) version of the response.
151+
* `await fetch("https://example.com").json()` returns a Python datastructure representing a JSON serialised payload in the response.
152+
* `await fetch("https://example.com").text()` returns a Python string version of the response.
153+
154+
The underlying browser `fetch` API has [many request options](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#supplying_request_options)
155+
that you should simply pass in as keyword arguments like this:
156+
157+
```python title="Supplying request options."
158+
from pyscript import fetch
159+
160+
161+
response = await fetch("https://example.com", method="POST", body="HELLO").text()
162+
```
163+
164+
Should you need access to the underlying [JavaScript response object](https://developer.mozilla.org/en-US/docs/Web/API/Response), you can find it as `response._response()`.
165+
166+
!!! Danger
167+
168+
You may encounter [CORS](https://developer.mozilla.org/en-US/docs/Glossary/CORS) errors (especially with reference to a missing
169+
[Access-Control-Allow-Origin header](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSMissingAllowOrigin).
170+
171+
This is a security feature of modern browsers where the site to which you
172+
are making a request **will not process a request from a site hosted at
173+
another domain**.
174+
175+
For example, if your PyScript app is hosted under `example.com` and you
176+
make a request to `bbc.co.uk` (who don't allow requests from other domains)
177+
then you'll encounter this sort of CORS related error.
178+
179+
There is nothing PyScript can do about this problem (it's a feature, not a
180+
bug). However, you could use a pass-through proxy service to get around
181+
this limitation (i.e. the proxy service makes the call on your behalf).
182+
119183
## Main-thread only features
120184

121185
### `pyscript.PyWorker`

docs/user-guide/editor.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,31 @@ The outcome of these code fragments should look something like this:
5454

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

57+
Sometimes you need to create a pre-baked Pythonic context for a shared
58+
environment used by an editor. This need is especially helpful in educational
59+
situations where boilerplate code can be run, with just the important salient
60+
code available in the editor.
61+
62+
To achieve this end use the `setup` attribute within a `script` tag. The
63+
content of this editor will not be shown, but will bootstrap the referenced
64+
environment automatically before any following editor within the same
65+
environment is evaluated.
66+
67+
```html title="Bootstrapping an environment with `setup`"
68+
<script type="mpy-editor" env="test_env" setup>
69+
# This code will not be visible, but will run before the next editor's code is
70+
# evaluated.
71+
a = 1
72+
</script>
73+
74+
<script type="mpy-editor" env="test_env">
75+
# Without the "setup" attribute, this editor is visible. Because it is using
76+
# the same env as the previous "setup" editor, the previous editor's code is
77+
# always evaluated first.
78+
print(a)
79+
</script>
80+
```
81+
5782
!!! info
5883
5984
Notice that the interpreter type, and optional environment name is shown

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.2.1/core.css">
23+
<link rel="stylesheet" href="https://pyscript.net/releases/2024.3.1/core.css">
2424
<!-- This script tag bootstraps PyScript -->
25-
<script type="module" src="https://pyscript.net/releases/2024.2.1/core.js"></script>
25+
<script type="module" src="https://pyscript.net/releases/2024.3.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/2024.2.1/core.js";
17+
import { hooks } from "https://pyscript.net/releases/2024.3.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) => {

version.json

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

0 commit comments

Comments
 (0)