ENG-12604: add examples/ and the playground benchmark app skeleton
PR-4 of the Holistic Framework Benchmarks project (ENG-12604). Lands the in-repo examples/ directory, its conventions, a small skeleton of examples/playground (the app the macro benchmarks will drive), and the guardrails around it: a namespace lint, a content-hash gate and a CI job that compiles the playground with this checkout and with reflex 0.8.23. No published package changes, so no news fragment (reflex-release changelog-check locally: "No packaged source changes; no changelog fragments required."). Independent of reflex-bench (#7274).
All Submissions:
- Have you followed the guidelines stated in CONTRIBUTING.md file?
- Have you checked to ensure there aren't any other open Pull Requests for the desired changed?
Type of change
- New feature (non-breaking change which adds functionality)
New Feature Submission:
- Does your submission pass the tests?
- Have you linted your code locally prior to submission?
Changes To Core Features:
Not applicable: nothing under reflex/ or packages/ changes.
Stack update
This PR now stacks on #7283: main ← #7274 ← #7281 ← #7283 ← this PR. Nothing merges until the whole stack is complete. The rebase was clean (this PR touches no reflex-bench files). With reflex_bench now present, tests/units/test_examples.py checks its ban for real: test_examples.py, test_hash_examples.py and the reflex-bench tests pass (515 passed, 2 skipped), hash_examples.py --check passes, and ruff check examples/ is clean.
What landed
examples/is no longer git-ignored.examples/README.mdexplains the directory (standalone apps, each runs withreflex runin a fresh venv, not uv workspace members) and its rules.examples/playground/:pyproject.toml(reflex-playground,requires-python >=3.10,reflex>=0.8.23,Private :: Do Not Upload),rxconfig.py,assets/(favicon.icofrom thereflex inittemplate,playground.css), and theplaygroundpackage:playground.py(rx.App+add_pageper page),state.py(PlaygroundState,BenchState),layout.py(shared layout with the benchmark hooks),components/marker.py,pages/{index,counter,item}.py. Real content: nav bar, counter with increment/decrement,/item/[item_id],rx.foreachover a short list,rx.cond(even/odd). About 230 lines of app code.- Namespace lint:
examples/ruff.tomlextends the root config and bans (TID251) every import name underpackages/*/src/(reflex_base,reflex_bench,reflex_components_*,reflex_docgen,reflex_cli, ...). Banning a module also bans its submodules.tests/units/test_examples.pyalso fails when a tracked example imports reflex in any form other thanimport reflex as rx(import reflex,from reflex import App,from reflex.state import State). Ruff's import-convention rules miss the deep-module forms, which are the ones that break across releases. - Content hash gate:
scripts/hash_examples.pywritesexamples/playground/.content-hash(sha256:<hex>).--checkexits 1 with the message below. New pre-commit hookhash-examplesregenerates it, and the existing pre-commit workflow fails on the diff. - CI:
.github/workflows/examples.yml, with jobsplayground-headandplayground-0.8.23. - Tests:
tests/units/test_hash_examples.py,tests/units/test_examples.py(the import rules), andtests/integration/tests_playwright/test_playground.py. The last one runs the playground (the files its content hash covers) with AppHarness and checks the benchmark contract below in a browser:#bench-hydratedon every page, theset_seq(7)round trip, the counter and itsrx.cond, the dynamic route reached from therx.foreachlinks, and that each# bench:hmr-targetpragma line exists once and its string shows in its element.
Benchmark contract (for later PRs)
Ids, all rendered on every page through layout.py, except #bench-marker-leaf (index page only):
| Id | Content |
|---|---|
bench-hydrated | rx.cond(BenchState.is_hydrated, rx.el.span(id="bench-hydrated")). Empty, so wait with Playwright state="attached". |
bench-seq | rx.el.span(BenchState.last_seq, id="bench-seq"). BenchState.set_seq(self, seq: int) sets it. Button #bench-set-seq fires set_seq(7). Websocket event name on 0.8.23 and HEAD: reflex___state____state.playground___state____bench_state.set_seq. |
bench-handler-value | BenchState.handler_value. BenchState.bench_value() sets it to HANDLER_MARKER, and button #bench-handler fires it. |
bench-marker-root | ROOT_MARKER in playground/layout.py (imported by every page) |
bench-marker-leaf | LEAF_MARKER in playground/components/marker.py (imported by the index page only) |
Pragma lines (the benchmarks rewrite exactly the string literal on the line; documented in the playground README):
| File | Line | Shows in |
|---|---|---|
playground/components/marker.py | LEAF_MARKER = "m-initial-leaf" # bench:hmr-target leaf | #bench-marker-leaf |
playground/layout.py | ROOT_MARKER = "m-initial-root" # bench:hmr-target root | #bench-marker-root |
playground/state.py | HANDLER_MARKER = "m-initial-handler" # bench:hmr-target handler | #bench-handler-value, once bench_value runs |
--check message: examples/playground changed: benchmark baselines reset by this PR (update with: uv run python scripts/hash_examples.py).
Verification (section 3)
Headless Chromium drove each running app with this Playwright script. It is not committed.
check_playground.py
import sys
from playwright.sync_api import expect, sync_playwright
base = sys.argv[1].rstrip("/")
def hydrated(page, path):
page.goto(base + path)
page.wait_for_selector("#bench-hydrated", state="attached", timeout=60_000)
print(f"{path}: #bench-hydrated attached")
with sync_playwright() as p:
browser = p.chromium.launch(executable_path="/opt/pw-browsers/chromium")
page = browser.new_page()
hydrated(page, "/")
for sel, want in [("#bench-marker-root", "m-initial-root"), ("#bench-marker-leaf", "m-initial-leaf"), ("#bench-seq", "0")]:
expect(page.locator(sel)).to_have_text(want)
print(f"/: {sel} == {want!r}")
items = page.locator("li").all_inner_texts()
print(f"/: rx.foreach items {items}")
assert items == ["alpha", "beta", "gamma"], items
page.click("#bench-set-seq")
expect(page.locator("#bench-seq")).to_have_text("7")
print("/: clicked #bench-set-seq (set_seq(7)) -> #bench-seq == '7'")
page.click("#bench-handler")
expect(page.locator("#bench-handler-value")).to_have_text("m-initial-handler")
print("/: clicked #bench-handler (bench_value) -> #bench-handler-value == 'm-initial-handler'")
page.click("text=Counter")
expect(page.locator("#count")).to_have_text("0")
page.click("#increment"); page.click("#increment"); page.click("#increment")
expect(page.locator("#count")).to_have_text("3")
expect(page.get_by_text("odd", exact=True)).to_be_visible()
page.click("#decrement")
expect(page.locator("#count")).to_have_text("2")
expect(page.get_by_text("even", exact=True)).to_be_visible()
print("/counter (nav link): +++- -> #count == '2', rx.cond shows 'even'")
hydrated(page, "/counter")
hydrated(page, "/item/42")
expect(page.locator("#item-id")).to_have_text("42")
print("/item/42: #item-id == '42'")
page.click("text=Home")
page.click("text=alpha")
expect(page.locator("#item-id")).to_have_text("alpha")
print("/item/42 -> Home -> item link 'alpha': #item-id == 'alpha'")
browser.close()
print("ALL CHECKS PASSED")
The script produced the same output on all five runs: HEAD dev, HEAD --env prod, 0.8.23 dev, 0.8.23 --env prod, and a standalone copy with reflex 0.9.12 from PyPI. None of the five server logs had a warning, error or deprecation.
/: #bench-hydrated attached
/: #bench-marker-root == 'm-initial-root'
/: #bench-marker-leaf == 'm-initial-leaf'
/: #bench-seq == '0'
/: rx.foreach items ['alpha', 'beta', 'gamma']
/: clicked #bench-set-seq (set_seq(7)) -> #bench-seq == '7'
/: clicked #bench-handler (bench_value) -> #bench-handler-value == 'm-initial-handler'
/counter (nav link): +++- -> #count == '2', rx.cond shows 'even'
/counter: #bench-hydrated attached
/item/42: #bench-hydrated attached
/item/42: #item-id == '42'
/item/42 -> Home -> item link 'alpha': #item-id == 'alpha'
ALL CHECKS PASSED
HEAD (reflex compile / reflex run, from examples/playground through the workspace venv):
$ uv run --no-sync --directory examples/playground --project ../.. reflex --version
0.9.12.post6.dev0+84bb6abb3
$ uv run --no-sync --directory examples/playground --project ../.. python -c "import reflex, sys; print(sys.prefix); print(reflex.__file__)"
/home/user/reflex/.venv
/home/user/reflex/reflex/__init__.py
$ cd examples/playground && uv run --no-sync --project ../.. reflex compile --no-rich # progress lines omitted
Info: Initializing the web directory.
Success: Initialized playground. Run `uv run reflex run` to start the app.
Success: App compiled successfully in 0.891 seconds.
$ uv run --no-sync --directory examples/playground --project ../.. reflex run --frontend-port 3100 --backend-port 8100
App running at: http://localhost:3100/
Backend running at: http://0.0.0.0:8100
$ uv run --no-sync --directory examples/playground --project ../.. reflex run --env prod --frontend-port 3100 --backend-port 3100
App running at: http://0.0.0.0:3100/
reflex 0.8.23 (fresh Python 3.12 venv):
$ uv venv --python 3.12 $RUNNER_TEMP/reflex-0.8.23
Using CPython 3.12.3 interpreter at: /usr/bin/python3.12
$ uv pip install --python $RUNNER_TEMP/reflex-0.8.23/bin/python "reflex==0.8.23"
Resolved 37 packages in 364ms
Installed 37 packages in 50ms
+ reflex==0.8.23
+ reflex-hosting-cli==0.1.72
$ export PATH="$RUNNER_TEMP/reflex-0.8.23/bin:$PATH"; cd examples/playground
$ reflex --version
0.8.23
$ reflex compile
[17:37:33] Initializing the web directory. console.py:231
Success: Initialized playground. Make sure to add reflex==0.8.23 to your
requirements.txt or pyproject.toml file.
Success: App compiled successfully in 1.426 seconds.
$ reflex run --frontend-port 3100 --backend-port 8100
App running at: http://localhost:3100/
Backend running at: http://0.0.0.0:8100
$ reflex run --env prod --frontend-port 3100 --backend-port 8100
App running at: http://0.0.0.0:3100/
Backend running at: http://0.0.0.0:8100
(Cold compiles took 11.1 s on HEAD and 11.3 s on 0.8.23. The 0.9 s and 1.4 s runs above replay the two CI jobs' steps with a warm bun cache.)
The tree stays clean after running the app. On both versions, a fresh checkout's reflex run/compile performs reflex's implicit init, which rewrites the app's .gitignore, and HEAD also writes reflex.lock/. After a compile on each version: cmp shows examples/playground/.gitignore byte-identical, hash_examples.py --check passes, and git status --untracked-files=all shows no new files.
uv workspace (section 2.1): uv does not complain about the nested non-member project. It treats the playground as a standalone project, so no [tool.uv.workspace] exclude is needed:
$ cd examples/playground && uv run -v --no-sync python -c "import sys; print(sys.prefix)" # debug lines excerpted
DEBUG Found workspace root `/home/user/reflex`, but project is not included
DEBUG Found workspace root: `/home/user/reflex/examples/playground`
DEBUG Adding root workspace member: `/home/user/reflex/examples/playground`
DEBUG Discovered project `reflex-playground` at: /home/user/reflex/examples/playground
DEBUG Using Python 3.14.7 interpreter at: /home/user/reflex/examples/playground/.venv/bin/python
/home/user/reflex/examples/playground/.venv
$ uv lock --check # repo root
Resolved 214 packages in 12ms
For the same reason, uv run --directory examples/playground reflex compile would not use the workspace's reflex: it would create examples/playground/.venv with reflex from PyPI. The HEAD job therefore runs uv run --no-sync --project ../.. reflex ... from examples/playground (versions above). A copy of the folder outside the repo also works standalone: uv run reflex --version → 0.9.12 from PyPI, and uv run reflex compile → App compiled successfully in 4.544 seconds.
Namespace lint catches a deliberate violation (added to state.py, then removed):
$ uv run ruff check examples/
examples/playground/playground/state.py:4:8: banned-api: `reflex_components_core` is banned: Examples import Reflex only as `import reflex as rx`.
examples/playground/playground/state.py:4:8: unused-import: [*] `reflex_components_core.core` imported but unused
examples/playground/playground/state.py:5:1: banned-api: `reflex_base` is banned: Examples import Reflex only as `import reflex as rx`.
examples/playground/playground/state.py:5:25: unused-import: [*] `reflex_base.constants` imported but unused
examples/playground/playground/state.py:6:1: banned-api: `reflex_base` is banned: Examples import Reflex only as `import reflex as rx`.
examples/playground/playground/state.py:6:39: unused-import: [*] `reflex_base.utils.console.warn` imported but unused
Found 6 errors.
$ # (reverted)
$ uv run ruff check examples/
All checks passed!
(The added lines were import reflex_components_core.core, from reflex_base import constants and from reflex_base.utils.console import warn.)
Hash gate catches a deliberate unhashed change (LEAF_MARKER edited, .content-hash untouched):
$ uv run pre-commit run --all-files --show-diff-on-failure
ruff-format..............................................................Passed
ruff-check...............................................................Passed
codespell................................................................Passed
update-pyi-files.........................................................Passed
hash-examples............................................................Failed
- hook id: hash-examples
- files were modified by this hook
pre-commit hook(s) made changes.
If you are seeing this message in CI, reproduce locally with: `pre-commit run --all-files`.
To run `pre-commit` as part of git workflow, use `pre-commit install`.
All changes made by hooks:
diff --git a/examples/playground/.content-hash b/examples/playground/.content-hash
index 8a4be593d..01dadec83 100644
--- a/examples/playground/.content-hash
+++ b/examples/playground/.content-hash
@@ -1 +1 @@
-sha256:5c66b0dc30717af353d0f8b0765ac1a73207debf1fbb3a59e6d2c0b39e9226f9
+sha256:2b50892c14aef694156c3a416c621fd9f9dfd7e68013afb4aee9aff8ea811977
diff --git a/examples/playground/playground/components/marker.py b/examples/playground/playground/components/marker.py
index e65c49169..34b8e3d5f 100644
--- a/examples/playground/playground/components/marker.py
+++ b/examples/playground/playground/components/marker.py
@@ -2,7 +2,7 @@
import reflex as rx
-LEAF_MARKER = "m-initial-leaf" # bench:hmr-target leaf
+LEAF_MARKER = "m-changed-leaf" # bench:hmr-target leaf
def marker() -> rx.Component:
$ # restore the committed .content-hash, keep the edit
$ uv run python scripts/hash_examples.py --check
examples/playground changed: benchmark baselines reset by this PR (update with: uv run python scripts/hash_examples.py)
$ echo $?
1
Repo checks: uv run ruff check . and uv run ruff check examples/: all checks passed. uv run ruff format --check .: 1568 files already formatted. uv run pyright reflex tests and uv run pyright examples scripts/hash_examples.py: 0 errors. uv run pytest tests/units --cov --no-cov-on-fail --cov-report= on the final commit: 9783 passed, 20 skipped, total coverage 79.04%. One earlier run overlapped a pyright run, and there the timing-based test_state.py::test_state_manager_lock_expire_contend failed once; it passes 5/5 in isolation, and this PR doesn't touch that code. uv run pre-commit run --all-files on the final commit: every hook passed. reflex-release changelog-check, check-headings and sync --check all pass. The new workflow validates against the GitHub workflow schema (check-jsonschema --builtin-schema vendor.github-workflows). Both CI jobs' run: steps were replayed locally (outputs above).
Deviations from the prompt
rxconfig.pyaddsRadixThemesPluginbehindhasattr(rx.plugins, "RadixThemesPlugin"). Without it, HEAD printsDeprecationWarning: Implicit Radix Themes enablement has been deprecated in version 0.9.0 ...on every compile and run. The plugin doesn't exist in 0.8.23 (which always loads Radix Themes), and the guard keeps the file loadable there (verified above). Otherwise the config isSitemapPlugin()only, listed explicitly to silence the "enabled by default" warning on both versions. Tailwind is left out because nothing asks for it.examples/ruff.tomlhas two settings beyond the ban list.src = ["*"]: otherwise ruff findsreflex/at the repo root and treatsreflexas first-party, soI001fires on the conventionalimport reflex as rx/ blank line /from playground... importgrouping. A per-fileINP001ignore for*/rxconfig.py: an app root is not a package.examples/playground/.gitignorealso ignoresreflex.lock/, although reflex's docs recommend committing it. HEAD writesreflex.lock/{bun.lock,package.json}on the first compile. Committing it would pin HEAD's frontend dependencies into the fixture, so reflex's own JS dependency bumps would reset the baselines instead of showing up in the series. The file also ignoresuv.lockfor the same reason (the app runs against several reflex versions), plus.venv/anduploaded_files/, and it starts with exactly the entriesreflex initwrites, so the implicit init is a no-op.- Hash details beyond the prompt. It normalizes CRLF to LF, because a Windows checkout with
core.autocrlfwould otherwise write a hash CI cannot reproduce. It excludes.content-hashitself, skips tracked files deleted from the tree, and lists a conflicted path once, not once per merge stage. - The pre-commit hook uses
files: ^(examples/playground/|scripts/hash_examples\.py$)+pass_filenames: false, notalways_run. It runs only when relevant. Pre-commit never passes deleted files, so a deletion-only commit skips the hook locally, but the CI--all-filesrun catches it. examples.ymldetails. The HEAD job also runshash_examples.py --checkfirst, for a fast failure with the explicit message. The 0.8.23 job's id isplayground-0_8_23(job ids cannot contain dots) and its display name isplayground-0.8.23. That job puts the venv'sbin/onPATHlike an activated venv (see finding 1 below).- Extra test module
tests/units/test_examples.py. It runs ruff on a snippet underexamples/that imports everypackages/*/src/*module, so a package added without a ban entry fails, and it checks the ban does not apply outsideexamples/. It also checks that every tracked example imports reflex only asimport reflex as rx.reflex_bench(from #7274) is already in the ban list, so the list is correct whichever PR merges first. - Branch. The session environment pins the branch, so this PR comes from
farhan/beautiful-carson-ldmn0u, notfarhan/eng-12604-examples-playground-skeleton. The title carries ENG-12604.
Findings for the harness PRs
- reflex 0.8.23
reflex run --env prodspawnsgranianby name. Calling the venv'sreflexbinary with the venv not onPATHfails withFileNotFoundError: [Errno 2] No such file or directory: 'granian', and the page never hydrates.<subject python> -m reflex ...has the same problem, so the harness must put the subject venv'sbin/onPATH. Dev mode is unaffected. - HEAD prod rejects different ports (
In prod mode, frontend and backend must run on the same port.) and printsApp running at:before it binds, as the version table says. Poll/pingbefore driving it. - HEAD writes
reflex.lock/into the app root on first compile. Benchmark workdirs will see it too.
Adversarial self-review
- Untracked files count toward the hash (as the prompt specifies). A stray untracked file in the playground (editor swap file, notes) changes a locally computed hash, and the hook would write a value CI cannot reproduce. Mitigation: the playground
.gitignorecovers everything reflex and uv create there, and the README says to commit new files and delete stray ones. Kept as specified. - Deletion-only commits skip the hook locally (deviation 5). CI catches them.
- The ban list is static. Fixed:
tests/units/test_examples.pyfails when a package underpackages/is not banned. I checked that removing thereflex_otelentry makes it fail. rx.State.item_idneeds# pyright: ignore[reportAttributeAccessIssue], because dynamic route vars are attached atadd_pagetime. This is the same pattern astests/integration/test_dynamic_routes.py, and the route var is the documented API on both versions (router.pageis deprecated since 0.8.1).- Pre-commit's pyright checks the playground against HEAD only. 0.8.23 compatibility is enforced by the
playground-0.8.23CI job, not by the type checker. examples/was ignored before, probably because people used it as a local scratch area (thepr-example-reviewskill builds apps there). Such scratch apps now show up as untracked. Move them, or list them in.git/info/exclude.- Changing the hashing algorithm changes the hash and resets baselines. This is inherent, and the hook reruns when the script changes, so the committed value stays consistent.
#bench-set-seqfires a fixedset_seq(7). It exists for manual and Playwright checks; the event benchmarks sendset_seq(n)over the websocket, using the event name documented above. The hooks sit in a visible, labelled "Benchmark hooks" footer, so someone copying the app can see what they are.- CLAUDE.md: every function has a Google-style docstring, imports are at module top in isort order, test functions are at module level, and there are no block comments. The hook, the READMEs and the HEAD job run Python through
uv. The 0.8.23 job builds its venv withuv venv/uv pipand then calls that venv'sreflex, because the venv is not a uv project.