state.py 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084
  1. """Define the reflex state specification."""
  2. from __future__ import annotations
  3. import asyncio
  4. import contextlib
  5. import copy
  6. import functools
  7. import inspect
  8. import json
  9. import traceback
  10. import urllib.parse
  11. import uuid
  12. from abc import ABC, abstractmethod
  13. from collections import defaultdict
  14. from types import FunctionType, MethodType
  15. from typing import (
  16. Any,
  17. AsyncIterator,
  18. Callable,
  19. ClassVar,
  20. Dict,
  21. List,
  22. Optional,
  23. Sequence,
  24. Set,
  25. Type,
  26. )
  27. import cloudpickle
  28. import pydantic
  29. import wrapt
  30. from redis.asyncio import Redis
  31. from reflex import constants
  32. from reflex.base import Base
  33. from reflex.event import (
  34. Event,
  35. EventHandler,
  36. EventSpec,
  37. _no_chain_background_task,
  38. fix_events,
  39. window_alert,
  40. )
  41. from reflex.utils import console, format, prerequisites, types
  42. from reflex.utils.exceptions import ImmutableStateError, LockExpiredError
  43. from reflex.utils.serializers import SerializedType, serialize, serializer
  44. from reflex.vars import BaseVar, ComputedVar, Var
  45. Delta = Dict[str, Any]
  46. var = ComputedVar
  47. class HeaderData(Base):
  48. """An object containing headers data."""
  49. host: str = ""
  50. origin: str = ""
  51. upgrade: str = ""
  52. connection: str = ""
  53. pragma: str = ""
  54. cache_control: str = ""
  55. user_agent: str = ""
  56. sec_websocket_version: str = ""
  57. sec_websocket_key: str = ""
  58. sec_websocket_extensions: str = ""
  59. accept_encoding: str = ""
  60. accept_language: str = ""
  61. def __init__(self, router_data: Optional[dict] = None):
  62. """Initalize the HeaderData object based on router_data.
  63. Args:
  64. router_data: the router_data dict.
  65. """
  66. super().__init__()
  67. if router_data:
  68. for k, v in router_data.get(constants.RouteVar.HEADERS, {}).items():
  69. setattr(self, format.to_snake_case(k), v)
  70. class PageData(Base):
  71. """An object containing page data."""
  72. host: str = "" # repeated with self.headers.origin (remove or keep the duplicate?)
  73. path: str = ""
  74. raw_path: str = ""
  75. full_path: str = ""
  76. full_raw_path: str = ""
  77. params: dict = {}
  78. def __init__(self, router_data: Optional[dict] = None):
  79. """Initalize the PageData object based on router_data.
  80. Args:
  81. router_data: the router_data dict.
  82. """
  83. super().__init__()
  84. if router_data:
  85. self.host = router_data.get(constants.RouteVar.HEADERS, {}).get("origin")
  86. self.path = router_data.get(constants.RouteVar.PATH, "")
  87. self.raw_path = router_data.get(constants.RouteVar.ORIGIN, "")
  88. self.full_path = f"{self.host}{self.path}"
  89. self.full_raw_path = f"{self.host}{self.raw_path}"
  90. self.params = router_data.get(constants.RouteVar.QUERY, {})
  91. class SessionData(Base):
  92. """An object containing session data."""
  93. client_token: str = ""
  94. client_ip: str = ""
  95. session_id: str = ""
  96. def __init__(self, router_data: Optional[dict] = None):
  97. """Initalize the SessionData object based on router_data.
  98. Args:
  99. router_data: the router_data dict.
  100. """
  101. super().__init__()
  102. if router_data:
  103. self.client_token = router_data.get(constants.RouteVar.CLIENT_TOKEN, "")
  104. self.client_ip = router_data.get(constants.RouteVar.CLIENT_IP, "")
  105. self.session_id = router_data.get(constants.RouteVar.SESSION_ID, "")
  106. class RouterData(Base):
  107. """An object containing RouterData."""
  108. session: SessionData = SessionData()
  109. headers: HeaderData = HeaderData()
  110. page: PageData = PageData()
  111. def __init__(self, router_data: Optional[dict] = None):
  112. """Initialize the RouterData object.
  113. Args:
  114. router_data: the router_data dict.
  115. """
  116. super().__init__()
  117. self.session = SessionData(router_data)
  118. self.headers = HeaderData(router_data)
  119. self.page = PageData(router_data)
  120. class State(Base, ABC, extra=pydantic.Extra.allow):
  121. """The state of the app."""
  122. # A map from the var name to the var.
  123. vars: ClassVar[Dict[str, Var]] = {}
  124. # The base vars of the class.
  125. base_vars: ClassVar[Dict[str, BaseVar]] = {}
  126. # The computed vars of the class.
  127. computed_vars: ClassVar[Dict[str, ComputedVar]] = {}
  128. # Vars inherited by the parent state.
  129. inherited_vars: ClassVar[Dict[str, Var]] = {}
  130. # Backend vars that are never sent to the client.
  131. backend_vars: ClassVar[Dict[str, Any]] = {}
  132. # Backend vars inherited
  133. inherited_backend_vars: ClassVar[Dict[str, Any]] = {}
  134. # The event handlers.
  135. event_handlers: ClassVar[Dict[str, EventHandler]] = {}
  136. # The parent state.
  137. parent_state: Optional[State] = None
  138. # The substates of the state.
  139. substates: Dict[str, State] = {}
  140. # The set of dirty vars.
  141. dirty_vars: Set[str] = set()
  142. # The set of dirty substates.
  143. dirty_substates: Set[str] = set()
  144. # The routing path that triggered the state
  145. router_data: Dict[str, Any] = {}
  146. # Mapping of var name to set of computed variables that depend on it
  147. computed_var_dependencies: Dict[str, Set[str]] = {}
  148. # Mapping of var name to set of substates that depend on it
  149. substate_var_dependencies: Dict[str, Set[str]] = {}
  150. # Per-instance copy of backend variable values
  151. _backend_vars: Dict[str, Any] = {}
  152. # The router data for the current page
  153. router: RouterData = RouterData()
  154. # The hydrated bool.
  155. is_hydrated: bool = False
  156. def __init__(self, *args, parent_state: State | None = None, **kwargs):
  157. """Initialize the state.
  158. Args:
  159. *args: The args to pass to the Pydantic init method.
  160. parent_state: The parent state.
  161. **kwargs: The kwargs to pass to the Pydantic init method.
  162. Raises:
  163. ValueError: If a substate class shadows another.
  164. """
  165. kwargs["parent_state"] = parent_state
  166. super().__init__(*args, **kwargs)
  167. # initialize per-instance var dependency tracking
  168. self.computed_var_dependencies = defaultdict(set)
  169. self.substate_var_dependencies = defaultdict(set)
  170. # Setup the substates.
  171. for substate in self.get_substates():
  172. substate_name = substate.get_name()
  173. if substate_name in self.substates:
  174. raise ValueError(
  175. f"The substate class '{substate_name}' has been defined multiple times. Shadowing "
  176. f"substate classes is not allowed."
  177. )
  178. self.substates[substate_name] = substate(parent_state=self)
  179. # Convert the event handlers to functions.
  180. self._init_event_handlers()
  181. # Initialize computed vars dependencies.
  182. inherited_vars = set(self.inherited_vars).union(
  183. set(self.inherited_backend_vars),
  184. )
  185. for cvar_name, cvar in self.computed_vars.items():
  186. # Add the dependencies.
  187. for var in cvar._deps(objclass=type(self)):
  188. self.computed_var_dependencies[var].add(cvar_name)
  189. if var in inherited_vars:
  190. # track that this substate depends on its parent for this var
  191. state_name = self.get_name()
  192. parent_state = self.parent_state
  193. while parent_state is not None and var in parent_state.vars:
  194. parent_state.substate_var_dependencies[var].add(state_name)
  195. state_name, parent_state = (
  196. parent_state.get_name(),
  197. parent_state.parent_state,
  198. )
  199. # Create a fresh copy of the backend variables for this instance
  200. self._backend_vars = copy.deepcopy(self.backend_vars)
  201. def _init_event_handlers(self, state: State | None = None):
  202. """Initialize event handlers.
  203. Allow event handlers to be called directly on the instance. This is
  204. called recursively for all parent states.
  205. Args:
  206. state: The state to initialize the event handlers on.
  207. """
  208. if state is None:
  209. state = self
  210. # Convert the event handlers to functions.
  211. for name, event_handler in state.event_handlers.items():
  212. if event_handler.is_background:
  213. fn = _no_chain_background_task(type(state), name, event_handler.fn)
  214. else:
  215. fn = functools.partial(event_handler.fn, self)
  216. fn.__module__ = event_handler.fn.__module__ # type: ignore
  217. fn.__qualname__ = event_handler.fn.__qualname__ # type: ignore
  218. setattr(self, name, fn)
  219. # Also allow direct calling of parent state event handlers
  220. if state.parent_state is not None:
  221. self._init_event_handlers(state.parent_state)
  222. def __repr__(self) -> str:
  223. """Get the string representation of the state.
  224. Returns:
  225. The string representation of the state.
  226. """
  227. return f"{self.__class__.__name__}({self.dict()})"
  228. @classmethod
  229. def __init_subclass__(cls, **kwargs):
  230. """Do some magic for the subclass initialization.
  231. Args:
  232. **kwargs: The kwargs to pass to the pydantic init_subclass method.
  233. """
  234. super().__init_subclass__(**kwargs)
  235. # Event handlers should not shadow builtin state methods.
  236. cls._check_overridden_methods()
  237. # Get the parent vars.
  238. parent_state = cls.get_parent_state()
  239. if parent_state is not None:
  240. cls.inherited_vars = parent_state.vars
  241. cls.inherited_backend_vars = parent_state.backend_vars
  242. cls.new_backend_vars = {
  243. name: value
  244. for name, value in cls.__dict__.items()
  245. if types.is_backend_variable(name)
  246. and name not in cls.inherited_backend_vars
  247. and not isinstance(value, FunctionType)
  248. }
  249. cls.backend_vars = {**cls.inherited_backend_vars, **cls.new_backend_vars}
  250. # Set the base and computed vars.
  251. cls.base_vars = {
  252. f.name: BaseVar(_var_name=f.name, _var_type=f.outer_type_)._var_set_state(
  253. cls
  254. )
  255. for f in cls.get_fields().values()
  256. if f.name not in cls.get_skip_vars()
  257. }
  258. cls.computed_vars = {
  259. v._var_name: v._var_set_state(cls)
  260. for v in cls.__dict__.values()
  261. if isinstance(v, ComputedVar)
  262. }
  263. cls.vars = {
  264. **cls.inherited_vars,
  265. **cls.base_vars,
  266. **cls.computed_vars,
  267. }
  268. cls.event_handlers = {}
  269. # Setup the base vars at the class level.
  270. for prop in cls.base_vars.values():
  271. cls._init_var(prop)
  272. # Set up the event handlers.
  273. events = {
  274. name: fn
  275. for name, fn in cls.__dict__.items()
  276. if not name.startswith("_")
  277. and isinstance(fn, Callable)
  278. and not isinstance(fn, EventHandler)
  279. }
  280. for name, fn in events.items():
  281. handler = EventHandler(fn=fn)
  282. cls.event_handlers[name] = handler
  283. setattr(cls, name, handler)
  284. @classmethod
  285. def _check_overridden_methods(cls):
  286. """Check for shadow methods and raise error if any.
  287. Raises:
  288. NameError: When an event handler shadows an inbuilt state method.
  289. """
  290. overridden_methods = set()
  291. state_base_functions = cls._get_base_functions()
  292. for name, method in inspect.getmembers(cls, inspect.isfunction):
  293. # Check if the method is overridden and not a dunder method
  294. if (
  295. not name.startswith("__")
  296. and method.__name__ in state_base_functions
  297. and state_base_functions[method.__name__] != method
  298. ):
  299. overridden_methods.add(method.__name__)
  300. for method_name in overridden_methods:
  301. raise NameError(
  302. f"The event handler name `{method_name}` shadows a builtin State method; use a different name instead"
  303. )
  304. @classmethod
  305. def get_skip_vars(cls) -> set[str]:
  306. """Get the vars to skip when serializing.
  307. Returns:
  308. The vars to skip when serializing.
  309. """
  310. return set(cls.inherited_vars) | {
  311. "parent_state",
  312. "substates",
  313. "dirty_vars",
  314. "dirty_substates",
  315. "router_data",
  316. "computed_var_dependencies",
  317. "substate_var_dependencies",
  318. "_backend_vars",
  319. }
  320. @classmethod
  321. @functools.lru_cache()
  322. def get_parent_state(cls) -> Type[State] | None:
  323. """Get the parent state.
  324. Returns:
  325. The parent state.
  326. """
  327. parent_states = [
  328. base
  329. for base in cls.__bases__
  330. if types._issubclass(base, State) and base is not State
  331. ]
  332. assert len(parent_states) < 2, "Only one parent state is allowed."
  333. return parent_states[0] if len(parent_states) == 1 else None # type: ignore
  334. @classmethod
  335. @functools.lru_cache()
  336. def get_substates(cls) -> set[Type[State]]:
  337. """Get the substates of the state.
  338. Returns:
  339. The substates of the state.
  340. """
  341. return set(cls.__subclasses__())
  342. @classmethod
  343. @functools.lru_cache()
  344. def get_name(cls) -> str:
  345. """Get the name of the state.
  346. Returns:
  347. The name of the state.
  348. """
  349. return format.to_snake_case(cls.__name__)
  350. @classmethod
  351. @functools.lru_cache()
  352. def get_full_name(cls) -> str:
  353. """Get the full name of the state.
  354. Returns:
  355. The full name of the state.
  356. """
  357. name = cls.get_name()
  358. parent_state = cls.get_parent_state()
  359. if parent_state is not None:
  360. name = ".".join((parent_state.get_full_name(), name))
  361. return name
  362. @classmethod
  363. @functools.lru_cache()
  364. def get_class_substate(cls, path: Sequence[str]) -> Type[State]:
  365. """Get the class substate.
  366. Args:
  367. path: The path to the substate.
  368. Returns:
  369. The class substate.
  370. Raises:
  371. ValueError: If the substate is not found.
  372. """
  373. if len(path) == 0:
  374. return cls
  375. if path[0] == cls.get_name():
  376. if len(path) == 1:
  377. return cls
  378. path = path[1:]
  379. for substate in cls.get_substates():
  380. if path[0] == substate.get_name():
  381. return substate.get_class_substate(path[1:])
  382. raise ValueError(f"Invalid path: {path}")
  383. @classmethod
  384. def get_class_var(cls, path: Sequence[str]) -> Any:
  385. """Get the class var.
  386. Args:
  387. path: The path to the var.
  388. Returns:
  389. The class var.
  390. Raises:
  391. ValueError: If the path is invalid.
  392. """
  393. path, name = path[:-1], path[-1]
  394. substate = cls.get_class_substate(tuple(path))
  395. if not hasattr(substate, name):
  396. raise ValueError(f"Invalid path: {path}")
  397. return getattr(substate, name)
  398. @classmethod
  399. def _init_var(cls, prop: BaseVar):
  400. """Initialize a variable.
  401. Args:
  402. prop: The variable to initialize
  403. Raises:
  404. TypeError: if the variable has an incorrect type
  405. """
  406. if not types.is_valid_var_type(prop._var_type):
  407. raise TypeError(
  408. "State vars must be primitive Python types, "
  409. "Plotly figures, Pandas dataframes, "
  410. "or subclasses of rx.Base. "
  411. f'Found var "{prop._var_name}" with type {prop._var_type}.'
  412. )
  413. cls._set_var(prop)
  414. cls._create_setter(prop)
  415. cls._set_default_value(prop)
  416. @classmethod
  417. def add_var(cls, name: str, type_: Any, default_value: Any = None):
  418. """Add dynamically a variable to the State.
  419. The variable added this way can be used in the same way as a variable
  420. defined statically in the model.
  421. Args:
  422. name: The name of the variable
  423. type_: The type of the variable
  424. default_value: The default value of the variable
  425. Raises:
  426. NameError: if a variable of this name already exists
  427. """
  428. if name in cls.__fields__:
  429. raise NameError(
  430. f"The variable '{name}' already exist. Use a different name"
  431. )
  432. # create the variable based on name and type
  433. var = BaseVar(_var_name=name, _var_type=type_)
  434. var._var_set_state(cls)
  435. # add the pydantic field dynamically (must be done before _init_var)
  436. cls.add_field(var, default_value)
  437. cls._init_var(var)
  438. # update the internal dicts so the new variable is correctly handled
  439. cls.base_vars.update({name: var})
  440. cls.vars.update({name: var})
  441. # let substates know about the new variable
  442. for substate_class in cls.__subclasses__():
  443. substate_class.vars.setdefault(name, var)
  444. @classmethod
  445. def _set_var(cls, prop: BaseVar):
  446. """Set the var as a class member.
  447. Args:
  448. prop: The var instance to set.
  449. """
  450. setattr(cls, prop._var_name, prop)
  451. @classmethod
  452. def _create_setter(cls, prop: BaseVar):
  453. """Create a setter for the var.
  454. Args:
  455. prop: The var to create a setter for.
  456. """
  457. setter_name = prop.get_setter_name(include_state=False)
  458. if setter_name not in cls.__dict__:
  459. event_handler = EventHandler(fn=prop.get_setter())
  460. cls.event_handlers[setter_name] = event_handler
  461. setattr(cls, setter_name, event_handler)
  462. @classmethod
  463. def _set_default_value(cls, prop: BaseVar):
  464. """Set the default value for the var.
  465. Args:
  466. prop: The var to set the default value for.
  467. """
  468. # Get the pydantic field for the var.
  469. field = cls.get_fields()[prop._var_name]
  470. if field.required:
  471. default_value = prop.get_default_value()
  472. if default_value is not None:
  473. field.required = False
  474. field.default = default_value
  475. if (
  476. not field.required
  477. and field.default is None
  478. and not types.is_optional(prop._var_type)
  479. ):
  480. # Ensure frontend uses null coalescing when accessing.
  481. prop._var_type = Optional[prop._var_type]
  482. @staticmethod
  483. def _get_base_functions() -> dict[str, FunctionType]:
  484. """Get all functions of the state class excluding dunder methods.
  485. Returns:
  486. The functions of rx.State class as a dict.
  487. """
  488. return {
  489. func[0]: func[1]
  490. for func in inspect.getmembers(State, predicate=inspect.isfunction)
  491. if not func[0].startswith("__")
  492. }
  493. def get_token(self) -> str:
  494. """Return the token of the client associated with this state.
  495. Returns:
  496. The token of the client.
  497. """
  498. console.deprecate(
  499. feature_name="get_token",
  500. reason="replaced by `State.router.session.client_token`",
  501. deprecation_version="0.3.0",
  502. removal_version="0.4.0",
  503. )
  504. return self.router_data.get(constants.RouteVar.CLIENT_TOKEN, "")
  505. def get_sid(self) -> str:
  506. """Return the session ID of the client associated with this state.
  507. Returns:
  508. The session ID of the client.
  509. """
  510. console.deprecate(
  511. feature_name="get_sid",
  512. reason="replaced by `State.router.session.session_id`",
  513. deprecation_version="0.3.0",
  514. removal_version="0.4.0",
  515. )
  516. return self.router_data.get(constants.RouteVar.SESSION_ID, "")
  517. def get_headers(self) -> Dict:
  518. """Return the headers of the client associated with this state.
  519. Returns:
  520. The headers of the client.
  521. """
  522. console.deprecate(
  523. feature_name="get_headers",
  524. reason="replaced by `State.router.headers`",
  525. deprecation_version="0.3.0",
  526. removal_version="0.4.0",
  527. )
  528. return self.router_data.get(constants.RouteVar.HEADERS, {})
  529. def get_client_ip(self) -> str:
  530. """Return the IP of the client associated with this state.
  531. Returns:
  532. The IP of the client.
  533. """
  534. console.deprecate(
  535. feature_name="get_client_ip",
  536. reason="replaced by `State.router.session.client_ip`",
  537. deprecation_version="0.3.0",
  538. removal_version="0.4.0",
  539. )
  540. return self.router_data.get(constants.RouteVar.CLIENT_IP, "")
  541. def get_current_page(self, origin=False) -> str:
  542. """Obtain the path of current page from the router data.
  543. Args:
  544. origin: whether to return the base route as shown in browser
  545. Returns:
  546. The current page.
  547. """
  548. console.deprecate(
  549. feature_name="get_current_page",
  550. reason="replaced by State.router.page / self.router.page",
  551. deprecation_version="0.3.0",
  552. removal_version="0.4.0",
  553. )
  554. return self.router.page.raw_path if origin else self.router.page.path
  555. def get_query_params(self) -> dict[str, str]:
  556. """Obtain the query parameters for the queried page.
  557. The query object contains both the URI parameters and the GET parameters.
  558. Returns:
  559. The dict of query parameters.
  560. """
  561. console.deprecate(
  562. feature_name="get_query_params",
  563. reason="replaced by `State.router.page.params`",
  564. deprecation_version="0.3.0",
  565. removal_version="0.4.0",
  566. )
  567. return self.router_data.get(constants.RouteVar.QUERY, {})
  568. def get_cookies(self) -> dict[str, str]:
  569. """Obtain the cookies of the client stored in the browser.
  570. Returns:
  571. The dict of cookies.
  572. """
  573. console.deprecate(
  574. feature_name=f"rx.get_cookies",
  575. reason="and has been replaced by rx.Cookie, which can be used as a state var",
  576. deprecation_version="0.3.0",
  577. removal_version="0.4.0",
  578. )
  579. cookie_dict = {}
  580. cookies = self.get_headers().get(constants.RouteVar.COOKIE, "").split(";")
  581. cookie_pairs = [cookie.split("=") for cookie in cookies if cookie]
  582. for pair in cookie_pairs:
  583. key, value = pair[0].strip(), urllib.parse.unquote(pair[1].strip())
  584. try:
  585. # cast non-string values to the actual types.
  586. value = json.loads(value)
  587. except json.JSONDecodeError:
  588. pass
  589. finally:
  590. cookie_dict[key] = value
  591. return cookie_dict
  592. @classmethod
  593. def setup_dynamic_args(cls, args: dict[str, str]):
  594. """Set up args for easy access in renderer.
  595. Args:
  596. args: a dict of args
  597. """
  598. def argsingle_factory(param):
  599. @ComputedVar
  600. def inner_func(self) -> str:
  601. return self.router.page.params.get(param, "")
  602. return inner_func
  603. def arglist_factory(param):
  604. @ComputedVar
  605. def inner_func(self) -> List:
  606. return self.router.page.params.get(param, [])
  607. return inner_func
  608. for param, value in args.items():
  609. if value == constants.RouteArgType.SINGLE:
  610. func = argsingle_factory(param)
  611. elif value == constants.RouteArgType.LIST:
  612. func = arglist_factory(param)
  613. else:
  614. continue
  615. # to allow passing as a prop
  616. func._var_name = param
  617. cls.vars[param] = cls.computed_vars[param] = func._var_set_state(cls) # type: ignore
  618. setattr(cls, param, func)
  619. def __getattribute__(self, name: str) -> Any:
  620. """Get the state var.
  621. If the var is inherited, get the var from the parent state.
  622. Args:
  623. name: The name of the var.
  624. Returns:
  625. The value of the var.
  626. """
  627. # If the state hasn't been initialized yet, return the default value.
  628. if not super().__getattribute__("__dict__"):
  629. return super().__getattribute__(name)
  630. inherited_vars = {
  631. **super().__getattribute__("inherited_vars"),
  632. **super().__getattribute__("inherited_backend_vars"),
  633. }
  634. if name in inherited_vars:
  635. return getattr(super().__getattribute__("parent_state"), name)
  636. backend_vars = super().__getattribute__("_backend_vars")
  637. if name in backend_vars:
  638. value = backend_vars[name]
  639. else:
  640. value = super().__getattribute__(name)
  641. if isinstance(value, MutableProxy.__mutable_types__) and (
  642. name in super().__getattribute__("base_vars") or name in backend_vars
  643. ):
  644. # track changes in mutable containers (list, dict, set, etc)
  645. return MutableProxy(wrapped=value, state=self, field_name=name)
  646. return value
  647. def __setattr__(self, name: str, value: Any):
  648. """Set the attribute.
  649. If the attribute is inherited, set the attribute on the parent state.
  650. Args:
  651. name: The name of the attribute.
  652. value: The value of the attribute.
  653. """
  654. if isinstance(value, MutableProxy):
  655. # unwrap proxy objects when assigning back to the state
  656. value = value.__wrapped__
  657. # Set the var on the parent state.
  658. inherited_vars = {**self.inherited_vars, **self.inherited_backend_vars}
  659. if name in inherited_vars:
  660. setattr(self.parent_state, name, value)
  661. return
  662. if types.is_backend_variable(name) and name != "_backend_vars":
  663. self._backend_vars.__setitem__(name, value)
  664. self.dirty_vars.add(name)
  665. self._mark_dirty()
  666. return
  667. # Set the attribute.
  668. super().__setattr__(name, value)
  669. # Add the var to the dirty list.
  670. if name in self.vars or name in self.computed_var_dependencies:
  671. self.dirty_vars.add(name)
  672. self._mark_dirty()
  673. # For now, handle router_data updates as a special case
  674. if name == constants.ROUTER_DATA:
  675. self.dirty_vars.add(name)
  676. self._mark_dirty()
  677. # propagate router_data updates down the state tree
  678. for substate in self.substates.values():
  679. setattr(substate, name, value)
  680. def reset(self):
  681. """Reset all the base vars to their default values."""
  682. # Reset the base vars.
  683. fields = self.get_fields()
  684. for prop_name in self.base_vars:
  685. if prop_name == constants.ROUTER:
  686. continue # never reset the router data
  687. setattr(self, prop_name, copy.deepcopy(fields[prop_name].default))
  688. # Recursively reset the substates.
  689. for substate in self.substates.values():
  690. substate.reset()
  691. def _reset_client_storage(self):
  692. """Reset client storage base vars to their default values."""
  693. # Client-side storage is reset during hydrate so that clearing cookies
  694. # on the browser also resets the values on the backend.
  695. fields = self.get_fields()
  696. for prop_name in self.base_vars:
  697. field = fields[prop_name]
  698. if isinstance(field.default, ClientStorageBase) or (
  699. isinstance(field.type_, type)
  700. and issubclass(field.type_, ClientStorageBase)
  701. ):
  702. setattr(self, prop_name, copy.deepcopy(field.default))
  703. # Recursively reset the substate client storage.
  704. for substate in self.substates.values():
  705. substate._reset_client_storage()
  706. def get_substate(self, path: Sequence[str]) -> State | None:
  707. """Get the substate.
  708. Args:
  709. path: The path to the substate.
  710. Returns:
  711. The substate.
  712. Raises:
  713. ValueError: If the substate is not found.
  714. """
  715. if len(path) == 0:
  716. return self
  717. if path[0] == self.get_name():
  718. if len(path) == 1:
  719. return self
  720. path = path[1:]
  721. if path[0] not in self.substates:
  722. raise ValueError(f"Invalid path: {path}")
  723. return self.substates[path[0]].get_substate(path[1:])
  724. def _get_event_handler(
  725. self, event: Event
  726. ) -> tuple[State | StateProxy, EventHandler]:
  727. """Get the event handler for the given event.
  728. Args:
  729. event: The event to get the handler for.
  730. Returns:
  731. The event handler.
  732. Raises:
  733. ValueError: If the event handler or substate is not found.
  734. """
  735. # Get the event handler.
  736. path = event.name.split(".")
  737. path, name = path[:-1], path[-1]
  738. substate = self.get_substate(path)
  739. if not substate:
  740. raise ValueError(
  741. "The value of state cannot be None when processing an event."
  742. )
  743. handler = substate.event_handlers[name]
  744. # For background tasks, proxy the state
  745. if handler.is_background:
  746. substate = StateProxy(substate)
  747. return substate, handler
  748. async def _process(self, event: Event) -> AsyncIterator[StateUpdate]:
  749. """Obtain event info and process event.
  750. Args:
  751. event: The event to process.
  752. Yields:
  753. The state update after processing the event.
  754. """
  755. # Get the event handler.
  756. substate, handler = self._get_event_handler(event)
  757. # Run the event generator and yield state updates.
  758. async for update in self._process_event(
  759. handler=handler,
  760. state=substate,
  761. payload=event.payload,
  762. ):
  763. yield update
  764. def _check_valid(self, handler: EventHandler, events: Any) -> Any:
  765. """Check if the events yielded are valid. They must be EventHandlers or EventSpecs.
  766. Args:
  767. handler: EventHandler.
  768. events: The events to be checked.
  769. Raises:
  770. TypeError: If any of the events are not valid.
  771. Returns:
  772. The events as they are if valid.
  773. """
  774. def _is_valid_type(events: Any) -> bool:
  775. return isinstance(events, (EventHandler, EventSpec))
  776. if events is None or _is_valid_type(events):
  777. return events
  778. try:
  779. if all(_is_valid_type(e) for e in events):
  780. return events
  781. except TypeError:
  782. pass
  783. raise TypeError(
  784. f"Your handler {handler.fn.__qualname__} must only return/yield: None, Events or other EventHandlers referenced by their class (not using `self`)"
  785. )
  786. def _as_state_update(
  787. self,
  788. handler: EventHandler,
  789. events: EventSpec | list[EventSpec] | None,
  790. final: bool,
  791. ) -> StateUpdate:
  792. """Convert the events to a StateUpdate.
  793. Fixes the events and checks for validity before converting.
  794. Args:
  795. handler: The handler where the events originated from.
  796. events: The events to queue with the update.
  797. final: Whether the handler is done processing.
  798. Returns:
  799. The valid StateUpdate containing the events and final flag.
  800. """
  801. # get the delta from the root of the state tree
  802. state = self
  803. while state.parent_state is not None:
  804. state = state.parent_state
  805. token = self.router.session.client_token
  806. # Convert valid EventHandler and EventSpec into Event
  807. fixed_events = fix_events(self._check_valid(handler, events), token)
  808. # Get the delta after processing the event.
  809. delta = state.get_delta()
  810. state._clean()
  811. return StateUpdate(
  812. delta=delta,
  813. events=fixed_events,
  814. final=final if not handler.is_background else True,
  815. )
  816. async def _process_event(
  817. self, handler: EventHandler, state: State | StateProxy, payload: Dict
  818. ) -> AsyncIterator[StateUpdate]:
  819. """Process event.
  820. Args:
  821. handler: EventHandler to process.
  822. state: State to process the handler.
  823. payload: The event payload.
  824. Yields:
  825. StateUpdate object
  826. """
  827. # Get the function to process the event.
  828. fn = functools.partial(handler.fn, state)
  829. # Clean the state before processing the event.
  830. self._clean()
  831. # Wrap the function in a try/except block.
  832. try:
  833. # Handle async functions.
  834. if asyncio.iscoroutinefunction(fn.func):
  835. events = await fn(**payload)
  836. # Handle regular functions.
  837. else:
  838. events = fn(**payload)
  839. # Handle async generators.
  840. if inspect.isasyncgen(events):
  841. async for event in events:
  842. yield state._as_state_update(handler, event, final=False)
  843. yield state._as_state_update(handler, events=None, final=True)
  844. # Handle regular generators.
  845. elif inspect.isgenerator(events):
  846. try:
  847. while True:
  848. yield state._as_state_update(handler, next(events), final=False)
  849. except StopIteration as si:
  850. # the "return" value of the generator is not available
  851. # in the loop, we must catch StopIteration to access it
  852. if si.value is not None:
  853. yield state._as_state_update(handler, si.value, final=False)
  854. yield state._as_state_update(handler, events=None, final=True)
  855. # Handle regular event chains.
  856. else:
  857. yield state._as_state_update(handler, events, final=True)
  858. # If an error occurs, throw a window alert.
  859. except Exception:
  860. error = traceback.format_exc()
  861. print(error)
  862. yield state._as_state_update(
  863. handler,
  864. window_alert("An error occurred. See logs for details."),
  865. final=True,
  866. )
  867. def _always_dirty_computed_vars(self) -> set[str]:
  868. """The set of ComputedVars that always need to be recalculated.
  869. Returns:
  870. Set of all ComputedVar in this state where cache=False
  871. """
  872. return set(
  873. cvar_name
  874. for cvar_name, cvar in self.computed_vars.items()
  875. if not cvar._cache
  876. )
  877. def _mark_dirty_computed_vars(self) -> None:
  878. """Mark ComputedVars that need to be recalculated based on dirty_vars."""
  879. dirty_vars = self.dirty_vars
  880. while dirty_vars:
  881. calc_vars, dirty_vars = dirty_vars, set()
  882. for cvar in self._dirty_computed_vars(from_vars=calc_vars):
  883. self.dirty_vars.add(cvar)
  884. dirty_vars.add(cvar)
  885. actual_var = self.computed_vars.get(cvar)
  886. if actual_var is not None:
  887. actual_var.mark_dirty(instance=self)
  888. def _dirty_computed_vars(self, from_vars: set[str] | None = None) -> set[str]:
  889. """Determine ComputedVars that need to be recalculated based on the given vars.
  890. Args:
  891. from_vars: find ComputedVar that depend on this set of vars. If unspecified, will use the dirty_vars.
  892. Returns:
  893. Set of computed vars to include in the delta.
  894. """
  895. return set(
  896. cvar
  897. for dirty_var in from_vars or self.dirty_vars
  898. for cvar in self.computed_var_dependencies[dirty_var]
  899. )
  900. def get_delta(self) -> Delta:
  901. """Get the delta for the state.
  902. Returns:
  903. The delta for the state.
  904. """
  905. delta = {}
  906. # Apply dirty variables down into substates
  907. self.dirty_vars.update(self._always_dirty_computed_vars())
  908. self._mark_dirty()
  909. # Return the dirty vars for this instance, any cached/dependent computed vars,
  910. # and always dirty computed vars (cache=False)
  911. delta_vars = (
  912. self.dirty_vars.intersection(self.base_vars)
  913. .union(self._dirty_computed_vars())
  914. .union(self._always_dirty_computed_vars())
  915. )
  916. subdelta = {
  917. prop: getattr(self, prop)
  918. for prop in delta_vars
  919. if not types.is_backend_variable(prop)
  920. }
  921. if len(subdelta) > 0:
  922. delta[self.get_full_name()] = subdelta
  923. # Recursively find the substate deltas.
  924. substates = self.substates
  925. for substate in self.dirty_substates:
  926. delta.update(substates[substate].get_delta())
  927. # Format the delta.
  928. delta = format.format_state(delta)
  929. # Return the delta.
  930. return delta
  931. def _mark_dirty(self):
  932. """Mark the substate and all parent states as dirty."""
  933. state_name = self.get_name()
  934. if (
  935. self.parent_state is not None
  936. and state_name not in self.parent_state.dirty_substates
  937. ):
  938. self.parent_state.dirty_substates.add(self.get_name())
  939. self.parent_state._mark_dirty()
  940. # have to mark computed vars dirty to allow access to newly computed
  941. # values within the same ComputedVar function
  942. self._mark_dirty_computed_vars()
  943. # Propagate dirty var / computed var status into substates
  944. substates = self.substates
  945. for var in self.dirty_vars:
  946. for substate_name in self.substate_var_dependencies[var]:
  947. self.dirty_substates.add(substate_name)
  948. substate = substates[substate_name]
  949. substate.dirty_vars.add(var)
  950. substate._mark_dirty()
  951. def _clean(self):
  952. """Reset the dirty vars."""
  953. # Recursively clean the substates.
  954. for substate in self.dirty_substates:
  955. self.substates[substate]._clean()
  956. # Clean this state.
  957. self.dirty_vars = set()
  958. self.dirty_substates = set()
  959. def get_value(self, key: str) -> Any:
  960. """Get the value of a field (without proxying).
  961. The returned value will NOT track dirty state updates.
  962. Args:
  963. key: The key of the field.
  964. Returns:
  965. The value of the field.
  966. """
  967. if isinstance(key, MutableProxy):
  968. return super().get_value(key.__wrapped__)
  969. return super().get_value(key)
  970. def dict(self, include_computed: bool = True, **kwargs) -> dict[str, Any]:
  971. """Convert the object to a dictionary.
  972. Args:
  973. include_computed: Whether to include computed vars.
  974. **kwargs: Kwargs to pass to the pydantic dict method.
  975. Returns:
  976. The object as a dictionary.
  977. """
  978. if include_computed:
  979. # Apply dirty variables down into substates to allow never-cached ComputedVar to
  980. # trigger recalculation of dependent vars
  981. self.dirty_vars.update(self._always_dirty_computed_vars())
  982. self._mark_dirty()
  983. base_vars = {
  984. prop_name: self.get_value(getattr(self, prop_name))
  985. for prop_name in self.base_vars
  986. }
  987. computed_vars = (
  988. {
  989. # Include the computed vars.
  990. prop_name: self.get_value(getattr(self, prop_name))
  991. for prop_name in self.computed_vars
  992. }
  993. if include_computed
  994. else {}
  995. )
  996. variables = {**base_vars, **computed_vars}
  997. d = {
  998. self.get_full_name(): {k: variables[k] for k in sorted(variables)},
  999. }
  1000. for substate_d in [
  1001. v.dict(include_computed=include_computed, **kwargs)
  1002. for v in self.substates.values()
  1003. ]:
  1004. d.update(substate_d)
  1005. return d
  1006. async def __aenter__(self) -> State:
  1007. """Enter the async context manager protocol.
  1008. This should not be used for the State class, but exists for
  1009. type-compatibility with StateProxy.
  1010. Raises:
  1011. TypeError: always, because async contextmanager protocol is only supported for background task.
  1012. """
  1013. raise TypeError(
  1014. "Only background task should use `async with self` to modify state."
  1015. )
  1016. async def __aexit__(self, *exc_info: Any) -> None:
  1017. """Exit the async context manager protocol.
  1018. This should not be used for the State class, but exists for
  1019. type-compatibility with StateProxy.
  1020. Args:
  1021. exc_info: The exception info tuple.
  1022. """
  1023. pass
  1024. class StateProxy(wrapt.ObjectProxy):
  1025. """Proxy of a state instance to control mutability of vars for a background task.
  1026. Since a background task runs against a state instance without holding the
  1027. state_manager lock for the token, the reference may become stale if the same
  1028. state is modified by another event handler.
  1029. The proxy object ensures that writes to the state are blocked unless
  1030. explicitly entering a context which refreshes the state from state_manager
  1031. and holds the lock for the token until exiting the context. After exiting
  1032. the context, a StateUpdate may be emitted to the frontend to notify the
  1033. client of the state change.
  1034. A background task will be passed the `StateProxy` as `self`, so mutability
  1035. can be safely performed inside an `async with self` block.
  1036. class State(rx.State):
  1037. counter: int = 0
  1038. @rx.background
  1039. async def bg_increment(self):
  1040. await asyncio.sleep(1)
  1041. async with self:
  1042. self.counter += 1
  1043. """
  1044. def __init__(self, state_instance):
  1045. """Create a proxy for a state instance.
  1046. Args:
  1047. state_instance: The state instance to proxy.
  1048. """
  1049. super().__init__(state_instance)
  1050. self._self_app = getattr(prerequisites.get_app(), constants.CompileVars.APP)
  1051. self._self_substate_path = state_instance.get_full_name().split(".")
  1052. self._self_actx = None
  1053. self._self_mutable = False
  1054. async def __aenter__(self) -> StateProxy:
  1055. """Enter the async context manager protocol.
  1056. Sets mutability to True and enters the `App.modify_state` async context,
  1057. which refreshes the state from state_manager and holds the lock for the
  1058. given state token until exiting the context.
  1059. Background tasks should avoid blocking calls while inside the context.
  1060. Returns:
  1061. This StateProxy instance in mutable mode.
  1062. """
  1063. self._self_actx = self._self_app.modify_state(
  1064. self.__wrapped__.router.session.client_token
  1065. )
  1066. mutable_state = await self._self_actx.__aenter__()
  1067. super().__setattr__(
  1068. "__wrapped__", mutable_state.get_substate(self._self_substate_path)
  1069. )
  1070. self._self_mutable = True
  1071. return self
  1072. async def __aexit__(self, *exc_info: Any) -> None:
  1073. """Exit the async context manager protocol.
  1074. Sets proxy mutability to False and persists any state changes.
  1075. Args:
  1076. exc_info: The exception info tuple.
  1077. """
  1078. if self._self_actx is None:
  1079. return
  1080. self._self_mutable = False
  1081. await self._self_actx.__aexit__(*exc_info)
  1082. self._self_actx = None
  1083. def __enter__(self):
  1084. """Enter the regular context manager protocol.
  1085. This is not supported for background tasks, and exists only to raise a more useful exception
  1086. when the StateProxy is used incorrectly.
  1087. Raises:
  1088. TypeError: always, because only async contextmanager protocol is supported.
  1089. """
  1090. raise TypeError("Background task must use `async with self` to modify state.")
  1091. def __exit__(self, *exc_info: Any) -> None:
  1092. """Exit the regular context manager protocol.
  1093. Args:
  1094. exc_info: The exception info tuple.
  1095. """
  1096. pass
  1097. def __getattr__(self, name: str) -> Any:
  1098. """Get the attribute from the underlying state instance.
  1099. Args:
  1100. name: The name of the attribute.
  1101. Returns:
  1102. The value of the attribute.
  1103. """
  1104. value = super().__getattr__(name)
  1105. if not name.startswith("_self_") and isinstance(value, MutableProxy):
  1106. # ensure mutations to these containers are blocked unless proxy is _mutable
  1107. return ImmutableMutableProxy(
  1108. wrapped=value.__wrapped__,
  1109. state=self, # type: ignore
  1110. field_name=value._self_field_name,
  1111. )
  1112. if isinstance(value, functools.partial) and value.args[0] is self.__wrapped__:
  1113. # Rebind event handler to the proxy instance
  1114. value = functools.partial(
  1115. value.func,
  1116. self,
  1117. *value.args[1:],
  1118. **value.keywords,
  1119. )
  1120. if isinstance(value, MethodType) and value.__self__ is self.__wrapped__:
  1121. # Rebind methods to the proxy instance
  1122. value = type(value)(value.__func__, self) # type: ignore
  1123. return value
  1124. def __setattr__(self, name: str, value: Any) -> None:
  1125. """Set the attribute on the underlying state instance.
  1126. If the attribute is internal, set it on the proxy instance instead.
  1127. Args:
  1128. name: The name of the attribute.
  1129. value: The value of the attribute.
  1130. Raises:
  1131. ImmutableStateError: If the state is not in mutable mode.
  1132. """
  1133. if (
  1134. name.startswith("_self_") # wrapper attribute
  1135. or self._self_mutable # lock held
  1136. # non-persisted state attribute
  1137. or name in self.__wrapped__.get_skip_vars()
  1138. ):
  1139. super().__setattr__(name, value)
  1140. return
  1141. raise ImmutableStateError(
  1142. "Background task StateProxy is immutable outside of a context "
  1143. "manager. Use `async with self` to modify state."
  1144. )
  1145. class StateUpdate(Base):
  1146. """A state update sent to the frontend."""
  1147. # The state delta.
  1148. delta: Delta = {}
  1149. # Events to be added to the event queue.
  1150. events: List[Event] = []
  1151. # Whether this is the final state update for the event.
  1152. final: bool = True
  1153. class StateManager(Base, ABC):
  1154. """A class to manage many client states."""
  1155. # The state class to use.
  1156. state: Type[State]
  1157. @classmethod
  1158. def create(cls, state: Type[State]):
  1159. """Create a new state manager.
  1160. Args:
  1161. state: The state class to use.
  1162. Returns:
  1163. The state manager (either memory or redis).
  1164. """
  1165. redis = prerequisites.get_redis()
  1166. if redis is not None:
  1167. return StateManagerRedis(state=state, redis=redis)
  1168. return StateManagerMemory(state=state)
  1169. @abstractmethod
  1170. async def get_state(self, token: str) -> State:
  1171. """Get the state for a token.
  1172. Args:
  1173. token: The token to get the state for.
  1174. Returns:
  1175. The state for the token.
  1176. """
  1177. pass
  1178. @abstractmethod
  1179. async def set_state(self, token: str, state: State):
  1180. """Set the state for a token.
  1181. Args:
  1182. token: The token to set the state for.
  1183. state: The state to set.
  1184. """
  1185. pass
  1186. @abstractmethod
  1187. @contextlib.asynccontextmanager
  1188. async def modify_state(self, token: str) -> AsyncIterator[State]:
  1189. """Modify the state for a token while holding exclusive lock.
  1190. Args:
  1191. token: The token to modify the state for.
  1192. Yields:
  1193. The state for the token.
  1194. """
  1195. yield self.state()
  1196. class StateManagerMemory(StateManager):
  1197. """A state manager that stores states in memory."""
  1198. # The mapping of client ids to states.
  1199. states: Dict[str, State] = {}
  1200. # The mutex ensures the dict of mutexes is updated exclusively
  1201. _state_manager_lock = asyncio.Lock()
  1202. # The dict of mutexes for each client
  1203. _states_locks: Dict[str, asyncio.Lock] = pydantic.PrivateAttr({})
  1204. class Config:
  1205. """The Pydantic config."""
  1206. fields = {
  1207. "_states_locks": {"exclude": True},
  1208. }
  1209. async def get_state(self, token: str) -> State:
  1210. """Get the state for a token.
  1211. Args:
  1212. token: The token to get the state for.
  1213. Returns:
  1214. The state for the token.
  1215. """
  1216. if token not in self.states:
  1217. self.states[token] = self.state()
  1218. return self.states[token]
  1219. async def set_state(self, token: str, state: State):
  1220. """Set the state for a token.
  1221. Args:
  1222. token: The token to set the state for.
  1223. state: The state to set.
  1224. """
  1225. pass
  1226. @contextlib.asynccontextmanager
  1227. async def modify_state(self, token: str) -> AsyncIterator[State]:
  1228. """Modify the state for a token while holding exclusive lock.
  1229. Args:
  1230. token: The token to modify the state for.
  1231. Yields:
  1232. The state for the token.
  1233. """
  1234. if token not in self._states_locks:
  1235. async with self._state_manager_lock:
  1236. if token not in self._states_locks:
  1237. self._states_locks[token] = asyncio.Lock()
  1238. async with self._states_locks[token]:
  1239. state = await self.get_state(token)
  1240. yield state
  1241. await self.set_state(token, state)
  1242. class StateManagerRedis(StateManager):
  1243. """A state manager that stores states in redis."""
  1244. # The redis client to use.
  1245. redis: Redis
  1246. # The token expiration time (s).
  1247. token_expiration: int = constants.Expiration.TOKEN
  1248. # The maximum time to hold a lock (ms).
  1249. lock_expiration: int = constants.Expiration.LOCK
  1250. # The keyspace subscription string when redis is waiting for lock to be released
  1251. _redis_notify_keyspace_events: str = (
  1252. "K" # Enable keyspace notifications (target a particular key)
  1253. "g" # For generic commands (DEL, EXPIRE, etc)
  1254. "x" # For expired events
  1255. "e" # For evicted events (i.e. maxmemory exceeded)
  1256. )
  1257. # These events indicate that a lock is no longer held
  1258. _redis_keyspace_lock_release_events: Set[bytes] = {
  1259. b"del",
  1260. b"expire",
  1261. b"expired",
  1262. b"evicted",
  1263. }
  1264. async def get_state(self, token: str) -> State:
  1265. """Get the state for a token.
  1266. Args:
  1267. token: The token to get the state for.
  1268. Returns:
  1269. The state for the token.
  1270. """
  1271. redis_state = await self.redis.get(token)
  1272. if redis_state is None:
  1273. await self.set_state(token, self.state())
  1274. return await self.get_state(token)
  1275. return cloudpickle.loads(redis_state)
  1276. async def set_state(self, token: str, state: State, lock_id: bytes | None = None):
  1277. """Set the state for a token.
  1278. Args:
  1279. token: The token to set the state for.
  1280. state: The state to set.
  1281. lock_id: If provided, the lock_key must be set to this value to set the state.
  1282. Raises:
  1283. LockExpiredError: If lock_id is provided and the lock for the token is not held by that ID.
  1284. """
  1285. # check that we're holding the lock
  1286. if (
  1287. lock_id is not None
  1288. and await self.redis.get(self._lock_key(token)) != lock_id
  1289. ):
  1290. raise LockExpiredError(
  1291. f"Lock expired for token {token} while processing. Consider increasing "
  1292. f"`app.state_manager.lock_expiration` (currently {self.lock_expiration}) "
  1293. "or use `@rx.background` decorator for long-running tasks."
  1294. )
  1295. await self.redis.set(token, cloudpickle.dumps(state), ex=self.token_expiration)
  1296. @contextlib.asynccontextmanager
  1297. async def modify_state(self, token: str) -> AsyncIterator[State]:
  1298. """Modify the state for a token while holding exclusive lock.
  1299. Args:
  1300. token: The token to modify the state for.
  1301. Yields:
  1302. The state for the token.
  1303. """
  1304. async with self._lock(token) as lock_id:
  1305. state = await self.get_state(token)
  1306. yield state
  1307. await self.set_state(token, state, lock_id)
  1308. @staticmethod
  1309. def _lock_key(token: str) -> bytes:
  1310. """Get the redis key for a token's lock.
  1311. Args:
  1312. token: The token to get the lock key for.
  1313. Returns:
  1314. The redis lock key for the token.
  1315. """
  1316. return f"{token}_lock".encode()
  1317. async def _try_get_lock(self, lock_key: bytes, lock_id: bytes) -> bool | None:
  1318. """Try to get a redis lock for a token.
  1319. Args:
  1320. lock_key: The redis key for the lock.
  1321. lock_id: The ID of the lock.
  1322. Returns:
  1323. True if the lock was obtained.
  1324. """
  1325. return await self.redis.set(
  1326. lock_key,
  1327. lock_id,
  1328. px=self.lock_expiration,
  1329. nx=True, # only set if it doesn't exist
  1330. )
  1331. async def _wait_lock(self, lock_key: bytes, lock_id: bytes) -> None:
  1332. """Wait for a redis lock to be released via pubsub.
  1333. Coroutine will not return until the lock is obtained.
  1334. Args:
  1335. lock_key: The redis key for the lock.
  1336. lock_id: The ID of the lock.
  1337. """
  1338. state_is_locked = False
  1339. lock_key_channel = f"__keyspace@0__:{lock_key.decode()}"
  1340. # Enable keyspace notifications for the lock key, so we know when it is available.
  1341. await self.redis.config_set(
  1342. "notify-keyspace-events", self._redis_notify_keyspace_events
  1343. )
  1344. async with self.redis.pubsub() as pubsub:
  1345. await pubsub.psubscribe(lock_key_channel)
  1346. while not state_is_locked:
  1347. # wait for the lock to be released
  1348. while True:
  1349. if not await self.redis.exists(lock_key):
  1350. break # key was removed, try to get the lock again
  1351. message = await pubsub.get_message(
  1352. ignore_subscribe_messages=True,
  1353. timeout=self.lock_expiration / 1000.0,
  1354. )
  1355. if message is None:
  1356. continue
  1357. if message["data"] in self._redis_keyspace_lock_release_events:
  1358. break
  1359. state_is_locked = await self._try_get_lock(lock_key, lock_id)
  1360. @contextlib.asynccontextmanager
  1361. async def _lock(self, token: str):
  1362. """Obtain a redis lock for a token.
  1363. Args:
  1364. token: The token to obtain a lock for.
  1365. Yields:
  1366. The ID of the lock (to be passed to set_state).
  1367. Raises:
  1368. LockExpiredError: If the lock has expired while processing the event.
  1369. """
  1370. lock_key = self._lock_key(token)
  1371. lock_id = uuid.uuid4().hex.encode()
  1372. if not await self._try_get_lock(lock_key, lock_id):
  1373. # Missed the fast-path to get lock, subscribe for lock delete/expire events
  1374. await self._wait_lock(lock_key, lock_id)
  1375. state_is_locked = True
  1376. try:
  1377. yield lock_id
  1378. except LockExpiredError:
  1379. state_is_locked = False
  1380. raise
  1381. finally:
  1382. if state_is_locked:
  1383. # only delete our lock
  1384. await self.redis.delete(lock_key)
  1385. class ClientStorageBase:
  1386. """Base class for client-side storage."""
  1387. def options(self) -> dict[str, Any]:
  1388. """Get the options for the storage.
  1389. Returns:
  1390. All set options for the storage (not None).
  1391. """
  1392. return {
  1393. format.to_camel_case(k): v for k, v in vars(self).items() if v is not None
  1394. }
  1395. class Cookie(ClientStorageBase, str):
  1396. """Represents a state Var that is stored as a cookie in the browser."""
  1397. name: str | None
  1398. path: str
  1399. max_age: int | None
  1400. domain: str | None
  1401. secure: bool | None
  1402. same_site: str
  1403. def __new__(
  1404. cls,
  1405. object: Any = "",
  1406. encoding: str | None = None,
  1407. errors: str | None = None,
  1408. /,
  1409. name: str | None = None,
  1410. path: str = "/",
  1411. max_age: int | None = None,
  1412. domain: str | None = None,
  1413. secure: bool | None = None,
  1414. same_site: str = "lax",
  1415. ):
  1416. """Create a client-side Cookie (str).
  1417. Args:
  1418. object: The initial object.
  1419. encoding: The encoding to use.
  1420. errors: The error handling scheme to use.
  1421. name: The name of the cookie on the client side.
  1422. path: Cookie path. Use / as the path if the cookie should be accessible on all pages.
  1423. max_age: Relative max age of the cookie in seconds from when the client receives it.
  1424. domain: Domain for the cookie (sub.domain.com or .allsubdomains.com).
  1425. secure: Is the cookie only accessible through HTTPS?
  1426. same_site: Whether the cookie is sent with third party requests.
  1427. One of (true|false|none|lax|strict)
  1428. Returns:
  1429. The client-side Cookie object.
  1430. Note: expires (absolute Date) is not supported at this time.
  1431. """
  1432. if encoding or errors:
  1433. inst = super().__new__(cls, object, encoding or "utf-8", errors or "strict")
  1434. else:
  1435. inst = super().__new__(cls, object)
  1436. inst.name = name
  1437. inst.path = path
  1438. inst.max_age = max_age
  1439. inst.domain = domain
  1440. inst.secure = secure
  1441. inst.same_site = same_site
  1442. return inst
  1443. class LocalStorage(ClientStorageBase, str):
  1444. """Represents a state Var that is stored in localStorage in the browser."""
  1445. name: str | None
  1446. def __new__(
  1447. cls,
  1448. object: Any = "",
  1449. encoding: str | None = None,
  1450. errors: str | None = None,
  1451. /,
  1452. name: str | None = None,
  1453. ) -> "LocalStorage":
  1454. """Create a client-side localStorage (str).
  1455. Args:
  1456. object: The initial object.
  1457. encoding: The encoding to use.
  1458. errors: The error handling scheme to use.
  1459. name: The name of the storage key on the client side.
  1460. Returns:
  1461. The client-side localStorage object.
  1462. """
  1463. if encoding or errors:
  1464. inst = super().__new__(cls, object, encoding or "utf-8", errors or "strict")
  1465. else:
  1466. inst = super().__new__(cls, object)
  1467. inst.name = name
  1468. return inst
  1469. class MutableProxy(wrapt.ObjectProxy):
  1470. """A proxy for a mutable object that tracks changes."""
  1471. # Methods on wrapped objects which should mark the state as dirty.
  1472. __mark_dirty_attrs__ = set(
  1473. [
  1474. "add",
  1475. "append",
  1476. "clear",
  1477. "difference_update",
  1478. "discard",
  1479. "extend",
  1480. "insert",
  1481. "intersection_update",
  1482. "pop",
  1483. "popitem",
  1484. "remove",
  1485. "reverse",
  1486. "setdefault",
  1487. "sort",
  1488. "symmetric_difference_update",
  1489. "update",
  1490. ]
  1491. )
  1492. # Methods on wrapped objects might return mutable objects that should be tracked.
  1493. __wrap_mutable_attrs__ = set(
  1494. [
  1495. "get",
  1496. "setdefault",
  1497. ]
  1498. )
  1499. __mutable_types__ = (list, dict, set, Base)
  1500. def __init__(self, wrapped: Any, state: State, field_name: str):
  1501. """Create a proxy for a mutable object that tracks changes.
  1502. Args:
  1503. wrapped: The object to proxy.
  1504. state: The state to mark dirty when the object is changed.
  1505. field_name: The name of the field on the state associated with the
  1506. wrapped object.
  1507. """
  1508. super().__init__(wrapped)
  1509. self._self_state = state
  1510. self._self_field_name = field_name
  1511. def _mark_dirty(
  1512. self,
  1513. wrapped=None,
  1514. instance=None,
  1515. args=tuple(),
  1516. kwargs=None,
  1517. ) -> Any:
  1518. """Mark the state as dirty, then call a wrapped function.
  1519. Intended for use with `FunctionWrapper` from the `wrapt` library.
  1520. Args:
  1521. wrapped: The wrapped function.
  1522. instance: The instance of the wrapped function.
  1523. args: The args for the wrapped function.
  1524. kwargs: The kwargs for the wrapped function.
  1525. Returns:
  1526. The result of the wrapped function.
  1527. """
  1528. self._self_state.dirty_vars.add(self._self_field_name)
  1529. self._self_state._mark_dirty()
  1530. if wrapped is not None:
  1531. return wrapped(*args, **(kwargs or {}))
  1532. def _wrap_recursive(self, value: Any) -> Any:
  1533. """Wrap a value recursively if it is mutable.
  1534. Args:
  1535. value: The value to wrap.
  1536. Returns:
  1537. The wrapped value.
  1538. """
  1539. if isinstance(value, self.__mutable_types__):
  1540. return type(self)(
  1541. wrapped=value,
  1542. state=self._self_state,
  1543. field_name=self._self_field_name,
  1544. )
  1545. return value
  1546. def _wrap_recursive_decorator(self, wrapped, instance, args, kwargs) -> Any:
  1547. """Wrap a function that returns a possibly mutable value.
  1548. Intended for use with `FunctionWrapper` from the `wrapt` library.
  1549. Args:
  1550. wrapped: The wrapped function.
  1551. instance: The instance of the wrapped function.
  1552. args: The args for the wrapped function.
  1553. kwargs: The kwargs for the wrapped function.
  1554. Returns:
  1555. The result of the wrapped function (possibly wrapped in a MutableProxy).
  1556. """
  1557. return self._wrap_recursive(wrapped(*args, **kwargs))
  1558. def __getattr__(self, __name: str) -> Any:
  1559. """Get the attribute on the proxied object and return a proxy if mutable.
  1560. Args:
  1561. __name: The name of the attribute.
  1562. Returns:
  1563. The attribute value.
  1564. """
  1565. value = super().__getattr__(__name)
  1566. if callable(value):
  1567. if __name in self.__mark_dirty_attrs__:
  1568. # Wrap special callables, like "append", which should mark state dirty.
  1569. value = wrapt.FunctionWrapper(value, self._mark_dirty)
  1570. if __name in self.__wrap_mutable_attrs__:
  1571. # Wrap methods that may return mutable objects tied to the state.
  1572. value = wrapt.FunctionWrapper(
  1573. value,
  1574. self._wrap_recursive_decorator,
  1575. )
  1576. if isinstance(value, self.__mutable_types__) and __name not in (
  1577. "__wrapped__",
  1578. "_self_state",
  1579. ):
  1580. # Recursively wrap mutable attribute values retrieved through this proxy.
  1581. return self._wrap_recursive(value)
  1582. return value
  1583. def __getitem__(self, key) -> Any:
  1584. """Get the item on the proxied object and return a proxy if mutable.
  1585. Args:
  1586. key: The key of the item.
  1587. Returns:
  1588. The item value.
  1589. """
  1590. value = super().__getitem__(key)
  1591. # Recursively wrap mutable items retrieved through this proxy.
  1592. return self._wrap_recursive(value)
  1593. def __iter__(self) -> Any:
  1594. """Iterate over the proxied object and return a proxy if mutable.
  1595. Yields:
  1596. Each item value (possibly wrapped in MutableProxy).
  1597. """
  1598. for value in super().__iter__():
  1599. # Recursively wrap mutable items retrieved through this proxy.
  1600. yield self._wrap_recursive(value)
  1601. def __delattr__(self, name):
  1602. """Delete the attribute on the proxied object and mark state dirty.
  1603. Args:
  1604. name: The name of the attribute.
  1605. """
  1606. self._mark_dirty(super().__delattr__, args=(name,))
  1607. def __delitem__(self, key):
  1608. """Delete the item on the proxied object and mark state dirty.
  1609. Args:
  1610. key: The key of the item.
  1611. """
  1612. self._mark_dirty(super().__delitem__, args=(key,))
  1613. def __setitem__(self, key, value):
  1614. """Set the item on the proxied object and mark state dirty.
  1615. Args:
  1616. key: The key of the item.
  1617. value: The value of the item.
  1618. """
  1619. self._mark_dirty(super().__setitem__, args=(key, value))
  1620. def __setattr__(self, name, value):
  1621. """Set the attribute on the proxied object and mark state dirty.
  1622. If the attribute starts with "_self_", then the state is NOT marked
  1623. dirty as these are internal proxy attributes.
  1624. Args:
  1625. name: The name of the attribute.
  1626. value: The value of the attribute.
  1627. """
  1628. if name.startswith("_self_"):
  1629. # Special case attributes of the proxy itself, not applied to the wrapped object.
  1630. super().__setattr__(name, value)
  1631. return
  1632. self._mark_dirty(super().__setattr__, args=(name, value))
  1633. def __copy__(self) -> Any:
  1634. """Return a copy of the proxy.
  1635. Returns:
  1636. A copy of the wrapped object, unconnected to the proxy.
  1637. """
  1638. return copy.copy(self.__wrapped__)
  1639. def __deepcopy__(self, memo=None) -> Any:
  1640. """Return a deepcopy of the proxy.
  1641. Args:
  1642. memo: The memo dict to use for the deepcopy.
  1643. Returns:
  1644. A deepcopy of the wrapped object, unconnected to the proxy.
  1645. """
  1646. return copy.deepcopy(self.__wrapped__, memo=memo)
  1647. @serializer
  1648. def serialize_mutable_proxy(mp: MutableProxy) -> SerializedType:
  1649. """Serialize the wrapped value of a MutableProxy.
  1650. Args:
  1651. mp: The MutableProxy to serialize.
  1652. Returns:
  1653. The serialized wrapped object.
  1654. Raises:
  1655. ValueError: when the wrapped object is not serializable.
  1656. """
  1657. value = serialize(mp.__wrapped__)
  1658. if value is None:
  1659. raise ValueError(f"Cannot serialize {type(mp.__wrapped__)}")
  1660. return value
  1661. class ImmutableMutableProxy(MutableProxy):
  1662. """A proxy for a mutable object that tracks changes.
  1663. This wrapper comes from StateProxy, and will raise an exception if an attempt is made
  1664. to modify the wrapped object when the StateProxy is immutable.
  1665. """
  1666. def _mark_dirty(
  1667. self,
  1668. wrapped=None,
  1669. instance=None,
  1670. args=tuple(),
  1671. kwargs=None,
  1672. ) -> Any:
  1673. """Raise an exception when an attempt is made to modify the object.
  1674. Intended for use with `FunctionWrapper` from the `wrapt` library.
  1675. Args:
  1676. wrapped: The wrapped function.
  1677. instance: The instance of the wrapped function.
  1678. args: The args for the wrapped function.
  1679. kwargs: The kwargs for the wrapped function.
  1680. Returns:
  1681. The result of the wrapped function.
  1682. Raises:
  1683. ImmutableStateError: if the StateProxy is not mutable.
  1684. """
  1685. if not self._self_state._self_mutable:
  1686. raise ImmutableStateError(
  1687. "Background task StateProxy is immutable outside of a context "
  1688. "manager. Use `async with self` to modify state."
  1689. )
  1690. return super()._mark_dirty(
  1691. wrapped=wrapped, instance=instance, args=args, kwargs=kwargs
  1692. )