Repository navigation
Add reflex, the API browser code reaches Reflex through
#7949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FarhanAliRaza
wants to merge
17
commits into
main
Choose a base branch
from
farhan/focused-darwin-dx76ek-env
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+483
−11
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
83fd080
Add env, the API scripts reach Reflex through
FarhanAliRaza 5dd9697
Format script_env.js with biome
FarhanAliRaza 20fc39d
Add docstrings to the script env tests
FarhanAliRaza 2c92b05
Rename the script API to reflex and keep env.json in scripts
FarhanAliRaza e95f417
Put ref, add_events and library directly on rx.js
FarhanAliRaza 4ae5dbc
Test reflex and rx.js end to end in the call_script app
FarhanAliRaza 2557adf
Document reflex and rx.js usage-first
FarhanAliRaza b35a13e
Reject library names the registry only inherits, and correct the refl…
FarhanAliRaza 8901c28
Drop rx.js.add_events, which only repeats an event trigger
FarhanAliRaza f2e95f3
Drop reflex.library and rx.js.library, which nothing needs
FarhanAliRaza fa329da
Merge remote-tracking branch 'upstream/main' into farhan/focused-darw…
FarhanAliRaza eb98b8b
Merge remote-tracking branch 'upstream/main' into farhan/focused-darw…
FarhanAliRaza c4c7a5e
docs: show how to get an event handler's full name for reflex.event
FarhanAliRaza e771062
docs: note reflex.ref is undefined for a component with a Var id
FarhanAliRaza 46b4462
Restore eventLoop.addEvents after reflex tests; list js last in _SUBM…
FarhanAliRaza c356643
docs: fix the wording of the handler full name warning
FarhanAliRaza 3482137
docs: import the local component example by its importable path
FarhanAliRaza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add `rx.js.ref(id)`, a var that reads a component's ref through the browser's `reflex` API from Python, e.g. `rx.run_script(rx.js.ref("search").current.value, callback=State.set_query)`. See [Reaching Reflex from JavaScript](https://reflex.dev/docs/api-reference/browser-javascript/#reaching-reflex-from-javascript). |
2 changes: 1 addition & 1 deletion
2
packages/reflex-base/news/+eval-minified-shared-chunk.performance.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| Shrink the shared JavaScript chunk by about 88 KB (11 KB gzipped) when `frontend_lazy_bundled_libraries` is on: server-provided scripts and callbacks no longer use a direct `eval`, so the minifier can shorten every name in that chunk. Scripts passed to `rx.call_script` and `rx.call_function` still see every export of `utils/state.js` (such as `ReflexEvent`, `refs` and `queueEvents`), plus `addEvents`, `socket`, `navigate`, `params`, `token` and the globals; names the module does not export, such as its imports, are no longer in scope. | ||
| Shrink the shared JavaScript chunk by about 88 KB (11 KB gzipped) when `frontend_lazy_bundled_libraries` is on: server-provided scripts and callbacks no longer use a direct `eval`, so the minifier can shorten every name in that chunk. Scripts passed to `rx.call_script` and `rx.call_function` still see every export of `utils/state.js` (such as `ReflexEvent`, `refs` and `queueEvents`), plus `addEvents`, `socket`, `navigate`, `params`, `token`, `env` (which `rx.get_upload_url` reads), `reflex` and the globals; names the module does not export, such as its other imports, are no longer in scope. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add `reflex`, the API browser JavaScript reaches Reflex through: `rx.call_script` code, and JavaScript modules through `import { reflex } from "$/utils/reflex"`, can call `reflex.ref(id)`, `reflex.addEvents(events)` and `reflex.event(name, payload)` instead of frontend internals, e.g. `rx.call_script("reflex.ref('search').current.value", callback=State.set_query)`. See [Reaching Reflex from JavaScript](https://reflex.dev/docs/api-reference/browser-javascript/#reaching-reflex-from-javascript). |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| """Build calls on ``reflex``, the API browser code reaches Reflex through. | ||
|
|
||
| The frontend defines ``reflex`` in ``utils/reflex.js``. Each function here | ||
| returns a Var that renders as a call on it, such as ``reflex.ref("search")``. | ||
| A page that uses one imports ``reflex``, and code ``rx.run_script`` sends from | ||
| the backend has ``reflex`` in scope, so the same Var works in both. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Mapping | ||
| from typing import Any | ||
|
|
||
| from reflex_base.constants.base import Dirs | ||
| from reflex_base.utils.imports import ImportVar | ||
| from reflex_base.vars.base import Var, VarData | ||
| from reflex_base.vars.function import FunctionVar | ||
| from reflex_base.vars.object import ObjectVar | ||
|
|
||
| __all__ = ["ref"] | ||
|
|
||
| _REFLEX_VAR_DATA = VarData( | ||
| imports={f"$/{Dirs.REFLEX_API_PATH}": [ImportVar(tag="reflex")]} | ||
| ) | ||
|
|
||
|
|
||
| def _call(member: str, *args: Any) -> Var: | ||
| """Call a member of ``reflex``. | ||
|
|
||
| Args: | ||
| member: The member's name. | ||
| *args: The arguments to call it with. | ||
|
|
||
| Returns: | ||
| The call. | ||
| """ | ||
| return ( | ||
| Var(_js_expr=f"reflex.{member}", _var_data=_REFLEX_VAR_DATA) | ||
| .to(FunctionVar) | ||
| .call(*args) | ||
| ) | ||
|
|
||
|
|
||
| def ref(id: str | Var[str]) -> ObjectVar[Mapping[str, Mapping[str, Any]]]: | ||
| """Get the ref of a component by its id. | ||
|
|
||
| Args: | ||
| id: The component's id. | ||
|
|
||
| Returns: | ||
| The ref, whose ``current`` is the mounted element, or undefined if no | ||
| component with that id has mounted or the component's id is a Var, | ||
| which registers no ref. | ||
| """ | ||
| return _call("ref", id).to(ObjectVar, Mapping[str, Mapping[str, Any]]) |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // `reflex`: the API JavaScript run by an app reaches Reflex through, in place | ||
| // of the internals of utils/state.js. `rx.call_script` code sees it as | ||
| // `reflex`, a module imports it from here, and `rx.js` builds calls on it from | ||
| // Python. Its members read the runtime at call time, so it is safe to import | ||
| // before the event loop mounts and from a module utils/state.js imports. | ||
| import { eventLoop } from "$/utils/context-registry"; | ||
| import { ReflexEvent, refs } from "$/utils/state"; | ||
|
|
||
| /** | ||
| * Get the name a component id is registered under in `refs`. | ||
| * | ||
| * Mirrors `format_ref` in reflex_base.utils.format: every run of characters | ||
| * that are not letters, digits or underscores becomes one underscore. | ||
| * @param {string} id The component id. | ||
| * @returns {string} The registry key. | ||
| */ | ||
| const refName = (id) => `ref_${String(id).replace(/[^\p{L}\p{N}_]+/gu, "_")}`; | ||
|
|
||
| export const reflex = Object.freeze({ | ||
| /** | ||
| * Get the ref of a component by its id. | ||
| * @param {string} id The component id. | ||
| * @returns {{current: any} | undefined} The ref, or undefined if no | ||
| * component with that id has rendered or the component's id is a Var, | ||
| * which registers no ref. Its `current` is null until React attaches the | ||
| * element. | ||
| */ | ||
| ref: (id) => refs[refName(id)], | ||
|
|
||
| /** | ||
| * Dispatch events through the mounted event loop, as event triggers do. | ||
| * @param {any[]} events The events, built with `reflex.event`. | ||
| */ | ||
| addEvents: (events) => { | ||
| eventLoop.addEvents(events); | ||
| }, | ||
|
|
||
| /** | ||
| * Build an event for `addEvents`. | ||
| * @param {string} name The full name of the event handler, built in Python | ||
| * as `f"{State.get_full_name()}.handler_name"` and passed in. | ||
| * @param {object} [payload] The handler's arguments, by name. | ||
| * @returns {any} The event. | ||
| */ | ||
| event: (name, payload = {}) => ReflexEvent(name, payload), | ||
| }); | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.