Running Python ASGI apps in the browser via Pyodide + a service worker
Summary
A project demonstrates running Python ASGI web applications entirely within the browser using Pyodide and a service worker, eliminating the need for a backend server for application logic. This innovative approach intercepts all same-origin requests under the "/app/" path via a dedicated service worker, executing them directly against the Python application using the ASGI protocol. The mechanism's versatility is confirmed through successful implementations with both a FastAPI demonstration and the complete Datasette application. This advancement builds upon previous work, such as Datasette Lite, which initially utilized Web Workers for navigation interception, by shifting to a more robust service worker-based request handling. This allows complex Python web applications to function client-side, requiring a server only for static file delivery.
Key takeaway
For AI Engineers developing interactive Python web tools, you should explore integrating Pyodide and service workers to deploy ASGI applications directly in the browser. This approach significantly reduces backend infrastructure needs, allowing your applications, like FastAPI or Datasette, to run client-side with only static file hosting. Consider this method for prototypes or applications where serverless deployment and reduced operational overhead are critical, streamlining your development and deployment workflows.
Key insights
Python ASGI web apps can run serverless in browsers via Pyodide and service workers.
Principles
- Service workers can intercept same-origin requests.
- Pyodide enables Python execution in browsers.
- ASGI provides a standard interface for web apps.
Method
A service worker intercepts "/app/" requests, routing them to a Python ASGI app running in-browser via Pyodide for execution.
In practice
- Deploy FastAPI apps without a backend server.
- Run Datasette entirely client-side.
- Develop interactive Python tools in browser.
Topics
- Pyodide
- Service Workers
- ASGI
- WebAssembly
- FastAPI
- Datasette
Best for: Software Engineer, AI Engineer, Machine Learning Engineer
Related on AIssential
Editorial summary, takeaway, and curation by AIssential. Original article published by Simon Willison's Weblog.