state.py 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. """Define the reflex state specification."""
  2. from __future__ import annotations
  3. import asyncio
  4. import copy
  5. import functools
  6. import inspect
  7. import json
  8. import traceback
  9. import urllib.parse
  10. from abc import ABC
  11. from collections import defaultdict
  12. from types import FunctionType
  13. from typing import (
  14. Any,
  15. AsyncIterator,
  16. Callable,
  17. ClassVar,
  18. Dict,
  19. List,
  20. Optional,
  21. Sequence,
  22. Set,
  23. Type,
  24. )
  25. import cloudpickle
  26. import pydantic
  27. import wrapt
  28. from redis import Redis
  29. from reflex import constants
  30. from reflex.base import Base
  31. from reflex.event import Event, EventHandler, EventSpec, fix_events, window_alert
  32. from reflex.utils import format, prerequisites, types
  33. from reflex.vars import BaseVar, ComputedVar, Var
  34. Delta = Dict[str, Any]
  35. class State(Base, ABC, extra=pydantic.Extra.allow):
  36. """The state of the app."""
  37. # A map from the var name to the var.
  38. vars: ClassVar[Dict[str, Var]] = {}
  39. # The base vars of the class.
  40. base_vars: ClassVar[Dict[str, BaseVar]] = {}
  41. # The computed vars of the class.
  42. computed_vars: ClassVar[Dict[str, ComputedVar]] = {}
  43. # Vars inherited by the parent state.
  44. inherited_vars: ClassVar[Dict[str, Var]] = {}
  45. # Backend vars that are never sent to the client.
  46. backend_vars: ClassVar[Dict[str, Any]] = {}
  47. # Backend vars inherited
  48. inherited_backend_vars: ClassVar[Dict[str, Any]] = {}
  49. # The event handlers.
  50. event_handlers: ClassVar[Dict[str, EventHandler]] = {}
  51. # The parent state.
  52. parent_state: Optional[State] = None
  53. # The substates of the state.
  54. substates: Dict[str, State] = {}
  55. # The set of dirty vars.
  56. dirty_vars: Set[str] = set()
  57. # The set of dirty substates.
  58. dirty_substates: Set[str] = set()
  59. # The routing path that triggered the state
  60. router_data: Dict[str, Any] = {}
  61. # Mapping of var name to set of computed variables that depend on it
  62. computed_var_dependencies: Dict[str, Set[str]] = {}
  63. # Mapping of var name to set of substates that depend on it
  64. substate_var_dependencies: Dict[str, Set[str]] = {}
  65. # Per-instance copy of backend variable values
  66. _backend_vars: Dict[str, Any] = {}
  67. def __init__(self, *args, parent_state: State | None = None, **kwargs):
  68. """Initialize the state.
  69. Args:
  70. *args: The args to pass to the Pydantic init method.
  71. parent_state: The parent state.
  72. **kwargs: The kwargs to pass to the Pydantic init method.
  73. Raises:
  74. ValueError: If a substate class shadows another.
  75. """
  76. kwargs["parent_state"] = parent_state
  77. super().__init__(*args, **kwargs)
  78. # initialize per-instance var dependency tracking
  79. self.computed_var_dependencies = defaultdict(set)
  80. self.substate_var_dependencies = defaultdict(set)
  81. # Setup the substates.
  82. for substate in self.get_substates():
  83. substate_name = substate.get_name()
  84. if substate_name in self.substates:
  85. raise ValueError(
  86. f"The substate class '{substate_name}' has been defined multiple times. Shadowing "
  87. f"substate classes is not allowed."
  88. )
  89. self.substates[substate_name] = substate(parent_state=self)
  90. # Convert the event handlers to functions.
  91. self._init_event_handlers()
  92. # Initialize computed vars dependencies.
  93. inherited_vars = set(self.inherited_vars).union(
  94. set(self.inherited_backend_vars),
  95. )
  96. for cvar_name, cvar in self.computed_vars.items():
  97. # Add the dependencies.
  98. for var in cvar.deps(objclass=type(self)):
  99. self.computed_var_dependencies[var].add(cvar_name)
  100. if var in inherited_vars:
  101. # track that this substate depends on its parent for this var
  102. state_name = self.get_name()
  103. parent_state = self.parent_state
  104. while parent_state is not None and var in parent_state.vars:
  105. parent_state.substate_var_dependencies[var].add(state_name)
  106. state_name, parent_state = (
  107. parent_state.get_name(),
  108. parent_state.parent_state,
  109. )
  110. # Create a fresh copy of the backend variables for this instance
  111. self._backend_vars = copy.deepcopy(self.backend_vars)
  112. def _init_event_handlers(self, state: State | None = None):
  113. """Initialize event handlers.
  114. Allow event handlers to be called directly on the instance. This is
  115. called recursively for all parent states.
  116. Args:
  117. state: The state to initialize the event handlers on.
  118. """
  119. if state is None:
  120. state = self
  121. # Convert the event handlers to functions.
  122. for name, event_handler in state.event_handlers.items():
  123. fn = functools.partial(event_handler.fn, self)
  124. fn.__module__ = event_handler.fn.__module__ # type: ignore
  125. fn.__qualname__ = event_handler.fn.__qualname__ # type: ignore
  126. setattr(self, name, fn)
  127. # Also allow direct calling of parent state event handlers
  128. if state.parent_state is not None:
  129. self._init_event_handlers(state.parent_state)
  130. def __repr__(self) -> str:
  131. """Get the string representation of the state.
  132. Returns:
  133. The string representation of the state.
  134. """
  135. return f"{self.__class__.__name__}({self.dict()})"
  136. @classmethod
  137. def __init_subclass__(cls, **kwargs):
  138. """Do some magic for the subclass initialization.
  139. Args:
  140. **kwargs: The kwargs to pass to the pydantic init_subclass method.
  141. """
  142. super().__init_subclass__(**kwargs)
  143. # Event handlers should not shadow builtin state methods.
  144. cls._check_overridden_methods()
  145. # Get the parent vars.
  146. parent_state = cls.get_parent_state()
  147. if parent_state is not None:
  148. cls.inherited_vars = parent_state.vars
  149. cls.inherited_backend_vars = parent_state.backend_vars
  150. cls.new_backend_vars = {
  151. name: value
  152. for name, value in cls.__dict__.items()
  153. if types.is_backend_variable(name)
  154. and name not in cls.inherited_backend_vars
  155. and not isinstance(value, FunctionType)
  156. }
  157. cls.backend_vars = {**cls.inherited_backend_vars, **cls.new_backend_vars}
  158. # Set the base and computed vars.
  159. cls.base_vars = {
  160. f.name: BaseVar(name=f.name, type_=f.outer_type_).set_state(cls)
  161. for f in cls.get_fields().values()
  162. if f.name not in cls.get_skip_vars()
  163. }
  164. cls.computed_vars = {
  165. v.name: v.set_state(cls)
  166. for v in cls.__dict__.values()
  167. if isinstance(v, ComputedVar)
  168. }
  169. cls.vars = {
  170. **cls.inherited_vars,
  171. **cls.base_vars,
  172. **cls.computed_vars,
  173. }
  174. cls.event_handlers = {}
  175. # Setup the base vars at the class level.
  176. for prop in cls.base_vars.values():
  177. cls._init_var(prop)
  178. # Set up the event handlers.
  179. events = {
  180. name: fn
  181. for name, fn in cls.__dict__.items()
  182. if not name.startswith("_")
  183. and isinstance(fn, Callable)
  184. and not isinstance(fn, EventHandler)
  185. }
  186. for name, fn in events.items():
  187. handler = EventHandler(fn=fn)
  188. cls.event_handlers[name] = handler
  189. setattr(cls, name, handler)
  190. @classmethod
  191. def _check_overridden_methods(cls):
  192. """Check for shadow methods and raise error if any.
  193. Raises:
  194. NameError: When an event handler shadows an inbuilt state method.
  195. """
  196. overridden_methods = set()
  197. state_base_functions = cls._get_base_functions()
  198. for name, method in inspect.getmembers(cls, inspect.isfunction):
  199. # Check if the method is overridden and not a dunder method
  200. if (
  201. not name.startswith("__")
  202. and method.__name__ in state_base_functions
  203. and state_base_functions[method.__name__] != method
  204. ):
  205. overridden_methods.add(method.__name__)
  206. for method_name in overridden_methods:
  207. raise NameError(
  208. f"The event handler name `{method_name}` shadows a builtin State method; use a different name instead"
  209. )
  210. @classmethod
  211. def get_skip_vars(cls) -> set[str]:
  212. """Get the vars to skip when serializing.
  213. Returns:
  214. The vars to skip when serializing.
  215. """
  216. return set(cls.inherited_vars) | {
  217. "parent_state",
  218. "substates",
  219. "dirty_vars",
  220. "dirty_substates",
  221. "router_data",
  222. "computed_var_dependencies",
  223. "substate_var_dependencies",
  224. "_backend_vars",
  225. }
  226. @classmethod
  227. @functools.lru_cache()
  228. def get_parent_state(cls) -> Type[State] | None:
  229. """Get the parent state.
  230. Returns:
  231. The parent state.
  232. """
  233. parent_states = [
  234. base
  235. for base in cls.__bases__
  236. if types._issubclass(base, State) and base is not State
  237. ]
  238. assert len(parent_states) < 2, "Only one parent state is allowed."
  239. return parent_states[0] if len(parent_states) == 1 else None # type: ignore
  240. @classmethod
  241. @functools.lru_cache()
  242. def get_substates(cls) -> set[Type[State]]:
  243. """Get the substates of the state.
  244. Returns:
  245. The substates of the state.
  246. """
  247. return set(cls.__subclasses__())
  248. @classmethod
  249. @functools.lru_cache()
  250. def get_name(cls) -> str:
  251. """Get the name of the state.
  252. Returns:
  253. The name of the state.
  254. """
  255. return format.to_snake_case(cls.__name__)
  256. @classmethod
  257. @functools.lru_cache()
  258. def get_full_name(cls) -> str:
  259. """Get the full name of the state.
  260. Returns:
  261. The full name of the state.
  262. """
  263. name = cls.get_name()
  264. parent_state = cls.get_parent_state()
  265. if parent_state is not None:
  266. name = ".".join((parent_state.get_full_name(), name))
  267. return name
  268. @classmethod
  269. @functools.lru_cache()
  270. def get_class_substate(cls, path: Sequence[str]) -> Type[State]:
  271. """Get the class substate.
  272. Args:
  273. path: The path to the substate.
  274. Returns:
  275. The class substate.
  276. Raises:
  277. ValueError: If the substate is not found.
  278. """
  279. if len(path) == 0:
  280. return cls
  281. if path[0] == cls.get_name():
  282. if len(path) == 1:
  283. return cls
  284. path = path[1:]
  285. for substate in cls.get_substates():
  286. if path[0] == substate.get_name():
  287. return substate.get_class_substate(path[1:])
  288. raise ValueError(f"Invalid path: {path}")
  289. @classmethod
  290. def get_class_var(cls, path: Sequence[str]) -> Any:
  291. """Get the class var.
  292. Args:
  293. path: The path to the var.
  294. Returns:
  295. The class var.
  296. Raises:
  297. ValueError: If the path is invalid.
  298. """
  299. path, name = path[:-1], path[-1]
  300. substate = cls.get_class_substate(tuple(path))
  301. if not hasattr(substate, name):
  302. raise ValueError(f"Invalid path: {path}")
  303. return getattr(substate, name)
  304. @classmethod
  305. def _init_var(cls, prop: BaseVar):
  306. """Initialize a variable.
  307. Args:
  308. prop: The variable to initialize
  309. Raises:
  310. TypeError: if the variable has an incorrect type
  311. """
  312. if not types.is_valid_var_type(prop.type_):
  313. raise TypeError(
  314. "State vars must be primitive Python types, "
  315. "Plotly figures, Pandas dataframes, "
  316. "or subclasses of rx.Base. "
  317. f'Found var "{prop.name}" with type {prop.type_}.'
  318. )
  319. cls._set_var(prop)
  320. cls._create_setter(prop)
  321. cls._set_default_value(prop)
  322. @classmethod
  323. def add_var(cls, name: str, type_: Any, default_value: Any = None):
  324. """Add dynamically a variable to the State.
  325. The variable added this way can be used in the same way as a variable
  326. defined statically in the model.
  327. Args:
  328. name: The name of the variable
  329. type_: The type of the variable
  330. default_value: The default value of the variable
  331. Raises:
  332. NameError: if a variable of this name already exists
  333. """
  334. if name in cls.__fields__:
  335. raise NameError(
  336. f"The variable '{name}' already exist. Use a different name"
  337. )
  338. # create the variable based on name and type
  339. var = BaseVar(name=name, type_=type_)
  340. var.set_state(cls)
  341. # add the pydantic field dynamically (must be done before _init_var)
  342. cls.add_field(var, default_value)
  343. cls._init_var(var)
  344. # update the internal dicts so the new variable is correctly handled
  345. cls.base_vars.update({name: var})
  346. cls.vars.update({name: var})
  347. # let substates know about the new variable
  348. for substate_class in cls.__subclasses__():
  349. substate_class.vars.setdefault(name, var)
  350. @classmethod
  351. def _set_var(cls, prop: BaseVar):
  352. """Set the var as a class member.
  353. Args:
  354. prop: The var instance to set.
  355. """
  356. setattr(cls, prop.name, prop)
  357. @classmethod
  358. def _create_setter(cls, prop: BaseVar):
  359. """Create a setter for the var.
  360. Args:
  361. prop: The var to create a setter for.
  362. """
  363. setter_name = prop.get_setter_name(include_state=False)
  364. if setter_name not in cls.__dict__:
  365. event_handler = EventHandler(fn=prop.get_setter())
  366. cls.event_handlers[setter_name] = event_handler
  367. setattr(cls, setter_name, event_handler)
  368. @classmethod
  369. def _set_default_value(cls, prop: BaseVar):
  370. """Set the default value for the var.
  371. Args:
  372. prop: The var to set the default value for.
  373. """
  374. # Get the pydantic field for the var.
  375. field = cls.get_fields()[prop.name]
  376. default_value = prop.get_default_value()
  377. if field.required and default_value is not None:
  378. field.required = False
  379. field.default = default_value
  380. @staticmethod
  381. def _get_base_functions() -> dict[str, FunctionType]:
  382. """Get all functions of the state class excluding dunder methods.
  383. Returns:
  384. The functions of rx.State class as a dict.
  385. """
  386. return {
  387. func[0]: func[1]
  388. for func in inspect.getmembers(State, predicate=inspect.isfunction)
  389. if not func[0].startswith("__")
  390. }
  391. def get_token(self) -> str:
  392. """Return the token of the client associated with this state.
  393. Returns:
  394. The token of the client.
  395. """
  396. return self.router_data.get(constants.RouteVar.CLIENT_TOKEN, "")
  397. def get_sid(self) -> str:
  398. """Return the session ID of the client associated with this state.
  399. Returns:
  400. The session ID of the client.
  401. """
  402. return self.router_data.get(constants.RouteVar.SESSION_ID, "")
  403. def get_headers(self) -> Dict:
  404. """Return the headers of the client associated with this state.
  405. Returns:
  406. The headers of the client.
  407. """
  408. return self.router_data.get(constants.RouteVar.HEADERS, {})
  409. def get_client_ip(self) -> str:
  410. """Return the IP of the client associated with this state.
  411. Returns:
  412. The IP of the client.
  413. """
  414. return self.router_data.get(constants.RouteVar.CLIENT_IP, "")
  415. def get_current_page(self, origin=False) -> str:
  416. """Obtain the path of current page from the router data.
  417. Args:
  418. origin: whether to return the base route as shown in browser
  419. Returns:
  420. The current page.
  421. """
  422. if origin:
  423. return self.router_data.get(constants.RouteVar.ORIGIN, "")
  424. else:
  425. return self.router_data.get(constants.RouteVar.PATH, "")
  426. def get_query_params(self) -> dict[str, str]:
  427. """Obtain the query parameters for the queried page.
  428. The query object contains both the URI parameters and the GET parameters.
  429. Returns:
  430. The dict of query parameters.
  431. """
  432. return self.router_data.get(constants.RouteVar.QUERY, {})
  433. def get_cookies(self) -> dict[str, str]:
  434. """Obtain the cookies of the client stored in the browser.
  435. Returns:
  436. The dict of cookies.
  437. """
  438. cookie_dict = {}
  439. cookies = self.get_headers().get(constants.RouteVar.COOKIE, "").split(";")
  440. cookie_pairs = [cookie.split("=") for cookie in cookies if cookie]
  441. for pair in cookie_pairs:
  442. key, value = pair[0].strip(), urllib.parse.unquote(pair[1].strip())
  443. try:
  444. # cast non-string values to the actual types.
  445. value = json.loads(value)
  446. except json.JSONDecodeError:
  447. pass
  448. finally:
  449. cookie_dict[key] = value
  450. return cookie_dict
  451. @classmethod
  452. def setup_dynamic_args(cls, args: dict[str, str]):
  453. """Set up args for easy access in renderer.
  454. Args:
  455. args: a dict of args
  456. """
  457. def argsingle_factory(param):
  458. @ComputedVar
  459. def inner_func(self) -> str:
  460. return self.get_query_params().get(param, "")
  461. return inner_func
  462. def arglist_factory(param):
  463. @ComputedVar
  464. def inner_func(self) -> List:
  465. return self.get_query_params().get(param, [])
  466. return inner_func
  467. for param, value in args.items():
  468. if value == constants.RouteArgType.SINGLE:
  469. func = argsingle_factory(param)
  470. elif value == constants.RouteArgType.LIST:
  471. func = arglist_factory(param)
  472. else:
  473. continue
  474. func.fget.__name__ = param # to allow passing as a prop # type: ignore
  475. cls.vars[param] = cls.computed_vars[param] = func.set_state(cls) # type: ignore
  476. setattr(cls, param, func)
  477. def __getattribute__(self, name: str) -> Any:
  478. """Get the state var.
  479. If the var is inherited, get the var from the parent state.
  480. Args:
  481. name: The name of the var.
  482. Returns:
  483. The value of the var.
  484. """
  485. # If the state hasn't been initialized yet, return the default value.
  486. if not super().__getattribute__("__dict__"):
  487. return super().__getattribute__(name)
  488. inherited_vars = {
  489. **super().__getattribute__("inherited_vars"),
  490. **super().__getattribute__("inherited_backend_vars"),
  491. }
  492. if name in inherited_vars:
  493. return getattr(super().__getattribute__("parent_state"), name)
  494. backend_vars = super().__getattribute__("_backend_vars")
  495. if name in backend_vars:
  496. value = backend_vars[name]
  497. else:
  498. value = super().__getattribute__(name)
  499. if isinstance(value, MutableProxy.__mutable_types__) and (
  500. name in super().__getattribute__("base_vars") or name in backend_vars
  501. ):
  502. # track changes in mutable containers (list, dict, set, etc)
  503. return MutableProxy(wrapped=value, state=self, field_name=name)
  504. return value
  505. def __setattr__(self, name: str, value: Any):
  506. """Set the attribute.
  507. If the attribute is inherited, set the attribute on the parent state.
  508. Args:
  509. name: The name of the attribute.
  510. value: The value of the attribute.
  511. """
  512. if isinstance(value, MutableProxy):
  513. # unwrap proxy objects when assigning back to the state
  514. value = value.__wrapped__
  515. # Set the var on the parent state.
  516. inherited_vars = {**self.inherited_vars, **self.inherited_backend_vars}
  517. if name in inherited_vars:
  518. setattr(self.parent_state, name, value)
  519. return
  520. if types.is_backend_variable(name) and name != "_backend_vars":
  521. self._backend_vars.__setitem__(name, value)
  522. self.dirty_vars.add(name)
  523. self._mark_dirty()
  524. return
  525. # Set the attribute.
  526. super().__setattr__(name, value)
  527. # Add the var to the dirty list.
  528. if name in self.vars or name in self.computed_var_dependencies:
  529. self.dirty_vars.add(name)
  530. self._mark_dirty()
  531. # For now, handle router_data updates as a special case
  532. if name == constants.ROUTER_DATA:
  533. self.dirty_vars.add(name)
  534. self._mark_dirty()
  535. # propagate router_data updates down the state tree
  536. for substate in self.substates.values():
  537. setattr(substate, name, value)
  538. def reset(self):
  539. """Reset all the base vars to their default values."""
  540. # Reset the base vars.
  541. fields = self.get_fields()
  542. for prop_name in self.base_vars:
  543. setattr(self, prop_name, fields[prop_name].default)
  544. # Recursively reset the substates.
  545. for substate in self.substates.values():
  546. substate.reset()
  547. def _reset_client_storage(self):
  548. """Reset client storage base vars to their default values."""
  549. # Client-side storage is reset during hydrate so that clearing cookies
  550. # on the browser also resets the values on the backend.
  551. fields = self.get_fields()
  552. for prop_name in self.base_vars:
  553. field = fields[prop_name]
  554. if isinstance(field.default, ClientStorageBase) or (
  555. isinstance(field.type_, type)
  556. and issubclass(field.type_, ClientStorageBase)
  557. ):
  558. setattr(self, prop_name, field.default)
  559. # Recursively reset the substate client storage.
  560. for substate in self.substates.values():
  561. substate._reset_client_storage()
  562. def get_substate(self, path: Sequence[str]) -> State | None:
  563. """Get the substate.
  564. Args:
  565. path: The path to the substate.
  566. Returns:
  567. The substate.
  568. Raises:
  569. ValueError: If the substate is not found.
  570. """
  571. if len(path) == 0:
  572. return self
  573. if path[0] == self.get_name():
  574. if len(path) == 1:
  575. return self
  576. path = path[1:]
  577. if path[0] not in self.substates:
  578. raise ValueError(f"Invalid path: {path}")
  579. return self.substates[path[0]].get_substate(path[1:])
  580. async def _process(self, event: Event) -> AsyncIterator[StateUpdate]:
  581. """Obtain event info and process event.
  582. Args:
  583. event: The event to process.
  584. Yields:
  585. The state update after processing the event.
  586. Raises:
  587. ValueError: If the state value is None.
  588. """
  589. # Get the event handler.
  590. path = event.name.split(".")
  591. path, name = path[:-1], path[-1]
  592. substate = self.get_substate(path)
  593. handler = substate.event_handlers[name] # type: ignore
  594. if not substate:
  595. raise ValueError(
  596. "The value of state cannot be None when processing an event."
  597. )
  598. # Get the event generator.
  599. event_iter = self._process_event(
  600. handler=handler,
  601. state=substate,
  602. payload=event.payload,
  603. )
  604. # Clean the state before processing the event.
  605. self._clean()
  606. # Run the event generator and return state updates.
  607. async for events, final in event_iter:
  608. # Fix the returned events.
  609. events = fix_events(events, event.token) # type: ignore
  610. # Get the delta after processing the event.
  611. delta = self.get_delta()
  612. # Yield the state update.
  613. yield StateUpdate(delta=delta, events=events, final=final)
  614. # Clean the state to prepare for the next event.
  615. self._clean()
  616. def _check_valid(self, handler: EventHandler, events: Any) -> Any:
  617. """Check if the events yielded are valid. They must be EventHandlers or EventSpecs.
  618. Args:
  619. handler: EventHandler.
  620. events: The events to be checked.
  621. Raises:
  622. TypeError: If any of the events are not valid.
  623. Returns:
  624. The events as they are if valid.
  625. """
  626. def _is_valid_type(events: Any) -> bool:
  627. return isinstance(events, (EventHandler, EventSpec))
  628. if events is None or _is_valid_type(events):
  629. return events
  630. try:
  631. if all(_is_valid_type(e) for e in events):
  632. return events
  633. except TypeError:
  634. pass
  635. raise TypeError(
  636. f"Your handler {handler.fn.__qualname__} must only return/yield: None, Events or other EventHandlers referenced by their class (not using `self`)"
  637. )
  638. async def _process_event(
  639. self, handler: EventHandler, state: State, payload: Dict
  640. ) -> AsyncIterator[tuple[list[EventSpec] | None, bool]]:
  641. """Process event.
  642. Args:
  643. handler: EventHandler to process.
  644. state: State to process the handler.
  645. payload: The event payload.
  646. Yields:
  647. Tuple containing:
  648. 0: The state update after processing the event.
  649. 1: Whether the event is the final event.
  650. """
  651. # Get the function to process the event.
  652. fn = functools.partial(handler.fn, state)
  653. # Wrap the function in a try/except block.
  654. try:
  655. # Handle async functions.
  656. if asyncio.iscoroutinefunction(fn.func):
  657. events = await fn(**payload)
  658. # Handle regular functions.
  659. else:
  660. events = fn(**payload)
  661. # Handle async generators.
  662. if inspect.isasyncgen(events):
  663. async for event in events:
  664. yield self._check_valid(handler, event), False
  665. yield None, True
  666. # Handle regular generators.
  667. elif inspect.isgenerator(events):
  668. try:
  669. while True:
  670. yield self._check_valid(handler, next(events)), False
  671. except StopIteration as si:
  672. # the "return" value of the generator is not available
  673. # in the loop, we must catch StopIteration to access it
  674. if si.value is not None:
  675. yield self._check_valid(handler, si.value), False
  676. yield None, True
  677. # Handle regular event chains.
  678. else:
  679. yield self._check_valid(handler, events), True
  680. # If an error occurs, throw a window alert.
  681. except Exception:
  682. error = traceback.format_exc()
  683. print(error)
  684. yield [window_alert("An error occurred. See logs for details.")], True
  685. def _always_dirty_computed_vars(self) -> set[str]:
  686. """The set of ComputedVars that always need to be recalculated.
  687. Returns:
  688. Set of all ComputedVar in this state where cache=False
  689. """
  690. return set(
  691. cvar_name
  692. for cvar_name, cvar in self.computed_vars.items()
  693. if not cvar.cache
  694. )
  695. def _mark_dirty_computed_vars(self) -> None:
  696. """Mark ComputedVars that need to be recalculated based on dirty_vars."""
  697. dirty_vars = self.dirty_vars
  698. while dirty_vars:
  699. calc_vars, dirty_vars = dirty_vars, set()
  700. for cvar in self._dirty_computed_vars(from_vars=calc_vars):
  701. self.dirty_vars.add(cvar)
  702. dirty_vars.add(cvar)
  703. actual_var = self.computed_vars.get(cvar)
  704. if actual_var is not None:
  705. actual_var.mark_dirty(instance=self)
  706. def _dirty_computed_vars(self, from_vars: set[str] | None = None) -> set[str]:
  707. """Determine ComputedVars that need to be recalculated based on the given vars.
  708. Args:
  709. from_vars: find ComputedVar that depend on this set of vars. If unspecified, will use the dirty_vars.
  710. Returns:
  711. Set of computed vars to include in the delta.
  712. """
  713. return set(
  714. cvar
  715. for dirty_var in from_vars or self.dirty_vars
  716. for cvar in self.computed_var_dependencies[dirty_var]
  717. )
  718. def get_delta(self) -> Delta:
  719. """Get the delta for the state.
  720. Returns:
  721. The delta for the state.
  722. """
  723. delta = {}
  724. # Apply dirty variables down into substates
  725. self.dirty_vars.update(self._always_dirty_computed_vars())
  726. self._mark_dirty()
  727. # Return the dirty vars for this instance, any cached/dependent computed vars,
  728. # and always dirty computed vars (cache=False)
  729. delta_vars = (
  730. self.dirty_vars.intersection(self.base_vars)
  731. .union(self._dirty_computed_vars())
  732. .union(self._always_dirty_computed_vars())
  733. )
  734. subdelta = {
  735. prop: getattr(self, prop)
  736. for prop in delta_vars
  737. if not types.is_backend_variable(prop)
  738. }
  739. if len(subdelta) > 0:
  740. delta[self.get_full_name()] = subdelta
  741. # Recursively find the substate deltas.
  742. substates = self.substates
  743. for substate in self.dirty_substates:
  744. delta.update(substates[substate].get_delta())
  745. # Format the delta.
  746. delta = format.format_state(delta)
  747. # Return the delta.
  748. return delta
  749. def _mark_dirty(self):
  750. """Mark the substate and all parent states as dirty."""
  751. state_name = self.get_name()
  752. if (
  753. self.parent_state is not None
  754. and state_name not in self.parent_state.dirty_substates
  755. ):
  756. self.parent_state.dirty_substates.add(self.get_name())
  757. self.parent_state._mark_dirty()
  758. # have to mark computed vars dirty to allow access to newly computed
  759. # values within the same ComputedVar function
  760. self._mark_dirty_computed_vars()
  761. # Propagate dirty var / computed var status into substates
  762. substates = self.substates
  763. for var in self.dirty_vars:
  764. for substate_name in self.substate_var_dependencies[var]:
  765. self.dirty_substates.add(substate_name)
  766. substate = substates[substate_name]
  767. substate.dirty_vars.add(var)
  768. substate._mark_dirty()
  769. def _clean(self):
  770. """Reset the dirty vars."""
  771. # Recursively clean the substates.
  772. for substate in self.dirty_substates:
  773. self.substates[substate]._clean()
  774. # Clean this state.
  775. self.dirty_vars = set()
  776. self.dirty_substates = set()
  777. def dict(self, include_computed: bool = True, **kwargs) -> dict[str, Any]:
  778. """Convert the object to a dictionary.
  779. Args:
  780. include_computed: Whether to include computed vars.
  781. **kwargs: Kwargs to pass to the pydantic dict method.
  782. Returns:
  783. The object as a dictionary.
  784. """
  785. if include_computed:
  786. # Apply dirty variables down into substates to allow never-cached ComputedVar to
  787. # trigger recalculation of dependent vars
  788. self.dirty_vars.update(self._always_dirty_computed_vars())
  789. self._mark_dirty()
  790. base_vars = {
  791. prop_name: self.get_value(getattr(self, prop_name))
  792. for prop_name in self.base_vars
  793. }
  794. computed_vars = (
  795. {
  796. # Include the computed vars.
  797. prop_name: self.get_value(getattr(self, prop_name))
  798. for prop_name in self.computed_vars
  799. }
  800. if include_computed
  801. else {}
  802. )
  803. substate_vars = {
  804. k: v.dict(include_computed=include_computed, **kwargs)
  805. for k, v in self.substates.items()
  806. }
  807. variables = {**base_vars, **computed_vars, **substate_vars}
  808. return {k: variables[k] for k in sorted(variables)}
  809. class DefaultState(State):
  810. """The default empty state."""
  811. pass
  812. class StateUpdate(Base):
  813. """A state update sent to the frontend."""
  814. # The state delta.
  815. delta: Delta = {}
  816. # Events to be added to the event queue.
  817. events: List[Event] = []
  818. # Whether this is the final state update for the event.
  819. final: bool = True
  820. class StateManager(Base):
  821. """A class to manage many client states."""
  822. # The state class to use.
  823. state: Type[State] = DefaultState
  824. # The mapping of client ids to states.
  825. states: Dict[str, State] = {}
  826. # The token expiration time (s).
  827. token_expiration: int = constants.TOKEN_EXPIRATION
  828. # The redis client to use.
  829. redis: Optional[Redis] = None
  830. def setup(self, state: Type[State]):
  831. """Set up the state manager.
  832. Args:
  833. state: The state class to use.
  834. """
  835. self.state = state
  836. self.redis = prerequisites.get_redis()
  837. def get_state(self, token: str) -> State:
  838. """Get the state for a token.
  839. Args:
  840. token: The token to get the state for.
  841. Returns:
  842. The state for the token.
  843. """
  844. if self.redis is not None:
  845. redis_state = self.redis.get(token)
  846. if redis_state is None:
  847. self.set_state(token, self.state())
  848. return self.get_state(token)
  849. return cloudpickle.loads(redis_state)
  850. if token not in self.states:
  851. self.states[token] = self.state()
  852. return self.states[token]
  853. def set_state(self, token: str, state: State):
  854. """Set the state for a token.
  855. Args:
  856. token: The token to set the state for.
  857. state: The state to set.
  858. """
  859. if self.redis is None:
  860. return
  861. self.redis.set(token, cloudpickle.dumps(state), ex=self.token_expiration)
  862. class ClientStorageBase:
  863. """Base class for client-side storage."""
  864. def options(self) -> dict[str, Any]:
  865. """Get the options for the storage.
  866. Returns:
  867. All set options for the storage (not None).
  868. """
  869. return {
  870. format.to_camel_case(k): v for k, v in vars(self).items() if v is not None
  871. }
  872. class Cookie(ClientStorageBase, str):
  873. """Represents a state Var that is stored as a cookie in the browser."""
  874. name: str | None
  875. path: str
  876. max_age: int | None
  877. domain: str | None
  878. secure: bool | None
  879. same_site: str
  880. def __new__(
  881. cls,
  882. object: Any = "",
  883. encoding: str | None = None,
  884. errors: str | None = None,
  885. /,
  886. name: str | None = None,
  887. path: str = "/",
  888. max_age: int | None = None,
  889. domain: str | None = None,
  890. secure: bool | None = None,
  891. same_site: str = "lax",
  892. ):
  893. """Create a client-side Cookie (str).
  894. Args:
  895. object: The initial object.
  896. encoding: The encoding to use.
  897. errors: The error handling scheme to use.
  898. name: The name of the cookie on the client side.
  899. path: Cookie path. Use / as the path if the cookie should be accessible on all pages.
  900. max_age: Relative max age of the cookie in seconds from when the client receives it.
  901. domain: Domain for the cookie (sub.domain.com or .allsubdomains.com).
  902. secure: Is the cookie only accessible through HTTPS?
  903. same_site: Whether the cookie is sent with third party requests.
  904. One of (true|false|none|lax|strict)
  905. Returns:
  906. The client-side Cookie object.
  907. Note: expires (absolute Date) is not supported at this time.
  908. """
  909. if encoding or errors:
  910. inst = super().__new__(cls, object, encoding or "utf-8", errors or "strict")
  911. else:
  912. inst = super().__new__(cls, object)
  913. inst.name = name
  914. inst.path = path
  915. inst.max_age = max_age
  916. inst.domain = domain
  917. inst.secure = secure
  918. inst.same_site = same_site
  919. return inst
  920. class LocalStorage(ClientStorageBase, str):
  921. """Represents a state Var that is stored in localStorage in the browser."""
  922. name: str | None
  923. def __new__(
  924. cls,
  925. object: Any = "",
  926. encoding: str | None = None,
  927. errors: str | None = None,
  928. /,
  929. name: str | None = None,
  930. ) -> "LocalStorage":
  931. """Create a client-side localStorage (str).
  932. Args:
  933. object: The initial object.
  934. encoding: The encoding to use.
  935. errors: The error handling scheme to use.
  936. name: The name of the storage key on the client side.
  937. Returns:
  938. The client-side localStorage object.
  939. """
  940. if encoding or errors:
  941. inst = super().__new__(cls, object, encoding or "utf-8", errors or "strict")
  942. else:
  943. inst = super().__new__(cls, object)
  944. inst.name = name
  945. return inst
  946. class MutableProxy(wrapt.ObjectProxy):
  947. """A proxy for a mutable object that tracks changes."""
  948. # Methods on wrapped objects which should mark the state as dirty.
  949. __mark_dirty_attrs__ = set(
  950. [
  951. "add",
  952. "append",
  953. "clear",
  954. "difference_update",
  955. "discard",
  956. "extend",
  957. "insert",
  958. "intersection_update",
  959. "pop",
  960. "popitem",
  961. "remove",
  962. "reverse",
  963. "setdefault",
  964. "sort",
  965. "symmetric_difference_update",
  966. "update",
  967. ]
  968. )
  969. __mutable_types__ = (list, dict, set, Base)
  970. def __init__(self, wrapped: Any, state: State, field_name: str):
  971. """Create a proxy for a mutable object that tracks changes.
  972. Args:
  973. wrapped: The object to proxy.
  974. state: The state to mark dirty when the object is changed.
  975. field_name: The name of the field on the state associated with the
  976. wrapped object.
  977. """
  978. super().__init__(wrapped)
  979. self._self_state = state
  980. self._self_field_name = field_name
  981. def _mark_dirty(self, wrapped=None, instance=None, args=tuple(), kwargs=None):
  982. """Mark the state as dirty, then call a wrapped function.
  983. Intended for use with `FunctionWrapper` from the `wrapt` library.
  984. Args:
  985. wrapped: The wrapped function.
  986. instance: The instance of the wrapped function.
  987. args: The args for the wrapped function.
  988. kwargs: The kwargs for the wrapped function.
  989. """
  990. self._self_state.dirty_vars.add(self._self_field_name)
  991. self._self_state._mark_dirty()
  992. if wrapped is not None:
  993. wrapped(*args, **(kwargs or {}))
  994. def __getattribute__(self, __name: str) -> Any:
  995. """Get the attribute on the proxied object and return a proxy if mutable.
  996. Args:
  997. __name: The name of the attribute.
  998. Returns:
  999. The attribute value.
  1000. """
  1001. value = super().__getattribute__(__name)
  1002. if callable(value) and __name in super().__getattribute__(
  1003. "__mark_dirty_attrs__"
  1004. ):
  1005. # Wrap special callables, like "append", which should mark state dirty.
  1006. return wrapt.FunctionWrapper(
  1007. value,
  1008. super().__getattribute__("_mark_dirty"),
  1009. )
  1010. if isinstance(
  1011. value, super().__getattribute__("__mutable_types__")
  1012. ) and __name not in ("__wrapped__", "_self_state"):
  1013. # Recursively wrap mutable attribute values retrieved through this proxy.
  1014. return MutableProxy(
  1015. wrapped=value,
  1016. state=self._self_state,
  1017. field_name=self._self_field_name,
  1018. )
  1019. return value
  1020. def __getitem__(self, key) -> Any:
  1021. """Get the item on the proxied object and return a proxy if mutable.
  1022. Args:
  1023. key: The key of the item.
  1024. Returns:
  1025. The item value.
  1026. """
  1027. value = super().__getitem__(key)
  1028. if isinstance(value, self.__mutable_types__):
  1029. # Recursively wrap mutable items retrieved through this proxy.
  1030. return MutableProxy(
  1031. wrapped=value,
  1032. state=self._self_state,
  1033. field_name=self._self_field_name,
  1034. )
  1035. return value
  1036. def __delattr__(self, name):
  1037. """Delete the attribute on the proxied object and mark state dirty.
  1038. Args:
  1039. name: The name of the attribute.
  1040. """
  1041. self._mark_dirty(super().__delattr__, args=(name,))
  1042. def __delitem__(self, key):
  1043. """Delete the item on the proxied object and mark state dirty.
  1044. Args:
  1045. key: The key of the item.
  1046. """
  1047. self._mark_dirty(super().__delitem__, args=(key,))
  1048. def __setitem__(self, key, value):
  1049. """Set the item on the proxied object and mark state dirty.
  1050. Args:
  1051. key: The key of the item.
  1052. value: The value of the item.
  1053. """
  1054. self._mark_dirty(super().__setitem__, args=(key, value))
  1055. def __setattr__(self, name, value):
  1056. """Set the attribute on the proxied object and mark state dirty.
  1057. If the attribute starts with "_self_", then the state is NOT marked
  1058. dirty as these are internal proxy attributes.
  1059. Args:
  1060. name: The name of the attribute.
  1061. value: The value of the attribute.
  1062. """
  1063. if name.startswith("_self_"):
  1064. # Special case attributes of the proxy itself, not applied to the wrapped object.
  1065. super().__setattr__(name, value)
  1066. return
  1067. self._mark_dirty(super().__setattr__, args=(name, value))
  1068. def __copy__(self) -> Any:
  1069. """Return a copy of the proxy.
  1070. Returns:
  1071. A copy of the wrapped object, unconnected to the proxy.
  1072. """
  1073. return copy.copy(self.__wrapped__)
  1074. def __deepcopy__(self, memo=None) -> Any:
  1075. """Return a deepcopy of the proxy.
  1076. Args:
  1077. memo: The memo dict to use for the deepcopy.
  1078. Returns:
  1079. A deepcopy of the wrapped object, unconnected to the proxy.
  1080. """
  1081. return copy.deepcopy(self.__wrapped__, memo=memo)