From 23220b746c0c74ccd234dffbec6449c019062310 Mon Sep 17 00:00:00 2001 From: fpliger Date: Wed, 20 Mar 2024 16:24:13 +0000 Subject: [PATCH] Deployed af47c01 to 2024.3.1 with MkDocs 1.5.3 and mike 1.1.2 --- 2024.3.1/sitemap.xml.gz | Bin 127 -> 127 bytes 2024.3.1/user-guide/builtins/index.html | 91 +++++++++++++----------- 2 files changed, 51 insertions(+), 40 deletions(-) diff --git a/2024.3.1/sitemap.xml.gz b/2024.3.1/sitemap.xml.gz index d30d8d88cde8d161784a6d6fff8fbbc18519b891..e0976248b482087fa18d948cbe28df98f44a4534 100644 GIT binary patch delta 13 Ucmb=gXP58h;8?@^dm?)U03E;tga7~l delta 13 Ucmb=gXP58h;Al4eGLgLk03CJ&d;kCd diff --git a/2024.3.1/user-guide/builtins/index.html b/2024.3.1/user-guide/builtins/index.html index f269f97..6f287c0 100644 --- a/2024.3.1/user-guide/builtins/index.html +++ b/2024.3.1/user-guide/builtins/index.html @@ -1021,30 +1021,41 @@

pyscript.fetch

A simple HTTP GET with pyscript.fetch
from pyscript import fetch
 
 
-response = await fetch("https://example.com").text()
+response = await fetch("https://example.com")
+if response.ok:
+    data = await response.text()
+else:
+    print(response.status)
 
-

If the fetch operation causes a response that is not deemed OK (the -definition of which -can be found here) -then an exception is raised.

-

Assuming an OK response, the following methods are available to you to access -the data returned from the server:

+

The object returned from an await fetch call will have attributes that +correspond to the +JavaScript response object. +This is useful for getting response codes, headers and other metadata before +processing the response's data.

+

Alternatively, rather than using a double await (one to get the response, the +other to grab the data), it's possible to chain the calls into a single +await like this:

+
A simple HTTP GET as a single await
from pyscript import fetch
+
+data = await fetch("https://example.com").text()
+
+

The following awaitable methods are available to you to access the data +returned from the server:

The underlying browser fetch API has many request options that you should simply pass in as keyword arguments like this:

-
Supplying request options.
from pyscript import fetch
-
-
-response = await fetch("https://example.com", method="POST", body="HELLO").text()
+
Supplying request options.
from pyscript import fetch
+
+
+response = await fetch("https://example.com", method="POST", body="HELLO").text()
 
-

Should you need access to the underlying JavaScript response object, you can find it as response._response().

Danger

You may encounter CORS errors (especially with reference to a missing @@ -1068,28 +1079,28 @@

pyscript.PyWorker

The following fragment demonstrates who to start the Python code in the file worker.py on a new worker from within Python.

-
Starting a new worker from Python
from pyscript import PyWorker
-
-
-a_worker = PyWorker("./worker.py")
+
Starting a new worker from Python
from pyscript import PyWorker
+
+
+a_worker = PyWorker("./worker.py")
 

Worker only features

pyscript.sync

A function used to pass serializable data from workers to the main thread.

Imagine you have this code on the main thread:

-
Python code on the main thread
from pyscript import PyWorker
-
-def hello(name="world"):
-    display(f"Hello, {name}")
-
-worker = PyWorker("./worker.py")
-worker.sync.hello = hello
+
Python code on the main thread
from pyscript import PyWorker
+
+def hello(name="world"):
+    display(f"Hello, {name}")
+
+worker = PyWorker("./worker.py")
+worker.sync.hello = hello
 

In the code on the worker, you can pass data back to handler functions like this:

-
Pass data back to the main thread from a worker
from pyscript import sync
-
-sync.hello("PyScript")
+
Pass data back to the main thread from a worker
from pyscript import sync
+
+sync.hello("PyScript")
 

HTML attributes

As a convenience, and to ensure backwards compatibility, PyScript allows the @@ -1110,16 +1121,16 @@

HTML attributes

start with on in standard HTML (e.g. onclick). The rule of thumb is to simply replace on with py- and then reference the name of a Python function.

-
A py-click event on an HTML button element.
<button py-click="handle_click" id="my_button">Click me!</button>
+
A py-click event on an HTML button element.
<button py-click="handle_click" id="my_button">Click me!</button>
 
-
The related Python function.
from pyscript import window
-
-
-def handle_click(event):
-    """
-    Simply log the click event to the browser's console.
-    """
-    window.console.log(event)    
+
The related Python function.
from pyscript import window
+
+
+def handle_click(event):
+    """
+    Simply log the click event to the browser's console.
+    """
+    window.console.log(event)    
 

Under the hood, the pyscript.when decorator is used to enable this behaviour.