feat: add frontend inspector for mapping DOM nodes to Python source#6411
feat: add frontend inspector for mapping DOM nodes to Python source#6411FarhanAliRaza wants to merge 4 commits intoreflex-dev:mainfrom
Conversation
- Add `frontend_inspector`, `frontend_inspector_shortcut`, and `frontend_inspector_editor` config options; "dev" mode fails the prod build (validated at Config init and re-checked at compile time). - New `reflex_base.inspector` package: state flag, capture registry that records source location per component, and an integration module owning compile-time wiring. - `Component.create` stamps emitted tags with a `data-rx-source` id when capture is enabled (Fragments skipped). - Vite config and package.json now react to inspector state and are re-rendered each compile; `launch-editor` pulled in as a dev dep on demand. - Extract framework-frame walking from `console.py` into shared `reflex_base.utils.frames`, reused by the inspector. - Exclude `_inspector_id` from generated pyi stubs. - Docs: new `advanced_onboarding/frontend_inspector.md` page, sidebar entry, and pointer from the configuration guide. - Tests for config validation, runtime gating, capture lifecycle, frame walking, browser contract, and the component data-attribute.
…n alt - Build inspector script src and runtime URLs through prepend_frontend_path so the script and its assets load when the app is hosted under a base path (e.g. docs at /docs/). Inspector JS now reads the URLs from the window config instead of hardcoding /__reflex/... - Editor middleware matches /__open-in-editor as a substring so it still fires when the request comes in prefixed. - Dedupe capture entries by (file, line, column, component); identical call sites now share an id, cutting the docs source-map from ~200MB to ~600KB. - Require alt at click time and add a 350ms post-focus guard so re-focusing the browser window doesn't hijack a click into 'open in editor'; hide the overlay on blur to avoid stale snap on return. - Update the inspector docs to reflect alt+click.
Greptile SummaryThis PR adds a dev-only frontend inspector that maps rendered DOM nodes back to their Python One P1 bug was found in the browser script: Confidence Score: 3/5Not safe to merge as-is: click-to-open-editor is broken for any non-default shortcut modifier configuration due to hardcoded altKey checks in the browser script. A P1 logic bug means onClick, onKeyDown (modifier-held preload), and onKeyUp all hardcode event.altKey instead of checking the configured shortcut modifier. This breaks the primary interaction for any user who configures a non-alt modifier. Since the default shortcut is alt+x this passed basic testing but is broken for all other configurations. packages/reflex-base/src/reflex_base/assets/inspector/inspector.js — the onClick, onKeyDown, and onKeyUp handlers need to consult the configured shortcut object instead of hardcoding event.altKey. Important Files Changed
Sequence DiagramsequenceDiagram
participant User as Developer
participant Config as rx.Config
participant Integration as inspector.integration
participant Capture as inspector.capture
participant Compile as App._compile
participant Emit as inspector.emit
participant Browser as inspector.js
User->>Config: frontend_inspector="dev"
Config->>Integration: validate(config)
Integration-->>Config: ok (or ConfigError if prod)
User->>Compile: reflex run
Compile->>Integration: prepare_for_compile(config)
Integration->>Capture: reset()
Integration-->>Compile: state enabled
loop For each Component.create()
Compile->>Capture: capture(cls.__name__)
Capture-->>Compile: cid (int)
Compile->>Compile: stamp data-rx=cid on tag
end
Compile->>Emit: write_source_map(public_dir)
Emit-->>Compile: /__reflex/source-map.json written
Browser->>Browser: Alt+X toggles inspector
Browser->>Browser: fetch(SOURCE_MAP_URL)
Browser-->>Browser: sourceMap loaded
Browser->>Browser: hover shows outline+label
Browser->>Browser: Alt+click GET /__open-in-editor?file=…
Browser->>Browser: dev_server_middleware launches editor
Reviews (1): Last reviewed commit: "fix(inspector): respect frontend_path, d..." | Re-trigger Greptile |
…prod frontend_inspector="dev" is already inert under REFLEX_ENV_MODE=prod (is_active gates every emission site), so the validate() raise just broke shared rxconfigs that get reused across run modes — including the docs CI which runs reflex run --env prod against a config that keeps the inspector on for local dev.
Cache the source map per modifier-press/click so edits are picked up without reloading the page, and gate clicks on the configured shortcut (alt/ctrl/meta/shift) instead of hard-coding altKey.
| frontend_inspector: FrontendInspectorMode = "off" | ||
|
|
||
| frontend_inspector_shortcut: str = "alt+x" | ||
|
|
||
| frontend_inspector_editor: str = "" | ||
|
|
There was a problem hiding this comment.
i would rather this be implemented as a plugin, and the config options belong in that plugin, similar to sitemap
frontend_inspector,frontend_inspector_shortcut, andfrontend_inspector_editorconfig options; "dev" mode fails the prod build (validated at Config init and re-checked at compile time).reflex_base.inspectorpackage: state flag, capture registry that records source location per component, and an integration module owning compile-time wiring.Component.createstamps emitted tags with adata-rx-sourceid when capture is enabled (Fragments skipped).launch-editorpulled in as a dev dep on demand.console.pyinto sharedreflex_base.utils.frames, reused by the inspector._inspector_idfrom generated pyi stubs.advanced_onboarding/frontend_inspector.mdpage, sidebar entry, and pointer from the configuration guide.All Submissions:
Type of change
Please delete options that are not relevant.
New Feature Submission:
Changes To Core Features:
Alt-click actually opens the file in the editor.