Running Python ASGI apps in the browser via Pyodide + a service worker

· Source: Simon Willison's Weblog · Field: Technology & Digital — Software Development & Engineering, Emerging Technologies & Innovation, Data Science & Analytics · Depth: Advanced, quick

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

Method

A service worker intercepts "/app/" requests, routing them to a Python ASGI app running in-browser via Pyodide for execution.

In practice

Topics

Best for: Software Engineer, AI Engineer, Machine Learning Engineer

Related on AIssential

Open in AIssential →

Editorial summary, takeaway, and curation by AIssential. Original article published by Simon Willison's Weblog.