Replies: 2 comments
-
I think the main problem that you're encountering is that A workaround is to set the default value for the field inside @classmethod
def get_component(cls, **props):
param_names = cls.__fields__["param_names"].default = props.pop("param_names")
cls.__fields__["parameter_items"].default = props.pop("parameter_items")
# Not clear if `cls.colors` is intended to be a state var or a ClassVar
colors = cls.__fields__["colors"].default
cls.__fields__["parameter_color"].default = {
parameter: colors[i % colors.length()]
for i, parameter in enumerate(param_names)
} As you can see, this isn't so nice. We're still trying to figure out a good API for how to set the initial values for the ComponentState class that gets created, based on values the user supplies to |
Beta Was this translation helpful? Give feedback.
-
@masenf Your solution worked perfectly! Thank you so much for your help. However, I was actually trying to accomplish a more complex task and simplified my question for clarity (since I believed solving the simplified version would help me with the actual problem I'm facing). What I'm really trying to do is to manage and modify the following class ParameterData(rx.Base):
parameter_names: list[str]
parameter_items: list[dict[str, str]]
class PageState(rx.State):
parameter_data: ParameterData | None
def load(self):
self.parameter_data = ParameterData(
parameter_names=["param_a", "param_b"],
parameter_items=[{
"param_a": "a-1",
"param_b": "b-1"
}, {
"param_a": "a-2",
"param_b": "b-2"
}]
) Based on your response, I revised my code as follows: class ParameterComponent(rx.ComponentState):
colors: list[str] = ["indigo", "cyan", "orange", "crimson"]
parameter_color: dict[str, str] = {}
parameter_data: ParameterData | None
def add_parameter(self, form_data, parameter_data: ParameterData):
# # AttributeError: The State var `state__page_state.parameter_data?.parameter_names` has no attribute 'append' or may have been annotated wrongly.
self.__fields__["parameter_data"].default.parameter_names.append(form_data["input_parameter"])
# parameter_data is converted to a dictionary. why?
parameter_data["parameter_names"].append(form_data["input_parameter"])
@classmethod
def show_param_name(cls, param_name: str) -> rx.Component:
return rx.badge(param_name, color_scheme=cls.parameter_color[param_name])
@classmethod
def get_component(cls, **props):
cls.parameter_data = cls.__fields__["parameter_data"].default = props.pop("parameter_data")
# # TypeError: Cannot iterate over Var 'state__page_state.parameter_data?.parameter_names'. Instead use `rx.foreach`.
# colors = cls.__fields__["colors"].default
# cls.__fields__["parameter_color"].default = {
# parameter: colors[i % len(colors)]
# for i, parameter in enumerate(cls.parameter_data.parameter_names)
# }
# hard coding for test
cls.__fields__["parameter_color"].default = {"param_a": "indigo", "param_b": "cyan"}
return rx.vstack(
rx.hstack(
rx.foreach(
cls.parameter_data.parameter_names, lambda v: cls.show_param_name(v)
),
rx.form(
rx.input(placeholder="add param..", name="input_parameter"),
on_submit=lambda form_data: cls.add_parameter(form_data, cls.parameter_data)
)
),
margin="10px"
)
parameter_component = ParameterComponent.create
@rx.page(route="/test3", on_load=PageState.load)
def example3():
return parameter_component(
parameter_data=PageState.parameter_data
) As you can see, there are several issues with this code:
Could you please advise if there's any other approach I should try? |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm looking to implement color assignment for parameters in Reflex, where each parameter name is assigned a color and this mapping is stored. When a parameter name key is encountered, the stored color should be applied. The following code snippet achieves the desired functionality:
Using
on_load
to directly retrieve the dictionary works correctly and I can write the code as I intended.However, the problem arises when I want to separate it using
ComponentState
. Below is the attempt I made to separate it intoComponentState
. This is not the complete code, and I tried to output only the parameter names:When running the above code, a
KeyError
occurs as shown below, which seems to be due toparam_name
being converted to aVar
during theforeach
iteration:I have tried various methods, but it becomes difficult to write the desired logic because primitive types are converted to BaseVar in some way. Is there a way to extract primitive types from Var, or any appropriate methods to handle this in the code above?
Beta Was this translation helpful? Give feedback.
All reactions