feat(flask): add Flask adapter (closes #5)#19
Merged
Conversation
Adds a Flask adapter so users can `pip install "component-framework[flask]"`
and serve server-side components from Flask, mirroring the FastAPI/Litestar/
Django adapters and following the constitution's Adapter Contract.
- adapters/flask.py: FlaskRenderer (Jinja2; shares the app's jinja_env for
consistent filters/globals/extensions) + a synchronous component endpoint
exposed via a blueprint (create_component_blueprint /
register_component_routes). Parses JSON and HTMX form-encoded bodies; JSON
404/400/500 errors. Import-guarded with _require_extra.
- pyproject: `flask` extra (flask>=3.0 only — no FastAPI/Django/JinjaX);
added to `all` and `dev`.
- examples/flask_example.py: runnable counter app.
- tests/test_flask_adapter.py: renderer + endpoint coverage (dispatch, mount,
form-encoded, 404/400/500), guarded by pytest.importorskip("flask").
- tests/test_optional_extras.py: ImportError guard test for the flask adapter.
- README: Flask marked supported (adapter table, install matrix, Quick Start).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017hdmTV88wAXU8ew1rorMjt
Review caught a real integration bug: component-client.js posts to "/components/<name>/" (trailing slash), but the blueprint rule only matched the no-slash form, so real usage 404'd while the no-slash tests passed. Add strict_slashes=False and a regression test for the trailing-slash URL. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017hdmTV88wAXU8ew1rorMjt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #5. Adds a Flask adapter so users can
pip install "component-framework[flask]"and serve server-side components from Flask, mirroring the FastAPI / Litestar / Django adapters and following the constitution's Adapter Contract.Changes
adapters/flask.pyFlaskRenderer(Renderer)— Jinja2-backed; accepts a Flask app (or a Jinja2Environment) and shares the app'sjinja_envso component templates inherit the app's filters, globals, and extensions.component_view) usingComponent.dispatch, exposed viacreate_component_blueprint()/register_component_routes()→POST /components/<name>._require_extra.pyproject.toml—flaskextra (flask>=3.0only); added toallanddev.examples/flask_example.py— runnable counter app.tests/test_flask_adapter.py(renderer + endpoint: dispatch, mount, form-encoded, 404/400/500) guarded bypytest.importorskip("flask"); plus a flask ImportError-guard test intests/test_optional_extras.py.Constitution gates
Renderersubclass inadapters/flask.pyimplementingcore/renderer.py.[flask]pulls onlyflask— no FastAPI/Django/JinjaX.examples/.pytest.importorskip("flask").core/using framework-specific types; no cross-adapter imports.WebSocket support is left as a follow-up (the contract lists it as optional).
Testing
tests/test_flask_adapter.py: 8 passed; flask guard test added.prek run --all-filesclean (ruff, ruff-format, ty).🤖 Generated with Claude Code