-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Component as Var type #3732
Component as Var type #3732
Conversation
* override dict in propsbase to use camelCase * fix underscore in dict * dang it darglint
* half of the way there * add dataclass support * Forbid Computed var shadowing (#3843) * get it right pyright * fix unit tests * rip out more pydantic * fix weird issues with merge_imports * add missing docstring * make special props a list instead of a set * fix moment pyi * actually ignore the runtime error * it's ruff out there --------- Co-authored-by: benedikt-bartscher <31854409+benedikt-bartscher@users.noreply.github.com>
import reflex as rx
class State(rx.State):
component_string: str = "rx.button('hi')"
message: rx.Component = rx.el.div("Hello")
@rx.var
def component(self) -> rx.Component:
try:
comp = eval(
self.component_string,
{
"rx": rx,
"State": State,
},
)
if not isinstance(comp, rx.Component):
return rx.el.div("Invalid component")
return comp
except Exception as e:
return rx.el.div(str(e))
def im_feeling_lucky(self):
import random
components = [
rx.el.div("Hello"),
rx.button("Click me", on_click=State.im_feeling_lucky),
rx.text_area("Type here"),
rx.badge("New"),
]
self.message = random.choice(components)
def index():
return rx.hstack(
rx.vstack(
rx.heading(
"Input",
size="3",
),
rx.text_area(
value=State.component_string,
on_change=State.set_component_string,
width="100%",
flex="1",
font_family="monospace",
),
height="100%",
flex="1",
),
rx.vstack(
rx.heading(
"Output",
size="3",
),
rx.card(State.component, width="100%", flex="1"),
rx.hstack(
rx.button("I'm feeling lucky", on_click=State.im_feeling_lucky),
State.message,
),
height="100%",
flex="1",
),
height="100vh",
width="100vw",
box_sizing="border-box",
padding="1rem",
gap="1rem",
)
app = rx.App()
app.add_page(index, "/") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got a small question:
How does this impact the initial size for the frontend ?
import * as React from "react"; | ||
import * as utils_context from "/utils/context.js"; | ||
import * as utils_state from "/utils/state.js"; | ||
import * as radix from "@radix-ui/themes"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this import all the radix components all the time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. In this PR we are not using radix that is stored in the window
. I assume loading it there takes some additional time (sadly, not sure if there's any way of doing this dynamically).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Functionality is working well for me, this is going to be huge.
- In a follow up we need to add more unit tests for all the new functions introduced here
- [discussed offline] There's a limitation on returning vars from other vars (leading to a "bug in React" error) - we should try to catch that happening and prevent it
Once this is rebased it should be good to merge!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first step of the new reflex!
@adhami3310 did most of the initial research for this one 👏
comp_
to be considered a dynamic component. In the future a special type of computed var will be used as the marker.await
the import. Because of this, we cannot have the component function in the initial state (yet).make_component
andcomponent_var
decorator should become part of the framework instead of defined in the sample code.fs
module.Sample code