Skip to content

Add flask hello world and todo app - #89

Open
hoodmane wants to merge 2 commits into
cloudflare:mainfrom
hoodmane:flask-examples
Open

Add flask hello world and todo app#89
hoodmane wants to merge 2 commits into
cloudflare:mainfrom
hoodmane:flask-examples

Conversation

@hoodmane

@hoodmane hoodmane commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Adds two flask examples: A hello world example and a todo-notes app.

Comment thread 20-flask/src/worker.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why twenty 😅 ? The last number was 17.

Comment thread 20-flask/src/worker.py
@@ -0,0 +1,26 @@
import wsgi

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be workers.wsgi. We don't expose this top-level

Comment thread 20-flask/package.json
"start": "uv run pywrangler dev"
},
"devDependencies": {
"wrangler": "^4.46.0"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use newer versions since we bumped the minimal wrangler version in workers-py. I updated all the examples recently #67.

Comment thread 20-flask/pyproject.toml
Comment on lines +10 to +15
# Local workers-runtime-sdk checkout (provides wsgi.py), built from source.
# pywrangler resolves the worker env from [project.dependencies] via
# `uv pip compile`, which ignores [tool.uv.sources] — so the local path must
# live directly in this string. Building a directory source requires the
# `[tool.pywrangler] allow-build` override below (turns off `--no-build`).
"workers-runtime-sdk @ file:///home/hood/Documents/programming/workers-py/packages/runtime-sdk",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be removed

Comment thread 20-flask/pyproject.toml
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
"webtypy>=0.1.7",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need webtypy here.

Comment thread 20-flask/pyproject.toml
Comment on lines +23 to +31
# Use the local workers-py (pywrangler) checkout so the `allow-build` override is
# available. This only affects the host dev tool, not the worker.
[tool.uv.sources]
workers-py = { path = "../../workers-py/packages/cli", editable = true }

[tool.pywrangler]
# Allow building sdists / local directory sources (drops `uv`'s `--no-build`),
# so the local workers-runtime-sdk directory above builds during sync.
allow-build = true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These too

Comment thread 20-flask/wrangler.jsonc
Comment on lines +5 to +9
"compatibility_date": "2025-11-02",
"compatibility_flags": [
"python_workers",
"python_process_pth_files"
],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe let's use newer compat dates

Comment on lines +3 to +16
The D1 binding is a JavaScript object whose methods return promises. Flask is a
WSGI framework, so its view functions are plain synchronous Python and cannot
`await`. We bridge the two with `pyodide.ffi.run_sync`, which suspends the
Python stack until a JS promise settles, using JSPI (JavaScript Promise
Integration) stack switching.

`run_sync` only works while a JSPI suspender is on the stack. `workers.wsgi.fetch`
invokes the WSGI app from inside the Worker's async `fetch` handler, so that
condition holds for every request routed through `src/entry.py`.

The Workers SDK already converts D1's JS return values into Python objects:
`.all()` yields a mapping whose `results` key is a `list` of dict-like rows, and
`.first()` yields a dict-like row or `None`. No explicit `to_py()` is needed;
this module just normalizes them to plain `dict`s.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit verbose.

this module just normalizes them to plain `dict`s.
"""

from pyodide.ffi import run_sync

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use run_until_complete or asyncio.run instead? I think that would be more familiar to the users than using run_sync.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants