Flask (2)
Last updated Sep 30, 2026 is supported in Python Workers.
Flask applications rely on a protocol called the Web Server Gateway Interface (WSGI). This means that Flask never directly reads or writes to a socket, instead relying on the WSGI server to communicate.
Python Workers include a WSGI server ↗︎ which you can use with Flask applications.
Create a Flask Worker
Use this quick start to run a minimal Flask application.
- Create
src/worker.pywith your flask application:
src/worker.pypython
```python from flask import Flask from workers import wsgi
app = Flask(name)
@app.get("/") def index(): return {"message": "Hello from Flask"}
Default = wsgi.entrypoint(app) ```
- In the project root, create
wrangler.jsonc:
jsonc
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "my-flask-worker",
"main": "src/worker.py",
// Set this to today's date
"compatibility_date": "2026-10-27",
"compatibility_flags": ["python_workers"]
}
toml
"$schema" = "node_modules/wrangler/config-schema.json"
name = "my-flask-worker"
main = "src/worker.py"
# Set this to today's date
compatibility_date = "2026-10-27"
compatibility_flags = [ "python_workers" ]
- Create a
pyproject.tomlto declare dependencies:
pyproject.tomltoml
```toml [project] name = "flask-worker" version = "0.1.0" requires-python = ">=3.12" dependencies = [ "flask", ]
[dependency-groups] dev = [ "workers-py", "workers-runtime-sdk", ] ```
- Start the local development server:
sh
uv run pywrangler dev
- In another terminal, send a request to the Worker:
sh
curl http://localhost:8787/
The Worker returns:
json
{"message":"Hello from Flask"}
Serve a frontend
You can serve any static frontend alongside your flask backend by using Workers Static Assets. Using Static Assets means your frontend files are not bundled inside the Worker itself, keeping the bundle small.
Place your static files in a directory such as ./public/. Then configure your Wrangler file with an assets block that includes a binding and sets run_worker_first to true. This ensures every request reaches your Flask Worker first, so your API routes take priority over static files.
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "my-flask-worker",
"main": "src/worker.py",
// Set this to today's date
"compatibility_date": "2026-10-27",
"compatibility_flags": ["python_workers"],
"assets": {
"directory": "./public/",
"binding": "ASSETS",
"run_worker_first": true
}
}
"$schema" = "node_modules/wrangler/config-schema.json"
name = "my-flask-worker"
main = "src/worker.py"
# Set this to today's date
compatibility_date = "2026-10-27"
compatibility_flags = [ "python_workers" ]
[assets]
directory = "./public/"
binding = "ASSETS"
run_worker_first = true
The following Worker handles an API route before forwarding other requests. The catch-all handlers return each asset's body, status, and headers:
src/worker.pypython
from flask import Flask, Response, request
from pyodide.ffi import run_sync
from workers import wsgi
app = Flask(__name__)
@app.get("/api/hello")
def api_hello():
return {"message": "Hello from the API"}
@app.get("/")
@app.get("/<path:path>")
def frontend(path=""):
assets = request.environ["workers.env"].ASSETS
asset_response = run_sync(assets.fetch(f"https://assets.local/{path}"))
body = run_sync(asset_response.bytes())
return Response(
body,
status=asset_response.status,
headers=asset_response.headers,
)
Default = wsgi.entrypoint(app)
run_sync bridges both asynchronous asset operations into Flask's synchronous handler. API routes take priority, and unmatched paths are served from ./public/.
More examples
Clone the cloudflare/python-workers-examples repository and run the flask-todo example there:
git clone https://github.com/cloudflare/python-workers-examples
cd python-workers-examples/flask-todo
# See README.md for instructions
Was this helpful?
YesNo
On this page
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/workers/languages/python/packages/flask/#page","headline":"Flask","description":"Run Flask applications in Python Workers.","url":"https://developers.cloudflare.com/workers/languages/python/packages/flask/","inLanguage":"en","image":"https://developers.cloudflare.com/workers/languages/python/packages/flask/og.png?v=99b9d57b4e7dd3b9","dateModified":"2026-09-30","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}
description: Use LangChain Python packages to build AI applications on Cloudflare Workers. title: Langchain image: https://developers.cloudflare.com/workers/languages/python/packages/langchain/og.png?v=206f0e7ef0bd9fec
Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/workers/llms.txt
Use this file to discover all available pages before exploring further.
Docs