state.py 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637
  1. """Define the reflex state specification."""
  2. from __future__ import annotations
  3. import asyncio
  4. import builtins
  5. import contextlib
  6. import copy
  7. import dataclasses
  8. import functools
  9. import inspect
  10. import pickle
  11. import sys
  12. import typing
  13. import warnings
  14. from abc import ABC
  15. from collections.abc import AsyncIterator, Callable, Sequence
  16. from hashlib import md5
  17. from types import FunctionType
  18. from typing import (
  19. TYPE_CHECKING,
  20. Any,
  21. BinaryIO,
  22. ClassVar,
  23. TypeVar,
  24. cast,
  25. get_args,
  26. get_type_hints,
  27. )
  28. import pydantic.v1 as pydantic
  29. from pydantic import BaseModel as BaseModelV2
  30. from pydantic.v1 import BaseModel as BaseModelV1
  31. from pydantic.v1.fields import ModelField
  32. from rich.markup import escape
  33. from typing_extensions import Self
  34. import reflex.istate.dynamic
  35. from reflex import constants, event
  36. from reflex.base import Base
  37. from reflex.config import PerformanceMode, environment
  38. from reflex.event import (
  39. BACKGROUND_TASK_MARKER,
  40. Event,
  41. EventHandler,
  42. EventSpec,
  43. fix_events,
  44. )
  45. from reflex.istate.data import RouterData
  46. from reflex.istate.proxy import ImmutableMutableProxy as ImmutableMutableProxy
  47. from reflex.istate.proxy import MutableProxy, StateProxy
  48. from reflex.istate.storage import ClientStorageBase
  49. from reflex.model import Model
  50. from reflex.utils import console, format, prerequisites, types
  51. from reflex.utils.exceptions import (
  52. ComputedVarShadowsBaseVarsError,
  53. ComputedVarShadowsStateVarError,
  54. DynamicComponentInvalidSignatureError,
  55. DynamicRouteArgShadowsStateVarError,
  56. EventHandlerShadowsBuiltInStateMethodError,
  57. ReflexRuntimeError,
  58. SetUndefinedStateVarError,
  59. StateMismatchError,
  60. StateSchemaMismatchError,
  61. StateSerializationError,
  62. StateTooLargeError,
  63. UnretrievableVarValueError,
  64. )
  65. from reflex.utils.exceptions import ImmutableStateError as ImmutableStateError
  66. from reflex.utils.exec import is_testing_env
  67. from reflex.utils.types import (
  68. _isinstance,
  69. get_origin,
  70. is_union,
  71. true_type_for_pydantic_field,
  72. value_inside_optional,
  73. )
  74. from reflex.vars import VarData
  75. from reflex.vars.base import (
  76. ComputedVar,
  77. DynamicRouteVar,
  78. Var,
  79. computed_var,
  80. dispatch,
  81. is_computed_var,
  82. )
  83. if TYPE_CHECKING:
  84. from reflex.components.component import Component
  85. Delta = dict[str, Any]
  86. var = computed_var
  87. if environment.REFLEX_PERF_MODE.get() != PerformanceMode.OFF:
  88. # If the state is this large, it's considered a performance issue.
  89. TOO_LARGE_SERIALIZED_STATE = environment.REFLEX_STATE_SIZE_LIMIT.get() * 1024
  90. # Only warn about each state class size once.
  91. _WARNED_ABOUT_STATE_SIZE: set[str] = set()
  92. # Errors caught during pickling of state
  93. HANDLED_PICKLE_ERRORS = (
  94. pickle.PicklingError,
  95. AttributeError,
  96. IndexError,
  97. TypeError,
  98. ValueError,
  99. )
  100. # For BaseState.get_var_value
  101. VAR_TYPE = TypeVar("VAR_TYPE")
  102. def _no_chain_background_task(
  103. state_cls: type[BaseState], name: str, fn: Callable
  104. ) -> Callable:
  105. """Protect against directly chaining a background task from another event handler.
  106. Args:
  107. state_cls: The state class that the event handler is in.
  108. name: The name of the background task.
  109. fn: The background task coroutine function / generator.
  110. Returns:
  111. A compatible coroutine function / generator that raises a runtime error.
  112. Raises:
  113. TypeError: If the background task is not async.
  114. """
  115. call = f"{state_cls.__name__}.{name}"
  116. message = (
  117. f"Cannot directly call background task {name!r}, use "
  118. f"`yield {call}` or `return {call}` instead."
  119. )
  120. if inspect.iscoroutinefunction(fn):
  121. async def _no_chain_background_task_co(*args, **kwargs):
  122. raise RuntimeError(message)
  123. return _no_chain_background_task_co
  124. if inspect.isasyncgenfunction(fn):
  125. async def _no_chain_background_task_gen(*args, **kwargs):
  126. yield
  127. raise RuntimeError(message)
  128. return _no_chain_background_task_gen
  129. raise TypeError(f"{fn} is marked as a background task, but is not async.")
  130. def _substate_key(
  131. token: str,
  132. state_cls_or_name: BaseState | type[BaseState] | str | Sequence[str],
  133. ) -> str:
  134. """Get the substate key.
  135. Args:
  136. token: The token of the state.
  137. state_cls_or_name: The state class/instance or name or sequence of name parts.
  138. Returns:
  139. The substate key.
  140. """
  141. if isinstance(state_cls_or_name, BaseState) or (
  142. isinstance(state_cls_or_name, type) and issubclass(state_cls_or_name, BaseState)
  143. ):
  144. state_cls_or_name = state_cls_or_name.get_full_name()
  145. elif isinstance(state_cls_or_name, (list, tuple)):
  146. state_cls_or_name = ".".join(state_cls_or_name)
  147. return f"{token}_{state_cls_or_name}"
  148. def _split_substate_key(substate_key: str) -> tuple[str, str]:
  149. """Split the substate key into token and state name.
  150. Args:
  151. substate_key: The substate key.
  152. Returns:
  153. Tuple of token and state name.
  154. """
  155. token, _, state_name = substate_key.partition("_")
  156. return token, state_name
  157. @dataclasses.dataclass(frozen=True, init=False)
  158. class EventHandlerSetVar(EventHandler):
  159. """A special event handler to wrap setvar functionality."""
  160. state_cls: type[BaseState] = dataclasses.field(init=False)
  161. def __init__(self, state_cls: type[BaseState]):
  162. """Initialize the EventHandlerSetVar.
  163. Args:
  164. state_cls: The state class that vars will be set on.
  165. """
  166. super().__init__(
  167. fn=type(self).setvar,
  168. state_full_name=state_cls.get_full_name(),
  169. )
  170. object.__setattr__(self, "state_cls", state_cls)
  171. def setvar(self, var_name: str, value: Any):
  172. """Set the state variable to the value of the event.
  173. Note: `self` here will be an instance of the state, not EventHandlerSetVar.
  174. Args:
  175. var_name: The name of the variable to set.
  176. value: The value to set the variable to.
  177. """
  178. getattr(self, constants.SETTER_PREFIX + var_name)(value)
  179. def __call__(self, *args: Any) -> EventSpec:
  180. """Performs pre-checks and munging on the provided args that will become an EventSpec.
  181. Args:
  182. *args: The event args.
  183. Returns:
  184. The (partial) EventSpec that will be used to create the event to setvar.
  185. Raises:
  186. AttributeError: If the given Var name does not exist on the state.
  187. EventHandlerValueError: If the given Var name is not a str
  188. NotImplementedError: If the setter for the given Var is async
  189. """
  190. from reflex.utils.exceptions import EventHandlerValueError
  191. if args:
  192. if not isinstance(args[0], str):
  193. raise EventHandlerValueError(
  194. f"Var name must be passed as a string, got {args[0]!r}"
  195. )
  196. handler = getattr(self.state_cls, constants.SETTER_PREFIX + args[0], None)
  197. # Check that the requested Var setter exists on the State at compile time.
  198. if handler is None:
  199. raise AttributeError(
  200. f"Variable `{args[0]}` cannot be set on `{self.state_cls.get_full_name()}`"
  201. )
  202. if asyncio.iscoroutinefunction(handler.fn):
  203. raise NotImplementedError(
  204. f"Setter for {args[0]} is async, which is not supported."
  205. )
  206. return super().__call__(*args)
  207. if TYPE_CHECKING:
  208. from pydantic.v1.fields import ModelField
  209. def _unwrap_field_type(type_: types.GenericType) -> type:
  210. """Unwrap rx.Field type annotations.
  211. Args:
  212. type_: The type to unwrap.
  213. Returns:
  214. The unwrapped type.
  215. """
  216. from reflex.vars import Field
  217. if get_origin(type_) is Field:
  218. return get_args(type_)[0]
  219. return type_
  220. def get_var_for_field(cls: type[BaseState], f: ModelField) -> Var:
  221. """Get a Var instance for a Pydantic field.
  222. Args:
  223. cls: The state class.
  224. f: The Pydantic field.
  225. Returns:
  226. The Var instance.
  227. """
  228. name = f.name + "_rx_state_"
  229. field_name = format.format_state_name(cls.get_full_name()) + "." + name
  230. return dispatch(
  231. field_name=field_name,
  232. var_data=VarData.from_state(cls, f.name),
  233. result_var_type=_unwrap_field_type(true_type_for_pydantic_field(f)),
  234. )
  235. async def _resolve_delta(delta: Delta) -> Delta:
  236. """Await all coroutines in the delta.
  237. Args:
  238. delta: The delta to process.
  239. Returns:
  240. The same delta dict with all coroutines resolved to their return value.
  241. """
  242. tasks = {}
  243. for state_name, state_delta in delta.items():
  244. for var_name, value in state_delta.items():
  245. if asyncio.iscoroutine(value):
  246. tasks[state_name, var_name] = asyncio.create_task(value)
  247. for (state_name, var_name), task in tasks.items():
  248. delta[state_name][var_name] = await task
  249. return delta
  250. all_base_state_classes: dict[str, None] = {}
  251. class BaseState(Base, ABC, extra=pydantic.Extra.allow):
  252. """The state of the app."""
  253. # A map from the var name to the var.
  254. vars: ClassVar[builtins.dict[str, Var]] = {}
  255. # The base vars of the class.
  256. base_vars: ClassVar[builtins.dict[str, Var]] = {}
  257. # The computed vars of the class.
  258. computed_vars: ClassVar[builtins.dict[str, ComputedVar]] = {}
  259. # Vars inherited by the parent state.
  260. inherited_vars: ClassVar[builtins.dict[str, Var]] = {}
  261. # Backend base vars that are never sent to the client.
  262. backend_vars: ClassVar[builtins.dict[str, Any]] = {}
  263. # Backend base vars inherited
  264. inherited_backend_vars: ClassVar[builtins.dict[str, Any]] = {}
  265. # The event handlers.
  266. event_handlers: ClassVar[builtins.dict[str, EventHandler]] = {}
  267. # A set of subclassses of this class.
  268. class_subclasses: ClassVar[set[type[BaseState]]] = set()
  269. # Mapping of var name to set of (state_full_name, var_name) that depend on it.
  270. _var_dependencies: ClassVar[builtins.dict[str, set[tuple[str, str]]]] = {}
  271. # Set of vars which always need to be recomputed
  272. _always_dirty_computed_vars: ClassVar[set[str]] = set()
  273. # Set of substates which always need to be recomputed
  274. _always_dirty_substates: ClassVar[set[str]] = set()
  275. # Set of states which might need to be recomputed if vars in this state change.
  276. _potentially_dirty_states: ClassVar[set[str]] = set()
  277. # The parent state.
  278. parent_state: BaseState | None = None
  279. # The substates of the state.
  280. substates: builtins.dict[str, BaseState] = {}
  281. # The set of dirty vars.
  282. dirty_vars: set[str] = set()
  283. # The set of dirty substates.
  284. dirty_substates: set[str] = set()
  285. # The routing path that triggered the state
  286. router_data: builtins.dict[str, Any] = {}
  287. # Per-instance copy of backend base variable values
  288. _backend_vars: builtins.dict[str, Any] = {}
  289. # The router data for the current page
  290. router: RouterData = RouterData()
  291. # Whether the state has ever been touched since instantiation.
  292. _was_touched: bool = False
  293. # Whether this state class is a mixin and should not be instantiated.
  294. _mixin: ClassVar[bool] = False
  295. # A special event handler for setting base vars.
  296. setvar: ClassVar[EventHandler]
  297. def __init__(
  298. self,
  299. parent_state: BaseState | None = None,
  300. init_substates: bool = True,
  301. _reflex_internal_init: bool = False,
  302. **kwargs,
  303. ):
  304. """Initialize the state.
  305. DO NOT INSTANTIATE STATE CLASSES DIRECTLY! Use StateManager.get_state() instead.
  306. Args:
  307. parent_state: The parent state.
  308. init_substates: Whether to initialize the substates in this instance.
  309. _reflex_internal_init: A flag to indicate that the state is being initialized by the framework.
  310. **kwargs: The kwargs to set as attributes on the state.
  311. Raises:
  312. ReflexRuntimeError: If the state is instantiated directly by end user.
  313. """
  314. from reflex.utils.exceptions import ReflexRuntimeError
  315. if not _reflex_internal_init and not is_testing_env():
  316. raise ReflexRuntimeError(
  317. "State classes should not be instantiated directly in a Reflex app. "
  318. "See https://reflex.dev/docs/state/ for further information."
  319. )
  320. if type(self)._mixin:
  321. raise ReflexRuntimeError(
  322. f"{type(self).__name__} is a state mixin and cannot be instantiated directly."
  323. )
  324. kwargs["parent_state"] = parent_state
  325. super().__init__()
  326. for name, value in kwargs.items():
  327. setattr(self, name, value)
  328. # Setup the substates (for memory state manager only).
  329. if init_substates:
  330. for substate in self.get_substates():
  331. self.substates[substate.get_name()] = substate(
  332. parent_state=self,
  333. _reflex_internal_init=True,
  334. )
  335. # Create a fresh copy of the backend variables for this instance
  336. self._backend_vars = copy.deepcopy(self.backend_vars)
  337. def __repr__(self) -> str:
  338. """Get the string representation of the state.
  339. Returns:
  340. The string representation of the state.
  341. """
  342. return f"{type(self).__name__}({self.dict()})"
  343. @classmethod
  344. def _get_computed_vars(cls) -> list[tuple[str, ComputedVar]]:
  345. """Helper function to get all computed vars of a instance.
  346. Returns:
  347. A list of computed vars.
  348. """
  349. return [
  350. (name, v)
  351. for mixin in [*cls._mixins(), cls]
  352. for name, v in mixin.__dict__.items()
  353. if is_computed_var(v) and name not in cls.inherited_vars
  354. ]
  355. @classmethod
  356. def _validate_module_name(cls) -> None:
  357. """Check if the module name is valid.
  358. Reflex uses ___ as state name module separator.
  359. Raises:
  360. NameError: If the module name is invalid.
  361. """
  362. if "___" in cls.__module__:
  363. raise NameError(
  364. "The module name of a State class cannot contain '___'. "
  365. "Please rename the module."
  366. )
  367. @classmethod
  368. def __init_subclass__(cls, mixin: bool = False, **kwargs):
  369. """Do some magic for the subclass initialization.
  370. Args:
  371. mixin: Whether the subclass is a mixin and should not be initialized.
  372. **kwargs: The kwargs to pass to the pydantic init_subclass method.
  373. Raises:
  374. StateValueError: If a substate class shadows another.
  375. """
  376. from reflex.utils.exceptions import StateValueError
  377. super().__init_subclass__(**kwargs)
  378. cls._mixin = mixin
  379. if mixin:
  380. return
  381. # Handle locally-defined states for pickling.
  382. if "<locals>" in cls.__qualname__:
  383. cls._handle_local_def()
  384. # Validate the module name.
  385. cls._validate_module_name()
  386. # Event handlers should not shadow builtin state methods.
  387. cls._check_overridden_methods()
  388. # Computed vars should not shadow builtin state props.
  389. cls._check_overridden_basevars()
  390. # Reset subclass tracking for this class.
  391. cls.class_subclasses = set()
  392. # Reset dirty substate tracking for this class.
  393. cls._always_dirty_substates = set()
  394. cls._potentially_dirty_states = set()
  395. # Get the parent vars.
  396. parent_state = cls.get_parent_state()
  397. if parent_state is not None:
  398. cls.inherited_vars = parent_state.vars
  399. cls.inherited_backend_vars = parent_state.backend_vars
  400. # Check if another substate class with the same name has already been defined.
  401. if cls.get_name() in {c.get_name() for c in parent_state.class_subclasses}:
  402. # This should not happen, since we have added module prefix to state names in #3214
  403. raise StateValueError(
  404. f"The substate class '{cls.get_name()}' has been defined multiple times. "
  405. "Shadowing substate classes is not allowed."
  406. )
  407. # Track this new subclass in the parent state's subclasses set.
  408. parent_state.class_subclasses.add(cls)
  409. # Get computed vars.
  410. computed_vars = cls._get_computed_vars()
  411. cls._check_overridden_computed_vars()
  412. new_backend_vars = {
  413. name: value
  414. for name, value in cls.__dict__.items()
  415. if types.is_backend_base_variable(name, cls)
  416. }
  417. # Add annotated backend vars that may not have a default value.
  418. new_backend_vars.update(
  419. {
  420. name: cls._get_var_default(name, annotation_value)
  421. for name, annotation_value in cls._get_type_hints().items()
  422. if name not in new_backend_vars
  423. and types.is_backend_base_variable(name, cls)
  424. }
  425. )
  426. cls.backend_vars = {
  427. **cls.inherited_backend_vars,
  428. **new_backend_vars,
  429. }
  430. # Set the base and computed vars.
  431. cls.base_vars = {
  432. f.name: get_var_for_field(cls, f)
  433. for f in cls.get_fields().values()
  434. if f.name not in cls.get_skip_vars()
  435. }
  436. cls.computed_vars = {
  437. name: v._replace(merge_var_data=VarData.from_state(cls))
  438. for name, v in computed_vars
  439. }
  440. cls.vars = {
  441. **cls.inherited_vars,
  442. **cls.base_vars,
  443. **cls.computed_vars,
  444. }
  445. cls.event_handlers = {}
  446. # Setup the base vars at the class level.
  447. for name, prop in cls.base_vars.items():
  448. cls._init_var(name, prop)
  449. # Set up the event handlers.
  450. events = {
  451. name: fn
  452. for name, fn in cls.__dict__.items()
  453. if cls._item_is_event_handler(name, fn)
  454. }
  455. for mixin_cls in cls._mixins():
  456. for name, value in mixin_cls.__dict__.items():
  457. if name in cls.inherited_vars:
  458. continue
  459. if is_computed_var(value):
  460. fget = cls._copy_fn(value.fget)
  461. newcv = value._replace(fget=fget, _var_data=VarData.from_state(cls))
  462. # cleanup refs to mixin cls in var_data
  463. setattr(cls, name, newcv)
  464. cls.computed_vars[newcv._js_expr] = newcv
  465. cls.vars[newcv._js_expr] = newcv
  466. continue
  467. if types.is_backend_base_variable(name, mixin_cls):
  468. cls.backend_vars[name] = copy.deepcopy(value)
  469. continue
  470. if events.get(name) is not None:
  471. continue
  472. if not cls._item_is_event_handler(name, value):
  473. continue
  474. if parent_state is not None and parent_state.event_handlers.get(name):
  475. continue
  476. value = cls._copy_fn(value)
  477. value.__qualname__ = f"{cls.__name__}.{name}"
  478. events[name] = value
  479. # Create the setvar event handler for this state
  480. cls._create_setvar()
  481. for name, fn in events.items():
  482. handler = cls._create_event_handler(fn)
  483. cls.event_handlers[name] = handler
  484. setattr(cls, name, handler)
  485. # Initialize per-class var dependency tracking.
  486. cls._var_dependencies = {}
  487. cls._init_var_dependency_dicts()
  488. all_base_state_classes[cls.get_full_name()] = None
  489. @classmethod
  490. def _add_event_handler(
  491. cls,
  492. name: str,
  493. fn: Callable,
  494. ):
  495. """Add an event handler dynamically to the state.
  496. Args:
  497. name: The name of the event handler.
  498. fn: The function to call when the event is triggered.
  499. """
  500. handler = cls._create_event_handler(fn)
  501. cls.event_handlers[name] = handler
  502. setattr(cls, name, handler)
  503. @staticmethod
  504. def _copy_fn(fn: Callable) -> Callable:
  505. """Copy a function. Used to copy ComputedVars and EventHandlers from mixins.
  506. Args:
  507. fn: The function to copy.
  508. Returns:
  509. The copied function.
  510. """
  511. newfn = FunctionType(
  512. fn.__code__,
  513. fn.__globals__,
  514. name=fn.__name__,
  515. argdefs=fn.__defaults__,
  516. closure=fn.__closure__,
  517. )
  518. newfn.__annotations__ = fn.__annotations__
  519. if mark := getattr(fn, BACKGROUND_TASK_MARKER, None):
  520. setattr(newfn, BACKGROUND_TASK_MARKER, mark)
  521. return newfn
  522. @staticmethod
  523. def _item_is_event_handler(name: str, value: Any) -> bool:
  524. """Check if the item is an event handler.
  525. Args:
  526. name: The name of the item.
  527. value: The value of the item.
  528. Returns:
  529. Whether the item is an event handler.
  530. """
  531. return (
  532. not name.startswith("_")
  533. and isinstance(value, Callable)
  534. and not isinstance(value, EventHandler)
  535. and hasattr(value, "__code__")
  536. )
  537. @classmethod
  538. def _evaluate(cls, f: Callable[[Self], Any], of_type: type | None = None) -> Var:
  539. """Evaluate a function to a ComputedVar. Experimental.
  540. Args:
  541. f: The function to evaluate.
  542. of_type: The type of the ComputedVar. Defaults to Component.
  543. Returns:
  544. The ComputedVar.
  545. """
  546. console.warn(
  547. "The _evaluate method is experimental and may be removed in future versions."
  548. )
  549. from reflex.components.component import Component
  550. of_type = of_type or Component
  551. unique_var_name = (
  552. ("dynamic_" + f.__module__ + "_" + f.__qualname__)
  553. .replace("<", "")
  554. .replace(">", "")
  555. .replace(".", "_")
  556. )
  557. while unique_var_name in cls.vars:
  558. unique_var_name += "_"
  559. def computed_var_func(state: Self):
  560. result = f(state)
  561. if not _isinstance(result, of_type, nested=1, treat_var_as_type=False):
  562. console.warn(
  563. f"Inline ComputedVar {f} expected type {escape(str(of_type))}, got {type(result)}. "
  564. "You can specify expected type with `of_type` argument."
  565. )
  566. return result
  567. computed_var_func.__name__ = unique_var_name
  568. computed_var_func_arg = computed_var(return_type=of_type, cache=False)(
  569. computed_var_func
  570. )
  571. setattr(cls, unique_var_name, computed_var_func_arg)
  572. cls.computed_vars[unique_var_name] = computed_var_func_arg
  573. cls.vars[unique_var_name] = computed_var_func_arg
  574. cls._update_substate_inherited_vars({unique_var_name: computed_var_func_arg})
  575. cls._always_dirty_computed_vars.add(unique_var_name)
  576. return getattr(cls, unique_var_name)
  577. @classmethod
  578. def _mixins(cls) -> list[type]:
  579. """Get the mixin classes of the state.
  580. Returns:
  581. The mixin classes of the state.
  582. """
  583. return [
  584. mixin
  585. for mixin in cls.__mro__
  586. if (
  587. mixin not in [pydantic.BaseModel, Base, cls]
  588. and issubclass(mixin, BaseState)
  589. and mixin._mixin is True
  590. )
  591. ]
  592. @classmethod
  593. def _handle_local_def(cls):
  594. """Handle locally-defined states for pickling."""
  595. known_names = dir(reflex.istate.dynamic)
  596. proposed_name = cls.__name__
  597. for ix in range(len(known_names)):
  598. if proposed_name not in known_names:
  599. break
  600. proposed_name = f"{cls.__name__}_{ix}"
  601. setattr(reflex.istate.dynamic, proposed_name, cls)
  602. cls.__original_name__ = cls.__name__
  603. cls.__original_module__ = cls.__module__
  604. cls.__name__ = cls.__qualname__ = proposed_name
  605. cls.__module__ = reflex.istate.dynamic.__name__
  606. @classmethod
  607. def _get_type_hints(cls) -> dict[str, Any]:
  608. """Get the type hints for this class.
  609. If the class is dynamic, evaluate the type hints with the original
  610. module in the local namespace.
  611. Returns:
  612. The type hints dict.
  613. """
  614. original_module = getattr(cls, "__original_module__", None)
  615. if original_module is not None:
  616. localns = sys.modules[original_module].__dict__
  617. else:
  618. localns = None
  619. return get_type_hints(cls, localns=localns)
  620. @classmethod
  621. def _init_var_dependency_dicts(cls):
  622. """Initialize the var dependency tracking dicts.
  623. Allows the state to know which vars each ComputedVar depends on and
  624. whether a ComputedVar depends on a var in its parent state.
  625. Additional updates tracking dicts for vars and substates that always
  626. need to be recomputed.
  627. """
  628. for cvar_name, cvar in cls.computed_vars.items():
  629. if not cvar._cache:
  630. # Do not perform dep calculation when cache=False (these are always dirty).
  631. continue
  632. for state_name, dvar_set in cvar._deps(objclass=cls).items():
  633. state_cls = cls.get_root_state().get_class_substate(state_name)
  634. for dvar in dvar_set:
  635. defining_state_cls = state_cls
  636. while dvar in {
  637. *defining_state_cls.inherited_vars,
  638. *defining_state_cls.inherited_backend_vars,
  639. }:
  640. parent_state = defining_state_cls.get_parent_state()
  641. if parent_state is not None:
  642. defining_state_cls = parent_state
  643. defining_state_cls._var_dependencies.setdefault(dvar, set()).add(
  644. (cls.get_full_name(), cvar_name)
  645. )
  646. defining_state_cls._potentially_dirty_states.add(
  647. cls.get_full_name()
  648. )
  649. # ComputedVar with cache=False always need to be recomputed
  650. cls._always_dirty_computed_vars = {
  651. cvar_name
  652. for cvar_name, cvar in cls.computed_vars.items()
  653. if not cvar._cache
  654. }
  655. # Any substate containing a ComputedVar with cache=False always needs to be recomputed
  656. if cls._always_dirty_computed_vars:
  657. # Tell parent classes that this substate has always dirty computed vars
  658. state_name = cls.get_name()
  659. parent_state = cls.get_parent_state()
  660. while parent_state is not None:
  661. parent_state._always_dirty_substates.add(state_name)
  662. state_name, parent_state = (
  663. parent_state.get_name(),
  664. parent_state.get_parent_state(),
  665. )
  666. # Reset cached schema value
  667. cls._to_schema.cache_clear()
  668. @classmethod
  669. def _check_overridden_methods(cls):
  670. """Check for shadow methods and raise error if any.
  671. Raises:
  672. EventHandlerShadowsBuiltInStateMethodError: When an event handler shadows an inbuilt state method.
  673. """
  674. overridden_methods = set()
  675. state_base_functions = cls._get_base_functions()
  676. for name, method in inspect.getmembers(cls, inspect.isfunction):
  677. # Check if the method is overridden and not a dunder method
  678. if (
  679. not name.startswith("__")
  680. and method.__name__ in state_base_functions
  681. and state_base_functions[method.__name__] != method
  682. ):
  683. overridden_methods.add(method.__name__)
  684. for method_name in overridden_methods:
  685. raise EventHandlerShadowsBuiltInStateMethodError(
  686. f"The event handler name `{method_name}` shadows a builtin State method; use a different name instead"
  687. )
  688. @classmethod
  689. def _check_overridden_basevars(cls):
  690. """Check for shadow base vars and raise error if any.
  691. Raises:
  692. ComputedVarShadowsBaseVarsError: When a computed var shadows a base var.
  693. """
  694. for name, computed_var_ in cls._get_computed_vars():
  695. if name in cls.__annotations__:
  696. raise ComputedVarShadowsBaseVarsError(
  697. f"The computed var name `{computed_var_._js_expr}` shadows a base var in {cls.__module__}.{cls.__name__}; use a different name instead"
  698. )
  699. @classmethod
  700. def _check_overridden_computed_vars(cls) -> None:
  701. """Check for shadow computed vars and raise error if any.
  702. Raises:
  703. ComputedVarShadowsStateVarError: When a computed var shadows another.
  704. """
  705. for name, cv in cls.__dict__.items():
  706. if not is_computed_var(cv):
  707. continue
  708. name = cv._js_expr
  709. if name in cls.inherited_vars or name in cls.inherited_backend_vars:
  710. raise ComputedVarShadowsStateVarError(
  711. f"The computed var name `{cv._js_expr}` shadows a var in {cls.__module__}.{cls.__name__}; use a different name instead"
  712. )
  713. @classmethod
  714. def get_skip_vars(cls) -> set[str]:
  715. """Get the vars to skip when serializing.
  716. Returns:
  717. The vars to skip when serializing.
  718. """
  719. return (
  720. set(cls.inherited_vars)
  721. | {
  722. "parent_state",
  723. "substates",
  724. "dirty_vars",
  725. "dirty_substates",
  726. "router_data",
  727. }
  728. | types.RESERVED_BACKEND_VAR_NAMES
  729. )
  730. @classmethod
  731. @functools.lru_cache
  732. def get_parent_state(cls) -> type[BaseState] | None:
  733. """Get the parent state.
  734. Raises:
  735. ValueError: If more than one parent state is found.
  736. Returns:
  737. The parent state.
  738. """
  739. parent_states = [
  740. base
  741. for base in cls.__bases__
  742. if issubclass(base, BaseState) and base is not BaseState and not base._mixin
  743. ]
  744. if len(parent_states) >= 2:
  745. raise ValueError(f"Only one parent state is allowed {parent_states}.")
  746. # The first non-mixin state in the mro is our parent.
  747. for base in cls.mro()[1:]:
  748. if not issubclass(base, BaseState) or base._mixin:
  749. continue
  750. if base is BaseState:
  751. break
  752. return base
  753. return None # No known parent
  754. @classmethod
  755. @functools.lru_cache
  756. def get_root_state(cls) -> type[BaseState]:
  757. """Get the root state.
  758. Returns:
  759. The root state.
  760. """
  761. parent_state = cls.get_parent_state()
  762. return cls if parent_state is None else parent_state.get_root_state()
  763. @classmethod
  764. def get_substates(cls) -> set[type[BaseState]]:
  765. """Get the substates of the state.
  766. Returns:
  767. The substates of the state.
  768. """
  769. return cls.class_subclasses
  770. @classmethod
  771. @functools.lru_cache
  772. def get_name(cls) -> str:
  773. """Get the name of the state.
  774. Returns:
  775. The name of the state.
  776. """
  777. module = cls.__module__.replace(".", "___")
  778. return format.to_snake_case(f"{module}___{cls.__name__}")
  779. @classmethod
  780. @functools.lru_cache
  781. def get_full_name(cls) -> str:
  782. """Get the full name of the state.
  783. Returns:
  784. The full name of the state.
  785. """
  786. name = cls.get_name()
  787. parent_state = cls.get_parent_state()
  788. if parent_state is not None:
  789. name = ".".join((parent_state.get_full_name(), name))
  790. return name
  791. @classmethod
  792. @functools.lru_cache
  793. def get_class_substate(cls, path: Sequence[str] | str) -> type[BaseState]:
  794. """Get the class substate.
  795. Args:
  796. path: The path to the substate.
  797. Returns:
  798. The class substate.
  799. Raises:
  800. ValueError: If the substate is not found.
  801. """
  802. if isinstance(path, str):
  803. path = tuple(path.split("."))
  804. if len(path) == 0:
  805. return cls
  806. if path[0] == cls.get_name():
  807. if len(path) == 1:
  808. return cls
  809. path = path[1:]
  810. for substate in cls.get_substates():
  811. if path[0] == substate.get_name():
  812. return substate.get_class_substate(path[1:])
  813. raise ValueError(f"Invalid path: {path}")
  814. @classmethod
  815. def get_class_var(cls, path: Sequence[str]) -> Any:
  816. """Get the class var.
  817. Args:
  818. path: The path to the var.
  819. Returns:
  820. The class var.
  821. Raises:
  822. ValueError: If the path is invalid.
  823. """
  824. path, name = path[:-1], path[-1]
  825. substate = cls.get_class_substate(tuple(path))
  826. if not hasattr(substate, name):
  827. raise ValueError(f"Invalid path: {path}")
  828. return getattr(substate, name)
  829. @classmethod
  830. def _init_var(cls, name: str, prop: Var):
  831. """Initialize a variable.
  832. Args:
  833. name: The name of the variable
  834. prop: The variable to initialize
  835. Raises:
  836. VarTypeError: if the variable has an incorrect type
  837. """
  838. from reflex.utils.exceptions import VarTypeError
  839. if not types.is_valid_var_type(prop._var_type):
  840. raise VarTypeError(
  841. "State vars must be of a serializable type. "
  842. "Valid types include strings, numbers, booleans, lists, "
  843. "dictionaries, dataclasses, datetime objects, and pydantic models. "
  844. f'Found var "{prop._js_expr}" with type {prop._var_type}.'
  845. )
  846. cls._set_var(name, prop)
  847. cls._create_setter(name, prop)
  848. cls._set_default_value(name, prop)
  849. @classmethod
  850. def add_var(cls, name: str, type_: Any, default_value: Any = None):
  851. """Add dynamically a variable to the State.
  852. The variable added this way can be used in the same way as a variable
  853. defined statically in the model.
  854. Args:
  855. name: The name of the variable
  856. type_: The type of the variable
  857. default_value: The default value of the variable
  858. Raises:
  859. NameError: if a variable of this name already exists
  860. """
  861. if name in cls.__fields__:
  862. raise NameError(
  863. f"The variable '{name}' already exist. Use a different name"
  864. )
  865. # create the variable based on name and type
  866. var = Var(
  867. _js_expr=format.format_state_name(cls.get_full_name())
  868. + "."
  869. + name
  870. + "_rx_state_",
  871. _var_type=type_,
  872. _var_data=VarData.from_state(cls, name),
  873. ).guess_type()
  874. # add the pydantic field dynamically (must be done before _init_var)
  875. cls.add_field(name, var, default_value)
  876. cls._init_var(name, var)
  877. # update the internal dicts so the new variable is correctly handled
  878. cls.base_vars.update({name: var})
  879. cls.vars.update({name: var})
  880. # let substates know about the new variable
  881. for substate_class in cls.class_subclasses:
  882. substate_class.vars.setdefault(name, var)
  883. # Reinitialize dependency tracking dicts.
  884. cls._init_var_dependency_dicts()
  885. @classmethod
  886. def _set_var(cls, name: str, prop: Var):
  887. """Set the var as a class member.
  888. Args:
  889. name: The name of the var.
  890. prop: The var instance to set.
  891. """
  892. setattr(cls, name, prop)
  893. @classmethod
  894. def _create_event_handler(cls, fn: Any):
  895. """Create an event handler for the given function.
  896. Args:
  897. fn: The function to create an event handler for.
  898. Returns:
  899. The event handler.
  900. """
  901. return EventHandler(fn=fn, state_full_name=cls.get_full_name())
  902. @classmethod
  903. def _create_setvar(cls):
  904. """Create the setvar method for the state."""
  905. cls.setvar = cls.event_handlers["setvar"] = EventHandlerSetVar(state_cls=cls)
  906. @classmethod
  907. def _create_setter(cls, name: str, prop: Var):
  908. """Create a setter for the var.
  909. Args:
  910. name: The name of the var.
  911. prop: The var to create a setter for.
  912. """
  913. setter_name = Var._get_setter_name_for_name(name)
  914. if setter_name not in cls.__dict__:
  915. event_handler = cls._create_event_handler(prop._get_setter(name))
  916. cls.event_handlers[setter_name] = event_handler
  917. setattr(cls, setter_name, event_handler)
  918. @classmethod
  919. def _set_default_value(cls, name: str, prop: Var):
  920. """Set the default value for the var.
  921. Args:
  922. name: The name of the var.
  923. prop: The var to set the default value for.
  924. """
  925. # Get the pydantic field for the var.
  926. field = cls.get_fields()[name]
  927. if field.required:
  928. default_value = prop._get_default_value()
  929. if default_value is not None:
  930. field.required = False
  931. field.default = default_value
  932. if (
  933. not field.required
  934. and field.default is None
  935. and field.default_factory is None
  936. and not types.is_optional(prop._var_type)
  937. ):
  938. # Ensure frontend uses null coalescing when accessing.
  939. object.__setattr__(prop, "_var_type", prop._var_type | None)
  940. @classmethod
  941. def _get_var_default(cls, name: str, annotation_value: Any) -> Any:
  942. """Get the default value of a (backend) var.
  943. Args:
  944. name: The name of the var.
  945. annotation_value: The annotation value of the var.
  946. Returns:
  947. The default value of the var or None.
  948. """
  949. try:
  950. return getattr(cls, name)
  951. except AttributeError:
  952. try:
  953. return Var("", _var_type=annotation_value)._get_default_value()
  954. except TypeError:
  955. pass
  956. return None
  957. @staticmethod
  958. def _get_base_functions() -> dict[str, FunctionType]:
  959. """Get all functions of the state class excluding dunder methods.
  960. Returns:
  961. The functions of rx.State class as a dict.
  962. """
  963. return {
  964. func[0]: func[1]
  965. for func in inspect.getmembers(BaseState, predicate=inspect.isfunction)
  966. if not func[0].startswith("__")
  967. }
  968. @classmethod
  969. def _update_substate_inherited_vars(cls, vars_to_add: dict[str, Var]):
  970. """Update the inherited vars of substates recursively when new vars are added.
  971. Also updates the var dependency tracking dicts after adding vars.
  972. Args:
  973. vars_to_add: names to Var instances to add to substates
  974. """
  975. for substate_class in cls.class_subclasses:
  976. for name, var in vars_to_add.items():
  977. if types.is_backend_base_variable(name, cls):
  978. substate_class.backend_vars.setdefault(name, var)
  979. substate_class.inherited_backend_vars.setdefault(name, var)
  980. else:
  981. substate_class.vars.setdefault(name, var)
  982. substate_class.inherited_vars.setdefault(name, var)
  983. substate_class._update_substate_inherited_vars(vars_to_add)
  984. # Reinitialize dependency tracking dicts.
  985. cls._init_var_dependency_dicts()
  986. @classmethod
  987. def setup_dynamic_args(cls, args: dict[str, str]):
  988. """Set up args for easy access in renderer.
  989. Args:
  990. args: a dict of args
  991. """
  992. if not args:
  993. return
  994. cls._check_overwritten_dynamic_args(list(args.keys()))
  995. def argsingle_factory(param: str):
  996. def inner_func(self: BaseState) -> str:
  997. return self.router.page.params.get(param, "")
  998. return inner_func
  999. def arglist_factory(param: str):
  1000. def inner_func(self: BaseState) -> list[str]:
  1001. return self.router.page.params.get(param, [])
  1002. return inner_func
  1003. dynamic_vars = {}
  1004. for param, value in args.items():
  1005. if value == constants.RouteArgType.SINGLE:
  1006. func = argsingle_factory(param)
  1007. elif value == constants.RouteArgType.LIST:
  1008. func = arglist_factory(param)
  1009. else:
  1010. continue
  1011. dynamic_vars[param] = DynamicRouteVar(
  1012. fget=func,
  1013. auto_deps=False,
  1014. deps=["router"],
  1015. _js_expr=param,
  1016. _var_data=VarData.from_state(cls),
  1017. )
  1018. setattr(cls, param, dynamic_vars[param])
  1019. # Update tracking dicts.
  1020. cls.computed_vars.update(dynamic_vars)
  1021. cls.vars.update(dynamic_vars)
  1022. cls._update_substate_inherited_vars(dynamic_vars)
  1023. @classmethod
  1024. def _check_overwritten_dynamic_args(cls, args: list[str]):
  1025. """Check if dynamic args are shadowing existing vars. Recursively checks all child states.
  1026. Args:
  1027. args: a dict of args
  1028. Raises:
  1029. DynamicRouteArgShadowsStateVarError: If a dynamic arg is shadowing an existing var.
  1030. """
  1031. for arg in args:
  1032. if (
  1033. arg in cls.computed_vars
  1034. and not isinstance(cls.computed_vars[arg], DynamicRouteVar)
  1035. ) or arg in cls.base_vars:
  1036. raise DynamicRouteArgShadowsStateVarError(
  1037. f"Dynamic route arg '{arg}' is shadowing an existing var in {cls.__module__}.{cls.__name__}"
  1038. )
  1039. for substate in cls.get_substates():
  1040. substate._check_overwritten_dynamic_args(args)
  1041. def __getattribute__(self, name: str) -> Any:
  1042. """Get the state var.
  1043. If the var is inherited, get the var from the parent state.
  1044. Args:
  1045. name: The name of the var.
  1046. Returns:
  1047. The value of the var.
  1048. """
  1049. # If the state hasn't been initialized yet, return the default value.
  1050. if not super().__getattribute__("__dict__"):
  1051. return super().__getattribute__(name)
  1052. # Fast path for dunder
  1053. if name.startswith("__"):
  1054. return super().__getattribute__(name)
  1055. # For now, handle router_data updates as a special case.
  1056. if (
  1057. name == constants.ROUTER_DATA
  1058. or name in super().__getattribute__("inherited_vars")
  1059. or name in super().__getattribute__("inherited_backend_vars")
  1060. ):
  1061. parent_state = super().__getattribute__("parent_state")
  1062. if parent_state is not None:
  1063. return getattr(parent_state, name)
  1064. # Allow event handlers to be called on the instance directly.
  1065. event_handlers = super().__getattribute__("event_handlers")
  1066. if name in event_handlers:
  1067. handler = event_handlers[name]
  1068. if handler.is_background:
  1069. fn = _no_chain_background_task(type(self), name, handler.fn)
  1070. else:
  1071. fn = functools.partial(handler.fn, self)
  1072. fn.__module__ = handler.fn.__module__
  1073. fn.__qualname__ = handler.fn.__qualname__
  1074. return fn
  1075. backend_vars = super().__getattribute__("_backend_vars")
  1076. if name in backend_vars:
  1077. value = backend_vars[name]
  1078. else:
  1079. value = super().__getattribute__(name)
  1080. if isinstance(value, EventHandler):
  1081. # The event handler is inherited from a parent, so let the parent convert
  1082. # it to a callable function.
  1083. parent_state = super().__getattribute__("parent_state")
  1084. if parent_state is not None:
  1085. return getattr(parent_state, name)
  1086. if MutableProxy._is_mutable_type(value) and (
  1087. name in super().__getattribute__("base_vars") or name in backend_vars
  1088. ):
  1089. # track changes in mutable containers (list, dict, set, etc)
  1090. return MutableProxy(wrapped=value, state=self, field_name=name)
  1091. return value
  1092. def __setattr__(self, name: str, value: Any):
  1093. """Set the attribute.
  1094. If the attribute is inherited, set the attribute on the parent state.
  1095. Args:
  1096. name: The name of the attribute.
  1097. value: The value of the attribute.
  1098. Raises:
  1099. SetUndefinedStateVarError: If a value of a var is set without first defining it.
  1100. """
  1101. if isinstance(value, MutableProxy):
  1102. # unwrap proxy objects when assigning back to the state
  1103. value = value.__wrapped__
  1104. # Set the var on the parent state.
  1105. if name in self.inherited_vars or name in self.inherited_backend_vars:
  1106. setattr(self.parent_state, name, value)
  1107. return
  1108. if name in self.backend_vars:
  1109. self._backend_vars.__setitem__(name, value)
  1110. self.dirty_vars.add(name)
  1111. self._mark_dirty()
  1112. return
  1113. if (
  1114. name not in self.vars
  1115. and name not in self.get_skip_vars()
  1116. and not name.startswith("__")
  1117. and not name.startswith(
  1118. f"_{getattr(type(self), '__original_name__', type(self).__name__)}__"
  1119. )
  1120. ):
  1121. raise SetUndefinedStateVarError(
  1122. f"The state variable '{name}' has not been defined in '{type(self).__name__}'. "
  1123. f"All state variables must be declared before they can be set."
  1124. )
  1125. fields = self.get_fields()
  1126. if name in fields:
  1127. field = fields[name]
  1128. field_type = _unwrap_field_type(true_type_for_pydantic_field(field))
  1129. if not _isinstance(value, field_type, nested=1, treat_var_as_type=False):
  1130. console.error(
  1131. f"Expected field '{type(self).__name__}.{name}' to receive type '{escape(str(field_type))}',"
  1132. f" but got '{value}' of type '{type(value)}'."
  1133. )
  1134. # Set the attribute.
  1135. super().__setattr__(name, value)
  1136. # Add the var to the dirty list.
  1137. if name in self.base_vars:
  1138. self.dirty_vars.add(name)
  1139. self._mark_dirty()
  1140. # For now, handle router_data updates as a special case
  1141. if name == constants.ROUTER_DATA:
  1142. self.dirty_vars.add(name)
  1143. self._mark_dirty()
  1144. def reset(self):
  1145. """Reset all the base vars to their default values."""
  1146. # Reset the base vars.
  1147. fields = self.get_fields()
  1148. for prop_name in self.base_vars:
  1149. if prop_name == constants.ROUTER:
  1150. continue # never reset the router data
  1151. field = fields[prop_name]
  1152. if default_factory := field.default_factory:
  1153. default = default_factory()
  1154. else:
  1155. default = copy.deepcopy(field.default)
  1156. setattr(self, prop_name, default)
  1157. # Reset the backend vars.
  1158. for prop_name, value in self.backend_vars.items():
  1159. setattr(self, prop_name, copy.deepcopy(value))
  1160. # Recursively reset the substates.
  1161. for substate in self.substates.values():
  1162. substate.reset()
  1163. def _reset_client_storage(self):
  1164. """Reset client storage base vars to their default values."""
  1165. # Client-side storage is reset during hydrate so that clearing cookies
  1166. # on the browser also resets the values on the backend.
  1167. fields = self.get_fields()
  1168. for prop_name in self.base_vars:
  1169. field = fields[prop_name]
  1170. if isinstance(field.default, ClientStorageBase) or (
  1171. isinstance(field.type_, type)
  1172. and issubclass(field.type_, ClientStorageBase)
  1173. ):
  1174. setattr(self, prop_name, copy.deepcopy(field.default))
  1175. # Recursively reset the substate client storage.
  1176. for substate in self.substates.values():
  1177. substate._reset_client_storage()
  1178. def get_substate(self, path: Sequence[str]) -> BaseState:
  1179. """Get the substate.
  1180. Args:
  1181. path: The path to the substate.
  1182. Returns:
  1183. The substate.
  1184. Raises:
  1185. ValueError: If the substate is not found.
  1186. """
  1187. if len(path) == 0:
  1188. return self
  1189. if path[0] == self.get_name():
  1190. if len(path) == 1:
  1191. return self
  1192. path = path[1:]
  1193. if path[0] not in self.substates:
  1194. raise ValueError(f"Invalid path: {path}")
  1195. return self.substates[path[0]].get_substate(path[1:])
  1196. @classmethod
  1197. def _get_potentially_dirty_states(cls) -> set[type[BaseState]]:
  1198. """Get substates which may have dirty vars due to dependencies.
  1199. Returns:
  1200. The set of potentially dirty substate classes.
  1201. """
  1202. return {
  1203. cls.get_class_substate(substate_name)
  1204. for substate_name in cls._always_dirty_substates
  1205. }.union(
  1206. {
  1207. cls.get_root_state().get_class_substate(substate_name)
  1208. for substate_name in cls._potentially_dirty_states
  1209. }
  1210. )
  1211. def _get_root_state(self) -> BaseState:
  1212. """Get the root state of the state tree.
  1213. Returns:
  1214. The root state of the state tree.
  1215. """
  1216. parent_state = self
  1217. while parent_state.parent_state is not None:
  1218. parent_state = parent_state.parent_state
  1219. return parent_state
  1220. async def _get_state_from_redis(self, state_cls: type[T_STATE]) -> T_STATE:
  1221. """Get a state instance from redis.
  1222. Args:
  1223. state_cls: The class of the state.
  1224. Returns:
  1225. The instance of state_cls associated with this state's client_token.
  1226. Raises:
  1227. RuntimeError: If redis is not used in this backend process.
  1228. StateMismatchError: If the state instance is not of the expected type.
  1229. """
  1230. # Then get the target state and all its substates.
  1231. state_manager = get_state_manager()
  1232. if not isinstance(state_manager, StateManagerRedis):
  1233. raise RuntimeError(
  1234. f"Requested state {state_cls.get_full_name()} is not cached and cannot be accessed without redis. "
  1235. "(All states should already be available -- this is likely a bug).",
  1236. )
  1237. state_in_redis = await state_manager.get_state(
  1238. token=_substate_key(self.router.session.client_token, state_cls),
  1239. top_level=False,
  1240. for_state_instance=self,
  1241. )
  1242. if not isinstance(state_in_redis, state_cls):
  1243. raise StateMismatchError(
  1244. f"Searched for state {state_cls.get_full_name()} but found {state_in_redis}."
  1245. )
  1246. return state_in_redis
  1247. def _get_state_from_cache(self, state_cls: type[T_STATE]) -> T_STATE:
  1248. """Get a state instance from the cache.
  1249. Args:
  1250. state_cls: The class of the state.
  1251. Returns:
  1252. The instance of state_cls associated with this state's client_token.
  1253. Raises:
  1254. StateMismatchError: If the state instance is not of the expected type.
  1255. """
  1256. root_state = self._get_root_state()
  1257. substate = root_state.get_substate(state_cls.get_full_name().split("."))
  1258. if not isinstance(substate, state_cls):
  1259. raise StateMismatchError(
  1260. f"Searched for state {state_cls.get_full_name()} but found {substate}."
  1261. )
  1262. return substate
  1263. async def get_state(self, state_cls: type[T_STATE]) -> T_STATE:
  1264. """Get an instance of the state associated with this token.
  1265. Allows for arbitrary access to sibling states from within an event handler.
  1266. Args:
  1267. state_cls: The class of the state.
  1268. Returns:
  1269. The instance of state_cls associated with this state's client_token.
  1270. """
  1271. # Fast case - if this state instance is already cached, get_substate from root state.
  1272. try:
  1273. return self._get_state_from_cache(state_cls)
  1274. except ValueError:
  1275. pass
  1276. # Slow case - fetch missing parent states from redis.
  1277. return await self._get_state_from_redis(state_cls)
  1278. async def get_var_value(self, var: Var[VAR_TYPE]) -> VAR_TYPE:
  1279. """Get the value of an rx.Var from another state.
  1280. Args:
  1281. var: The var to get the value for.
  1282. Returns:
  1283. The value of the var.
  1284. Raises:
  1285. UnretrievableVarValueError: If the var does not have a literal value
  1286. or associated state.
  1287. """
  1288. # Oopsie case: you didn't give me a Var... so get what you give.
  1289. if not isinstance(var, Var):
  1290. return var
  1291. unset = object()
  1292. # Fast case: this is a literal var and the value is known.
  1293. if (
  1294. var_value := getattr(var, "_var_value", unset)
  1295. ) is not unset and not isinstance(var_value, Var):
  1296. return var_value # pyright: ignore [reportReturnType]
  1297. var_data = var._get_all_var_data()
  1298. if var_data is None or not var_data.state:
  1299. raise UnretrievableVarValueError(
  1300. f"Unable to retrieve value for {var._js_expr}: not associated with any state."
  1301. )
  1302. # Fastish case: this var belongs to this state
  1303. if var_data.state == self.get_full_name():
  1304. return getattr(self, var_data.field_name)
  1305. # Slow case: this var belongs to another state
  1306. other_state = await self.get_state(
  1307. self._get_root_state().get_class_substate(var_data.state)
  1308. )
  1309. return getattr(other_state, var_data.field_name)
  1310. def _get_event_handler(
  1311. self, event: Event
  1312. ) -> tuple[BaseState | StateProxy, EventHandler]:
  1313. """Get the event handler for the given event.
  1314. Args:
  1315. event: The event to get the handler for.
  1316. Returns:
  1317. The event handler.
  1318. Raises:
  1319. ValueError: If the event handler or substate is not found.
  1320. """
  1321. # Get the event handler.
  1322. path = event.name.split(".")
  1323. path, name = path[:-1], path[-1]
  1324. substate = self.get_substate(path)
  1325. if not substate:
  1326. raise ValueError(
  1327. "The value of state cannot be None when processing an event."
  1328. )
  1329. handler = substate.event_handlers[name]
  1330. # For background tasks, proxy the state
  1331. if handler.is_background:
  1332. substate = StateProxy(substate)
  1333. return substate, handler
  1334. async def _process(self, event: Event) -> AsyncIterator[StateUpdate]:
  1335. """Obtain event info and process event.
  1336. Args:
  1337. event: The event to process.
  1338. Yields:
  1339. The state update after processing the event.
  1340. """
  1341. # Get the event handler.
  1342. substate, handler = self._get_event_handler(event)
  1343. # Run the event generator and yield state updates.
  1344. async for update in self._process_event(
  1345. handler=handler,
  1346. state=substate,
  1347. payload=event.payload,
  1348. ):
  1349. yield update
  1350. def _check_valid(self, handler: EventHandler, events: Any) -> Any:
  1351. """Check if the events yielded are valid. They must be EventHandlers or EventSpecs.
  1352. Args:
  1353. handler: EventHandler.
  1354. events: The events to be checked.
  1355. Raises:
  1356. TypeError: If any of the events are not valid.
  1357. Returns:
  1358. The events as they are if valid.
  1359. """
  1360. def _is_valid_type(events: Any) -> bool:
  1361. return isinstance(events, (Event, EventHandler, EventSpec))
  1362. if events is None or _is_valid_type(events):
  1363. return events
  1364. if not isinstance(events, Sequence):
  1365. events = [events]
  1366. try:
  1367. if all(_is_valid_type(e) for e in events):
  1368. return events
  1369. except TypeError:
  1370. pass
  1371. coroutines = [e for e in events if asyncio.iscoroutine(e)]
  1372. for coroutine in coroutines:
  1373. coroutine_name = coroutine.__qualname__
  1374. warnings.filterwarnings(
  1375. "ignore", message=f"coroutine '{coroutine_name}' was never awaited"
  1376. )
  1377. raise TypeError(
  1378. f"Your handler {handler.fn.__qualname__} must only return/yield: None, Events or other EventHandlers referenced by their class (i.e. using `type(self)` or other class references)."
  1379. f" Returned events of types {', '.join(map(str, map(type, events)))!s}."
  1380. )
  1381. async def _as_state_update(
  1382. self,
  1383. handler: EventHandler,
  1384. events: EventSpec | list[EventSpec] | None,
  1385. final: bool,
  1386. ) -> StateUpdate:
  1387. """Convert the events to a StateUpdate.
  1388. Fixes the events and checks for validity before converting.
  1389. Args:
  1390. handler: The handler where the events originated from.
  1391. events: The events to queue with the update.
  1392. final: Whether the handler is done processing.
  1393. Returns:
  1394. The valid StateUpdate containing the events and final flag.
  1395. """
  1396. # get the delta from the root of the state tree
  1397. state = self._get_root_state()
  1398. token = self.router.session.client_token
  1399. # Convert valid EventHandler and EventSpec into Event
  1400. fixed_events = fix_events(self._check_valid(handler, events), token)
  1401. try:
  1402. # Get the delta after processing the event.
  1403. delta = await state._get_resolved_delta()
  1404. state._clean()
  1405. return StateUpdate(
  1406. delta=delta,
  1407. events=fixed_events,
  1408. final=final if not handler.is_background else True,
  1409. )
  1410. except Exception as ex:
  1411. state._clean()
  1412. event_specs = (
  1413. prerequisites.get_and_validate_app().app.backend_exception_handler(ex)
  1414. )
  1415. if event_specs is None:
  1416. return StateUpdate()
  1417. event_specs_correct_type = cast(
  1418. list[EventSpec | EventHandler] | None,
  1419. [event_specs] if isinstance(event_specs, EventSpec) else event_specs,
  1420. )
  1421. fixed_events = fix_events(
  1422. event_specs_correct_type,
  1423. token,
  1424. router_data=state.router_data,
  1425. )
  1426. return StateUpdate(
  1427. events=fixed_events,
  1428. final=True,
  1429. )
  1430. async def _process_event(
  1431. self,
  1432. handler: EventHandler,
  1433. state: BaseState | StateProxy,
  1434. payload: builtins.dict,
  1435. ) -> AsyncIterator[StateUpdate]:
  1436. """Process event.
  1437. Args:
  1438. handler: EventHandler to process.
  1439. state: State to process the handler.
  1440. payload: The event payload.
  1441. Yields:
  1442. StateUpdate object
  1443. Raises:
  1444. ValueError: If a string value is received for an int or float type and cannot be converted.
  1445. """
  1446. from reflex.utils import telemetry
  1447. # Get the function to process the event.
  1448. fn = functools.partial(handler.fn, state)
  1449. try:
  1450. type_hints = typing.get_type_hints(handler.fn)
  1451. except Exception:
  1452. type_hints = {}
  1453. for arg, value in list(payload.items()):
  1454. hinted_args = type_hints.get(arg, Any)
  1455. if hinted_args is Any:
  1456. continue
  1457. if is_union(hinted_args):
  1458. if value is None:
  1459. continue
  1460. hinted_args = value_inside_optional(hinted_args)
  1461. if (
  1462. isinstance(value, dict)
  1463. and inspect.isclass(hinted_args)
  1464. and not types.is_generic_alias(hinted_args) # py3.10
  1465. ):
  1466. if issubclass(hinted_args, Model):
  1467. # Remove non-fields from the payload
  1468. payload[arg] = hinted_args(
  1469. **{
  1470. key: value
  1471. for key, value in value.items()
  1472. if key in hinted_args.__fields__
  1473. }
  1474. )
  1475. elif dataclasses.is_dataclass(hinted_args) or issubclass(
  1476. hinted_args, (Base, BaseModelV1, BaseModelV2)
  1477. ):
  1478. payload[arg] = hinted_args(**value)
  1479. elif isinstance(value, list) and (hinted_args is set or hinted_args is set):
  1480. payload[arg] = set(value)
  1481. elif isinstance(value, list) and (
  1482. hinted_args is tuple or hinted_args is tuple
  1483. ):
  1484. payload[arg] = tuple(value)
  1485. elif isinstance(value, str) and (
  1486. hinted_args is int or hinted_args is float
  1487. ):
  1488. try:
  1489. payload[arg] = hinted_args(value)
  1490. except ValueError:
  1491. raise ValueError(
  1492. f"Received a string value ({value}) for {arg} but expected a {hinted_args}"
  1493. ) from None
  1494. else:
  1495. console.warn(
  1496. f"Received a string value ({value}) for {arg} but expected a {hinted_args}. A simple conversion was successful."
  1497. )
  1498. # Wrap the function in a try/except block.
  1499. try:
  1500. # Handle async functions.
  1501. if asyncio.iscoroutinefunction(fn.func):
  1502. events = await fn(**payload)
  1503. # Handle regular functions.
  1504. else:
  1505. events = fn(**payload)
  1506. # Handle async generators.
  1507. if inspect.isasyncgen(events):
  1508. async for event in events:
  1509. yield await state._as_state_update(handler, event, final=False)
  1510. yield await state._as_state_update(handler, events=None, final=True)
  1511. # Handle regular generators.
  1512. elif inspect.isgenerator(events):
  1513. try:
  1514. while True:
  1515. yield await state._as_state_update(
  1516. handler, next(events), final=False
  1517. )
  1518. except StopIteration as si:
  1519. # the "return" value of the generator is not available
  1520. # in the loop, we must catch StopIteration to access it
  1521. if si.value is not None:
  1522. yield await state._as_state_update(
  1523. handler, si.value, final=False
  1524. )
  1525. yield await state._as_state_update(handler, events=None, final=True)
  1526. # Handle regular event chains.
  1527. else:
  1528. yield await state._as_state_update(handler, events, final=True)
  1529. # If an error occurs, throw a window alert.
  1530. except Exception as ex:
  1531. telemetry.send_error(ex, context="backend")
  1532. event_specs = (
  1533. prerequisites.get_and_validate_app().app.backend_exception_handler(ex)
  1534. )
  1535. yield await state._as_state_update(
  1536. handler,
  1537. event_specs,
  1538. final=True,
  1539. )
  1540. def _mark_dirty_computed_vars(self) -> None:
  1541. """Mark ComputedVars that need to be recalculated based on dirty_vars."""
  1542. # Append expired computed vars to dirty_vars to trigger recalculation
  1543. self.dirty_vars.update(self._expired_computed_vars())
  1544. # Append always dirty computed vars to dirty_vars to trigger recalculation
  1545. self.dirty_vars.update(self._always_dirty_computed_vars)
  1546. dirty_vars = self.dirty_vars
  1547. while dirty_vars:
  1548. calc_vars, dirty_vars = dirty_vars, set()
  1549. for state_name, cvar in self._dirty_computed_vars(from_vars=calc_vars):
  1550. if state_name == self.get_full_name():
  1551. defining_state = self
  1552. else:
  1553. defining_state = self._get_root_state().get_substate(
  1554. tuple(state_name.split("."))
  1555. )
  1556. defining_state.dirty_vars.add(cvar)
  1557. actual_var = defining_state.computed_vars.get(cvar)
  1558. if actual_var is not None:
  1559. actual_var.mark_dirty(instance=defining_state)
  1560. if defining_state is self:
  1561. dirty_vars.add(cvar)
  1562. else:
  1563. # mark dirty where this var is defined
  1564. defining_state._mark_dirty()
  1565. def _expired_computed_vars(self) -> set[str]:
  1566. """Determine ComputedVars that need to be recalculated based on the expiration time.
  1567. Returns:
  1568. Set of computed vars to include in the delta.
  1569. """
  1570. return {
  1571. cvar
  1572. for cvar in self.computed_vars
  1573. if self.computed_vars[cvar].needs_update(instance=self)
  1574. }
  1575. def _dirty_computed_vars(
  1576. self, from_vars: set[str] | None = None, include_backend: bool = True
  1577. ) -> set[tuple[str, str]]:
  1578. """Determine ComputedVars that need to be recalculated based on the given vars.
  1579. Args:
  1580. from_vars: find ComputedVar that depend on this set of vars. If unspecified, will use the dirty_vars.
  1581. include_backend: whether to include backend vars in the calculation.
  1582. Returns:
  1583. Set of computed vars to include in the delta.
  1584. """
  1585. return {
  1586. (state_name, cvar)
  1587. for dirty_var in from_vars or self.dirty_vars
  1588. for state_name, cvar in self._var_dependencies.get(dirty_var, set())
  1589. if include_backend or not self.computed_vars[cvar]._backend
  1590. }
  1591. def get_delta(self) -> Delta:
  1592. """Get the delta for the state.
  1593. Returns:
  1594. The delta for the state.
  1595. """
  1596. delta = {}
  1597. self._mark_dirty_computed_vars()
  1598. frontend_computed_vars: set[str] = {
  1599. name for name, cv in self.computed_vars.items() if not cv._backend
  1600. }
  1601. # Return the dirty vars for this instance, any cached/dependent computed vars,
  1602. # and always dirty computed vars (cache=False)
  1603. delta_vars = self.dirty_vars.intersection(self.base_vars).union(
  1604. self.dirty_vars.intersection(frontend_computed_vars)
  1605. )
  1606. subdelta: dict[str, Any] = {
  1607. prop + "_rx_state_": self.get_value(prop)
  1608. for prop in delta_vars
  1609. if not types.is_backend_base_variable(prop, type(self))
  1610. }
  1611. if len(subdelta) > 0:
  1612. delta[self.get_full_name()] = subdelta
  1613. # Recursively find the substate deltas.
  1614. substates = self.substates
  1615. for substate in self.dirty_substates.union(self._always_dirty_substates):
  1616. delta.update(substates[substate].get_delta())
  1617. # Return the delta.
  1618. return delta
  1619. async def _get_resolved_delta(self) -> Delta:
  1620. """Get the delta for the state after resolving all coroutines.
  1621. Returns:
  1622. The resolved delta for the state.
  1623. """
  1624. return await _resolve_delta(self.get_delta())
  1625. def _mark_dirty(self):
  1626. """Mark the substate and all parent states as dirty."""
  1627. state_name = self.get_name()
  1628. if (
  1629. self.parent_state is not None
  1630. and state_name not in self.parent_state.dirty_substates
  1631. ):
  1632. self.parent_state.dirty_substates.add(self.get_name())
  1633. self.parent_state._mark_dirty()
  1634. # have to mark computed vars dirty to allow access to newly computed
  1635. # values within the same ComputedVar function
  1636. self._mark_dirty_computed_vars()
  1637. def _update_was_touched(self):
  1638. """Update the _was_touched flag based on dirty_vars."""
  1639. if self.dirty_vars and not self._was_touched:
  1640. for var in self.dirty_vars:
  1641. if var in self.base_vars or var in self._backend_vars:
  1642. self._was_touched = True
  1643. break
  1644. if var == constants.ROUTER_DATA and self.parent_state is None:
  1645. self._was_touched = True
  1646. break
  1647. def _get_was_touched(self) -> bool:
  1648. """Check current dirty_vars and flag to determine if state instance was modified.
  1649. If any dirty vars belong to this state, mark _was_touched.
  1650. This flag determines whether this state instance should be persisted to redis.
  1651. Returns:
  1652. Whether this state instance was ever modified.
  1653. """
  1654. # Ensure the flag is up to date based on the current dirty_vars
  1655. self._update_was_touched()
  1656. return self._was_touched
  1657. def _clean(self):
  1658. """Reset the dirty vars."""
  1659. # Update touched status before cleaning dirty_vars.
  1660. self._update_was_touched()
  1661. # Recursively clean the substates.
  1662. for substate in self.dirty_substates:
  1663. if substate not in self.substates:
  1664. continue
  1665. self.substates[substate]._clean()
  1666. # Clean this state.
  1667. self.dirty_vars = set()
  1668. self.dirty_substates = set()
  1669. def get_value(self, key: str) -> Any:
  1670. """Get the value of a field (without proxying).
  1671. The returned value will NOT track dirty state updates.
  1672. Args:
  1673. key: The key of the field.
  1674. Returns:
  1675. The value of the field.
  1676. """
  1677. value = super().get_value(key)
  1678. if isinstance(value, MutableProxy):
  1679. return value.__wrapped__
  1680. return value
  1681. def dict(
  1682. self, include_computed: bool = True, initial: bool = False, **kwargs
  1683. ) -> dict[str, Any]:
  1684. """Convert the object to a dictionary.
  1685. Args:
  1686. include_computed: Whether to include computed vars.
  1687. initial: Whether to get the initial value of computed vars.
  1688. **kwargs: Kwargs to pass to the pydantic dict method.
  1689. Returns:
  1690. The object as a dictionary.
  1691. """
  1692. if include_computed:
  1693. self._mark_dirty_computed_vars()
  1694. base_vars = {
  1695. prop_name: self.get_value(prop_name) for prop_name in self.base_vars
  1696. }
  1697. if initial and include_computed:
  1698. computed_vars = {
  1699. # Include initial computed vars.
  1700. prop_name: (
  1701. cv._initial_value
  1702. if is_computed_var(cv)
  1703. and not isinstance(cv._initial_value, types.Unset)
  1704. else self.get_value(prop_name)
  1705. )
  1706. for prop_name, cv in self.computed_vars.items()
  1707. if not cv._backend
  1708. }
  1709. elif include_computed:
  1710. computed_vars = {
  1711. # Include the computed vars.
  1712. prop_name: self.get_value(prop_name)
  1713. for prop_name, cv in self.computed_vars.items()
  1714. if not cv._backend
  1715. }
  1716. else:
  1717. computed_vars = {}
  1718. variables = {**base_vars, **computed_vars}
  1719. d = {
  1720. self.get_full_name(): {
  1721. k + "_rx_state_": variables[k] for k in sorted(variables)
  1722. },
  1723. }
  1724. for substate_d in [
  1725. v.dict(include_computed=include_computed, initial=initial, **kwargs)
  1726. for v in self.substates.values()
  1727. ]:
  1728. d.update(substate_d)
  1729. return d
  1730. async def __aenter__(self) -> BaseState:
  1731. """Enter the async context manager protocol.
  1732. This should not be used for the State class, but exists for
  1733. type-compatibility with StateProxy.
  1734. Raises:
  1735. TypeError: always, because async contextmanager protocol is only supported for background task.
  1736. """
  1737. raise TypeError(
  1738. "Only background task should use `async with self` to modify state."
  1739. )
  1740. async def __aexit__(self, *exc_info: Any) -> None:
  1741. """Exit the async context manager protocol.
  1742. This should not be used for the State class, but exists for
  1743. type-compatibility with StateProxy.
  1744. Args:
  1745. exc_info: The exception info tuple.
  1746. """
  1747. pass
  1748. def __getstate__(self):
  1749. """Get the state for redis serialization.
  1750. This method is called by pickle to serialize the object.
  1751. It explicitly removes parent_state and substates because those are serialized separately
  1752. by the StateManagerRedis to allow for better horizontal scaling as state size increases.
  1753. Returns:
  1754. The state dict for serialization.
  1755. """
  1756. state = super().__getstate__()
  1757. state["__dict__"] = state["__dict__"].copy()
  1758. if state["__dict__"].get("parent_state") is not None:
  1759. # Do not serialize router data in substates (only the root state).
  1760. state["__dict__"].pop("router", None)
  1761. state["__dict__"].pop("router_data", None)
  1762. # Never serialize parent_state or substates.
  1763. state["__dict__"].pop("parent_state", None)
  1764. state["__dict__"].pop("substates", None)
  1765. state["__dict__"].pop("_was_touched", None)
  1766. # Remove all inherited vars.
  1767. for inherited_var_name in self.inherited_vars:
  1768. state["__dict__"].pop(inherited_var_name, None)
  1769. return state
  1770. def __setstate__(self, state: dict[str, Any]):
  1771. """Set the state from redis deserialization.
  1772. This method is called by pickle to deserialize the object.
  1773. Args:
  1774. state: The state dict for deserialization.
  1775. """
  1776. state["__dict__"]["parent_state"] = None
  1777. state["__dict__"]["substates"] = {}
  1778. super().__setstate__(state)
  1779. def _check_state_size(
  1780. self,
  1781. pickle_state_size: int,
  1782. ):
  1783. """Print a warning when the state is too large.
  1784. Args:
  1785. pickle_state_size: The size of the pickled state.
  1786. Raises:
  1787. StateTooLargeError: If the state is too large.
  1788. """
  1789. state_full_name = self.get_full_name()
  1790. if (
  1791. state_full_name not in _WARNED_ABOUT_STATE_SIZE
  1792. and pickle_state_size > TOO_LARGE_SERIALIZED_STATE
  1793. and self.substates
  1794. ):
  1795. msg = (
  1796. f"State {state_full_name} serializes to {pickle_state_size} bytes "
  1797. + "which may present performance issues. Consider reducing the size of this state."
  1798. )
  1799. if environment.REFLEX_PERF_MODE.get() == PerformanceMode.WARN:
  1800. console.warn(msg)
  1801. elif environment.REFLEX_PERF_MODE.get() == PerformanceMode.RAISE:
  1802. raise StateTooLargeError(msg)
  1803. _WARNED_ABOUT_STATE_SIZE.add(state_full_name)
  1804. @classmethod
  1805. @functools.lru_cache
  1806. def _to_schema(cls) -> str:
  1807. """Convert a state to a schema.
  1808. Returns:
  1809. The hash of the schema.
  1810. """
  1811. def _field_tuple(
  1812. field_name: str,
  1813. ) -> tuple[str, str, Any, bool | None, Any]:
  1814. model_field = cls.__fields__[field_name]
  1815. return (
  1816. field_name,
  1817. model_field.name,
  1818. _serialize_type(model_field.type_),
  1819. (
  1820. model_field.required
  1821. if isinstance(model_field.required, bool)
  1822. else None
  1823. ),
  1824. (model_field.default if is_serializable(model_field.default) else None),
  1825. )
  1826. return md5(
  1827. pickle.dumps(
  1828. sorted(_field_tuple(field_name) for field_name in cls.base_vars)
  1829. )
  1830. ).hexdigest()
  1831. def _serialize(self) -> bytes:
  1832. """Serialize the state for redis.
  1833. Returns:
  1834. The serialized state.
  1835. Raises:
  1836. StateSerializationError: If the state cannot be serialized.
  1837. """
  1838. payload = b""
  1839. error = ""
  1840. try:
  1841. payload = pickle.dumps((self._to_schema(), self))
  1842. except HANDLED_PICKLE_ERRORS as og_pickle_error:
  1843. error = (
  1844. f"Failed to serialize state {self.get_full_name()} due to unpicklable object. "
  1845. "This state will not be persisted. "
  1846. )
  1847. try:
  1848. import dill
  1849. payload = dill.dumps((self._to_schema(), self))
  1850. except ImportError:
  1851. error += (
  1852. f"Pickle error: {og_pickle_error}. "
  1853. "Consider `pip install 'dill>=0.3.8'` for more exotic serialization support."
  1854. )
  1855. except HANDLED_PICKLE_ERRORS as ex:
  1856. error += f"Dill was also unable to pickle the state: {ex}"
  1857. console.warn(error)
  1858. if environment.REFLEX_PERF_MODE.get() != PerformanceMode.OFF:
  1859. self._check_state_size(len(payload))
  1860. if not payload:
  1861. raise StateSerializationError(error)
  1862. return payload
  1863. @classmethod
  1864. def _deserialize(
  1865. cls, data: bytes | None = None, fp: BinaryIO | None = None
  1866. ) -> BaseState:
  1867. """Deserialize the state from redis/disk.
  1868. data and fp are mutually exclusive, but one must be provided.
  1869. Args:
  1870. data: The serialized state data.
  1871. fp: The file pointer to the serialized state data.
  1872. Returns:
  1873. The deserialized state.
  1874. Raises:
  1875. ValueError: If both data and fp are provided, or neither are provided.
  1876. StateSchemaMismatchError: If the state schema does not match the expected schema.
  1877. """
  1878. if data is not None and fp is None:
  1879. (substate_schema, state) = pickle.loads(data)
  1880. elif fp is not None and data is None:
  1881. (substate_schema, state) = pickle.load(fp)
  1882. else:
  1883. raise ValueError("Only one of `data` or `fp` must be provided")
  1884. if substate_schema != state._to_schema():
  1885. raise StateSchemaMismatchError()
  1886. return state
  1887. def _serialize_type(type_: Any) -> str:
  1888. """Serialize a type.
  1889. Args:
  1890. type_: The type to serialize.
  1891. Returns:
  1892. The serialized type.
  1893. """
  1894. if not inspect.isclass(type_):
  1895. return f"{type_}"
  1896. return f"{type_.__module__}.{type_.__qualname__}"
  1897. def is_serializable(value: Any) -> bool:
  1898. """Check if a value is serializable.
  1899. Args:
  1900. value: The value to check.
  1901. Returns:
  1902. Whether the value is serializable.
  1903. """
  1904. try:
  1905. return bool(pickle.dumps(value))
  1906. except Exception:
  1907. return False
  1908. T_STATE = TypeVar("T_STATE", bound=BaseState)
  1909. class State(BaseState):
  1910. """The app Base State."""
  1911. # The hydrated bool.
  1912. is_hydrated: bool = False
  1913. T = TypeVar("T", bound=BaseState)
  1914. def dynamic(func: Callable[[T], Component]):
  1915. """Create a dynamically generated components from a state class.
  1916. Args:
  1917. func: The function to generate the component.
  1918. Returns:
  1919. The dynamically generated component.
  1920. Raises:
  1921. DynamicComponentInvalidSignatureError: If the function does not have exactly one parameter or a type hint for the state class.
  1922. """
  1923. number_of_parameters = len(inspect.signature(func).parameters)
  1924. func_signature = get_type_hints(func)
  1925. if "return" in func_signature:
  1926. func_signature.pop("return")
  1927. values = list(func_signature.values())
  1928. if number_of_parameters != 1:
  1929. raise DynamicComponentInvalidSignatureError(
  1930. "The function must have exactly one parameter, which is the state class."
  1931. )
  1932. if len(values) != 1:
  1933. raise DynamicComponentInvalidSignatureError(
  1934. "You must provide a type hint for the state class in the function."
  1935. )
  1936. state_class: type[T] = values[0]
  1937. def wrapper() -> Component:
  1938. from reflex.components.base.fragment import fragment
  1939. return fragment(state_class._evaluate(lambda state: func(state)))
  1940. return wrapper
  1941. class FrontendEventExceptionState(State):
  1942. """Substate for handling frontend exceptions."""
  1943. @event
  1944. def handle_frontend_exception(self, stack: str, component_stack: str) -> None:
  1945. """Handle frontend exceptions.
  1946. If a frontend exception handler is provided, it will be called.
  1947. Otherwise, the default frontend exception handler will be called.
  1948. Args:
  1949. stack: The stack trace of the exception.
  1950. component_stack: The stack trace of the component where the exception occurred.
  1951. """
  1952. prerequisites.get_and_validate_app().app.frontend_exception_handler(
  1953. Exception(stack)
  1954. )
  1955. class UpdateVarsInternalState(State):
  1956. """Substate for handling internal state var updates."""
  1957. async def update_vars_internal(self, vars: dict[str, Any]) -> None:
  1958. """Apply updates to fully qualified state vars.
  1959. The keys in `vars` should be in the form of `{state.get_full_name()}.{var_name}`,
  1960. and each value will be set on the appropriate substate instance.
  1961. This function is primarily used to apply cookie and local storage
  1962. updates from the frontend to the appropriate substate.
  1963. Args:
  1964. vars: The fully qualified vars and values to update.
  1965. """
  1966. for var, value in vars.items():
  1967. state_name, _, var_name = var.rpartition(".")
  1968. var_state_cls = State.get_class_substate(state_name)
  1969. var_state = await self.get_state(var_state_cls)
  1970. setattr(var_state, var_name, value)
  1971. class OnLoadInternalState(State):
  1972. """Substate for handling on_load event enumeration.
  1973. This is a separate substate to avoid deserializing the entire state tree for every page navigation.
  1974. """
  1975. def on_load_internal(self) -> list[Event | EventSpec] | None:
  1976. """Queue on_load handlers for the current page.
  1977. Returns:
  1978. The list of events to queue for on load handling.
  1979. """
  1980. # Do not app._compile()! It should be already compiled by now.
  1981. load_events = prerequisites.get_and_validate_app().app.get_load_events(
  1982. self.router.page.path
  1983. )
  1984. if not load_events:
  1985. self.is_hydrated = True
  1986. return # Fast path for navigation with no on_load events defined.
  1987. self.is_hydrated = False
  1988. return [
  1989. *fix_events(
  1990. cast(list[EventSpec | EventHandler], load_events),
  1991. self.router.session.client_token,
  1992. router_data=self.router_data,
  1993. ),
  1994. State.set_is_hydrated(True), # pyright: ignore [reportAttributeAccessIssue]
  1995. ]
  1996. class ComponentState(State, mixin=True):
  1997. """Base class to allow for the creation of a state instance per component.
  1998. This allows for the bundling of UI and state logic into a single class,
  1999. where each instance has a separate instance of the state.
  2000. Subclass this class and define vars and event handlers in the traditional way.
  2001. Then define a `get_component` method that returns the UI for the component instance.
  2002. See the full [docs](https://reflex.dev/docs/substates/component-state/) for more.
  2003. Basic example:
  2004. ```python
  2005. # Subclass ComponentState and define vars and event handlers.
  2006. class Counter(rx.ComponentState):
  2007. # Define vars that change.
  2008. count: int = 0
  2009. # Define event handlers.
  2010. def increment(self):
  2011. self.count += 1
  2012. def decrement(self):
  2013. self.count -= 1
  2014. @classmethod
  2015. def get_component(cls, **props):
  2016. # Access the state vars and event handlers using `cls`.
  2017. return rx.hstack(
  2018. rx.button("Decrement", on_click=cls.decrement),
  2019. rx.text(cls.count),
  2020. rx.button("Increment", on_click=cls.increment),
  2021. **props,
  2022. )
  2023. counter = Counter.create()
  2024. ```
  2025. """
  2026. # The number of components created from this class.
  2027. _per_component_state_instance_count: ClassVar[int] = 0
  2028. def __init__(self, *args, **kwargs):
  2029. """Do not allow direct initialization of the ComponentState.
  2030. Args:
  2031. *args: The args to pass to the State init method.
  2032. **kwargs: The kwargs to pass to the State init method.
  2033. Raises:
  2034. ReflexRuntimeError: If the ComponentState is initialized directly.
  2035. """
  2036. if type(self)._mixin:
  2037. raise ReflexRuntimeError(
  2038. f"{ComponentState.__name__} {type(self).__name__} is not meant to be initialized directly. "
  2039. + "Use the `create` method to create a new instance and access the state via the `State` attribute."
  2040. )
  2041. super().__init__(*args, **kwargs)
  2042. @classmethod
  2043. def __init_subclass__(cls, mixin: bool = True, **kwargs):
  2044. """Overwrite mixin default to True.
  2045. Args:
  2046. mixin: Whether the subclass is a mixin and should not be initialized.
  2047. **kwargs: The kwargs to pass to the pydantic init_subclass method.
  2048. """
  2049. super().__init_subclass__(mixin=mixin, **kwargs)
  2050. @classmethod
  2051. def get_component(cls, *children, **props) -> Component:
  2052. """Get the component instance.
  2053. Args:
  2054. children: The children of the component.
  2055. props: The props of the component.
  2056. Raises:
  2057. NotImplementedError: if the subclass does not override this method.
  2058. """
  2059. raise NotImplementedError(
  2060. f"{cls.__name__} must implement get_component to return the component instance."
  2061. )
  2062. @classmethod
  2063. def create(cls, *children, **props) -> Component:
  2064. """Create a new instance of the Component.
  2065. Args:
  2066. children: The children of the component.
  2067. props: The props of the component.
  2068. Returns:
  2069. A new instance of the Component with an independent copy of the State.
  2070. """
  2071. from reflex.compiler.compiler import into_component
  2072. cls._per_component_state_instance_count += 1
  2073. state_cls_name = f"{cls.__name__}_n{cls._per_component_state_instance_count}"
  2074. component_state = type(
  2075. state_cls_name,
  2076. (cls, State),
  2077. {"__module__": reflex.istate.dynamic.__name__},
  2078. mixin=False,
  2079. )
  2080. # Save a reference to the dynamic state for pickle/unpickle.
  2081. setattr(reflex.istate.dynamic, state_cls_name, component_state)
  2082. component = component_state.get_component(*children, **props)
  2083. component = into_component(component)
  2084. component.State = component_state
  2085. return component
  2086. @dataclasses.dataclass(
  2087. frozen=True,
  2088. )
  2089. class StateUpdate:
  2090. """A state update sent to the frontend."""
  2091. # The state delta.
  2092. delta: Delta = dataclasses.field(default_factory=dict)
  2093. # Events to be added to the event queue.
  2094. events: list[Event] = dataclasses.field(default_factory=list)
  2095. # Whether this is the final state update for the event.
  2096. final: bool = True
  2097. def json(self) -> str:
  2098. """Convert the state update to a JSON string.
  2099. Returns:
  2100. The state update as a JSON string.
  2101. """
  2102. return format.json_dumps(self)
  2103. def code_uses_state_contexts(javascript_code: str) -> bool:
  2104. """Check if the rendered Javascript uses state contexts.
  2105. Args:
  2106. javascript_code: The Javascript code to check.
  2107. Returns:
  2108. True if the code attempts to access a member of StateContexts.
  2109. """
  2110. return bool("useContext(StateContexts" in javascript_code)
  2111. def reload_state_module(
  2112. module: str,
  2113. state: type[BaseState] = State,
  2114. ) -> None:
  2115. """Reset rx.State subclasses to avoid conflict when reloading.
  2116. Args:
  2117. module: The module to reload.
  2118. state: Recursive argument for the state class to reload.
  2119. """
  2120. # Clean out all potentially dirty states of reloaded modules.
  2121. for pd_state in tuple(state._potentially_dirty_states):
  2122. with contextlib.suppress(ValueError):
  2123. if (
  2124. state.get_root_state().get_class_substate(pd_state).__module__ == module
  2125. and module is not None
  2126. ):
  2127. state._potentially_dirty_states.remove(pd_state)
  2128. for subclass in tuple(state.class_subclasses):
  2129. reload_state_module(module=module, state=subclass)
  2130. if subclass.__module__ == module and module is not None:
  2131. all_base_state_classes.pop(subclass.get_full_name(), None)
  2132. state.class_subclasses.remove(subclass)
  2133. state._always_dirty_substates.discard(subclass.get_name())
  2134. state._var_dependencies = {}
  2135. state._init_var_dependency_dicts()
  2136. state.get_class_substate.cache_clear()
  2137. from reflex.istate.manager import LockExpiredError as LockExpiredError # noqa: E402
  2138. from reflex.istate.manager import StateManager as StateManager # noqa: E402
  2139. from reflex.istate.manager import StateManagerDisk as StateManagerDisk # noqa: E402
  2140. from reflex.istate.manager import StateManagerMemory as StateManagerMemory # noqa: E402
  2141. from reflex.istate.manager import StateManagerRedis as StateManagerRedis # noqa: E402
  2142. from reflex.istate.manager import get_state_manager as get_state_manager # noqa: E402
  2143. from reflex.istate.manager import ( # noqa: E402
  2144. reset_disk_state_manager as reset_disk_state_manager,
  2145. )