Add browser_fetch helpers and async HTTP demos for Pyodide
Ship pyfetch-based fetch utilities in lib/, run asyncio scripts via nest-asyncio in the worker, and add sample demos for HTTPS in the browser. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
32
src/static/bundled-demos/demo/aiohttp_fetch_demo.py
Normal file
32
src/static/bundled-demos/demo/aiohttp_fetch_demo.py
Normal file
@@ -0,0 +1,32 @@
|
||||
"""Async HTTPS GET of Pyodide’s lock file (browser-safe).
|
||||
|
||||
Older Pyodide + ``aiohttp`` examples used ``ClientSession`` here, but the
|
||||
Pyodide ``aiohttp`` wheel often raises ``RuntimeError('SSL is not supported.')``
|
||||
for ``https://`` — there is no real Python TLS stack in the worker; traffic must
|
||||
go through the browser’s ``fetch``.
|
||||
|
||||
This demo uses the shared ``browser_fetch`` helpers (``pyodide.http.pyfetch``
|
||||
under the hood), same idea as ``async_fetch_demo.py``.
|
||||
|
||||
CORS still applies — jsDelivr allows cross-origin GETs. ``print(..., flush=True)``
|
||||
helps batched worker stdout appear before the run finishes.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
from browser_fetch import fetch_json
|
||||
|
||||
|
||||
async def main():
|
||||
url = "https://cdn.jsdelivr.net/pyodide/v0.26.4/full/pyodide-lock.json"
|
||||
print("Fetching", url, "via browser_fetch.fetch_json ...", flush=True)
|
||||
data = await fetch_json(url)
|
||||
info = data.get("info") or {}
|
||||
packages = data.get("packages") or {}
|
||||
print("pyodide-lock version:", info.get("version"), flush=True)
|
||||
print("python runtime :", info.get("python"), flush=True)
|
||||
print("indexed packages :", len(packages), flush=True)
|
||||
print("aiohttp wheel in lock:", "aiohttp" in packages, flush=True)
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
27
src/static/bundled-demos/demo/async_fetch_demo.py
Normal file
27
src/static/bundled-demos/demo/async_fetch_demo.py
Normal file
@@ -0,0 +1,27 @@
|
||||
"""Async HTTP in the browser (Pyodide).
|
||||
|
||||
``aiohttp`` and similar clients use OS sockets, which Wasm does not provide, so
|
||||
a ``session.get(...)`` can hang after ``print("Running")`` with no body.
|
||||
|
||||
Use ``pyodide.http.pyfetch`` or the shared ``browser_fetch`` helpers. From an
|
||||
HTTPS editor page, use ``https://`` URLs (mixed content blocks ``http://``).
|
||||
|
||||
Many sites do not send CORS headers, so the browser blocks the response even
|
||||
when the URL is valid. This demo uses jsDelivr JSON that allows cross-origin
|
||||
GET (same host Pyodide loads from).
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
||||
from browser_fetch import fetch_json
|
||||
|
||||
|
||||
async def main():
|
||||
print("Running")
|
||||
url = "https://cdn.jsdelivr.net/pyodide/v0.26.4/full/repodata.json"
|
||||
data = await fetch_json(url)
|
||||
info = data.get("info") or {}
|
||||
print("Fetched repodata; pyodide lock says:", info.get("version"), info.get("python"))
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user