reflex icon indicating copy to clipboard operation
reflex copied to clipboard

Dynamically add state vars

Open picklelo opened this issue 3 years ago β€’ 1 comments

Currently all state vars need to be defined as fields within the State class. Sometimes it is useful to dynamically add vars after the state class is already defined.

Minimally this requires:

  • logic to set all the relevant state var info: https://github.com/pynecone-io/pynecone/blob/main/pynecone/state.py#L114:
        for prop in cls.base_vars.values():
            if not utils._issubclass(prop.type_, utils.StateVar):
                raise TypeError(
                    "State vars must be primitive Python types, "
                    "Plotly figures, Pandas dataframes, "
                    "or subclasses of pc.Base. "
                    f'Found var "{prop.name}" with type {prop.type_}.'
                )
            cls._set_var(prop)
            cls._create_setter(prop)
            cls._set_default_value(prop)
  • adding it as a Pydantic field

Then we should be able to do something like

State.add_var(pc.BaseVar(name="myvar", type_="str"))

picklelo avatar Jan 21 '23 22:01 picklelo

I'll have a look at this issue.

Since I have a use case that would benefit from it, it should make testing it a bit easier.

Lendemor avatar Jan 25 '23 19:01 Lendemor