state.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. """Define the pynecone state specification."""
  2. from __future__ import annotations
  3. import asyncio
  4. import functools
  5. import pickle
  6. import traceback
  7. from abc import ABC
  8. from typing import Any, Callable, ClassVar, Dict, List, Optional, Sequence, Set, Type
  9. from pynecone import constants, utils
  10. from pynecone.base import Base
  11. from pynecone.event import Event, EventHandler, window_alert
  12. from pynecone.var import BaseVar, ComputedVar, Var
  13. Delta = Dict[str, Any]
  14. class State(Base, ABC):
  15. """The state of the app."""
  16. # A map from the var name to the var.
  17. vars: ClassVar[Dict[str, Var]] = {}
  18. # The base vars of the class.
  19. base_vars: ClassVar[Dict[str, BaseVar]] = {}
  20. # The computed vars of the class.
  21. computed_vars: ClassVar[Dict[str, ComputedVar]] = {}
  22. # vars inherited by the parent state.
  23. inherited_vars: ClassVar[Dict[str, Var]] = {}
  24. # The parent state.
  25. parent_state: Optional[State] = None
  26. # The substates of the state.
  27. substates: Dict[str, State] = {}
  28. # The set of dirty vars.
  29. dirty_vars: Set[str] = set()
  30. # The set of dirty substates.
  31. dirty_substates: Set[str] = set()
  32. def __init__(self, *args, **kwargs):
  33. """Initialize the state.
  34. Args:
  35. *args: The args to pass to the Pydantic init method.
  36. **kwargs: The kwargs to pass to the Pydantic init method.
  37. """
  38. super().__init__(*args, **kwargs)
  39. # Setup the substates.
  40. for substate in self.get_substates():
  41. self.substates[substate.get_name()] = substate().set(parent_state=self)
  42. def __repr__(self) -> str:
  43. """Get the string representation of the state.
  44. Returns:
  45. The string representation of the state.
  46. """
  47. return f"{self.__class__.__name__}({self.dict()})"
  48. @classmethod
  49. def __init_subclass__(cls, **kwargs):
  50. """Do some magic for the subclass initialization.
  51. Args:
  52. **kwargs: The kwargs to pass to the pydantic init_subclass method.
  53. Raises:
  54. TypeError: If the class has a var with an invalid type.
  55. """
  56. super().__init_subclass__(**kwargs)
  57. # Get the parent vars.
  58. parent_state = cls.get_parent_state()
  59. if parent_state is not None:
  60. cls.inherited_vars = parent_state.vars
  61. # Set the base and computed vars.
  62. skip_vars = set(cls.inherited_vars) | {
  63. "parent_state",
  64. "substates",
  65. "dirty_vars",
  66. "dirty_substates",
  67. }
  68. cls.base_vars = {
  69. f.name: BaseVar(name=f.name, type_=f.outer_type_).set_state(cls)
  70. for f in cls.get_fields().values()
  71. if f.name not in skip_vars
  72. }
  73. cls.computed_vars = {
  74. v.name: v.set_state(cls)
  75. for v in cls.__dict__.values()
  76. if isinstance(v, ComputedVar)
  77. }
  78. cls.vars = {
  79. **cls.inherited_vars,
  80. **cls.base_vars,
  81. **cls.computed_vars,
  82. }
  83. # Setup the base vars at the class level.
  84. for prop in cls.base_vars.values():
  85. if not utils._issubclass(prop.type_, utils.StateVar):
  86. raise TypeError(
  87. "State vars must be primitive Python types, "
  88. "Plotly figures, Pandas dataframes, "
  89. "or subclasses of pc.Base. "
  90. f'Found var "{prop.name}" with type {prop.type_}.'
  91. )
  92. cls._set_var(prop)
  93. cls._create_setter(prop)
  94. cls._set_default_value(prop)
  95. # Set up the event handlers.
  96. events = {
  97. name: fn
  98. for name, fn in cls.__dict__.items()
  99. if not name.startswith("_") and isinstance(fn, Callable)
  100. }
  101. for name, fn in events.items():
  102. event_handler = EventHandler(fn=fn)
  103. setattr(cls, name, event_handler)
  104. @classmethod
  105. @functools.lru_cache()
  106. def get_parent_state(cls) -> Optional[Type[State]]:
  107. """Get the parent state.
  108. Returns:
  109. The parent state.
  110. """
  111. parent_states = [
  112. base
  113. for base in cls.__bases__
  114. if utils._issubclass(base, State) and base is not State
  115. ]
  116. assert len(parent_states) < 2, "Only one parent state is allowed."
  117. return parent_states[0] if len(parent_states) == 1 else None # type: ignore
  118. @classmethod
  119. @functools.lru_cache()
  120. def get_substates(cls) -> Set[Type[State]]:
  121. """Get the substates of the state.
  122. Returns:
  123. The substates of the state.
  124. """
  125. return {subclass for subclass in cls.__subclasses__()}
  126. @classmethod
  127. @functools.lru_cache()
  128. def get_name(cls) -> str:
  129. """Get the name of the state.
  130. Returns:
  131. The name of the state.
  132. """
  133. return utils.to_snake_case(cls.__name__)
  134. @classmethod
  135. @functools.lru_cache()
  136. def get_full_name(cls) -> str:
  137. """Get the full name of the state.
  138. Returns:
  139. The full name of the state.
  140. """
  141. name = cls.get_name()
  142. parent_state = cls.get_parent_state()
  143. if parent_state is not None:
  144. name = ".".join((parent_state.get_full_name(), name))
  145. return name
  146. @classmethod
  147. @functools.lru_cache()
  148. def get_class_substate(cls, path: Sequence[str]) -> Type[State]:
  149. """Get the class substate.
  150. Args:
  151. path: The path to the substate.
  152. Returns:
  153. The class substate.
  154. Raises:
  155. ValueError: If the substate is not found.
  156. """
  157. if len(path) == 0:
  158. return cls
  159. if path[0] == cls.get_name():
  160. if len(path) == 1:
  161. return cls
  162. path = path[1:]
  163. for substate in cls.get_substates():
  164. if path[0] == substate.get_name():
  165. return substate.get_class_substate(path[1:])
  166. raise ValueError(f"Invalid path: {path}")
  167. @classmethod
  168. def get_class_var(cls, path: Sequence[str]) -> Any:
  169. """Get the class var.
  170. Args:
  171. path: The path to the var.
  172. Returns:
  173. The class var.
  174. Raises:
  175. ValueError: If the path is invalid.
  176. """
  177. path, name = path[:-1], path[-1]
  178. substate = cls.get_class_substate(tuple(path))
  179. if not hasattr(substate, name):
  180. raise ValueError(f"Invalid path: {path}")
  181. return getattr(substate, name)
  182. @classmethod
  183. def _set_var(cls, prop: BaseVar):
  184. """Set the var as a class member.
  185. Args:
  186. prop: The var instance to set.
  187. """
  188. setattr(cls, prop.name, prop)
  189. @classmethod
  190. def _create_setter(cls, prop: BaseVar):
  191. """Create a setter for the var.
  192. Args:
  193. prop: The var to create a setter for.
  194. """
  195. setter_name = prop.get_setter_name(include_state=False)
  196. if setter_name not in cls.__dict__:
  197. setattr(cls, setter_name, prop.get_setter())
  198. @classmethod
  199. def _set_default_value(cls, prop: BaseVar):
  200. """Set the default value for the var.
  201. Args:
  202. prop: The var to set the default value for.
  203. """
  204. # Get the pydantic field for the var.
  205. field = cls.get_fields()[prop.name]
  206. default_value = prop.get_default_value()
  207. if field.required and default_value is not None:
  208. field.required = False
  209. field.default = default_value
  210. def __getattribute__(self, name: str) -> Any:
  211. """Get the attribute.
  212. Args:
  213. name: The name of the attribute.
  214. Returns:
  215. The attribute.
  216. Raises:
  217. Exception: If the attribute is not found.
  218. """
  219. # If it is an inherited var, return from the parent state.
  220. if name != "inherited_vars" and name in self.inherited_vars:
  221. return getattr(self.parent_state, name)
  222. try:
  223. return super().__getattribute__(name)
  224. except Exception as e:
  225. # Check if the attribute is a substate.
  226. if name in self.substates:
  227. return self.substates[name]
  228. raise e
  229. def __setattr__(self, name: str, value: Any):
  230. """Set the attribute.
  231. Args:
  232. name: The name of the attribute.
  233. value: The value of the attribute.
  234. """
  235. if name != "inherited_vars" and name in self.inherited_vars:
  236. setattr(self.parent_state, name, value)
  237. return
  238. # Set the attribute.
  239. super().__setattr__(name, value)
  240. # Add the var to the dirty list.
  241. if name in self.vars:
  242. self.dirty_vars.add(name)
  243. self.mark_dirty()
  244. def reset(self):
  245. """Reset all the base vars to their default values."""
  246. # Reset the base vars.
  247. fields = self.get_fields()
  248. for prop_name in self.base_vars:
  249. setattr(self, prop_name, fields[prop_name].default)
  250. # Recursively reset the substates.
  251. for substate in self.substates.values():
  252. substate.reset()
  253. # Clean the state.
  254. self.clean()
  255. def get_substate(self, path: Sequence[str]) -> Optional[State]:
  256. """Get the substate.
  257. Args:
  258. path: The path to the substate.
  259. Returns:
  260. The substate.
  261. Raises:
  262. ValueError: If the substate is not found.
  263. """
  264. if len(path) == 0:
  265. return self
  266. if path[0] == self.get_name():
  267. if len(path) == 1:
  268. return self
  269. path = path[1:]
  270. if path[0] not in self.substates:
  271. raise ValueError(f"Invalid path: {path}")
  272. return self.substates[path[0]].get_substate(path[1:])
  273. async def process(self, event: Event) -> StateUpdate:
  274. """Process an event.
  275. Args:
  276. event: The event to process.
  277. Returns:
  278. The state update after processing the event.
  279. """
  280. # Get the event handler.
  281. path = event.name.split(".")
  282. path, name = path[:-1], path[-1]
  283. substate = self.get_substate(path)
  284. handler = getattr(substate, name)
  285. # Process the event.
  286. fn = functools.partial(handler.fn, substate)
  287. try:
  288. if asyncio.iscoroutinefunction(fn.func):
  289. events = await fn(**event.payload)
  290. else:
  291. events = fn(**event.payload)
  292. except:
  293. error = traceback.format_exc()
  294. print(error)
  295. return StateUpdate(events=[window_alert(error)])
  296. # Fix the returned events.
  297. events = utils.fix_events(events, event.token)
  298. # Get the delta after processing the event.
  299. delta = self.get_delta()
  300. # Reset the dirty vars.
  301. self.clean()
  302. # Return the state update.
  303. return StateUpdate(delta=delta, events=events)
  304. def get_delta(self) -> Delta:
  305. """Get the delta for the state.
  306. Returns:
  307. The delta for the state.
  308. """
  309. delta = {}
  310. # Return the dirty vars, as well as all computed vars.
  311. subdelta = {
  312. prop: getattr(self, prop)
  313. for prop in self.dirty_vars | set(self.computed_vars.keys())
  314. }
  315. if len(subdelta) > 0:
  316. delta[self.get_full_name()] = subdelta
  317. # Recursively find the substate deltas.
  318. for substate in self.dirty_substates:
  319. delta.update(self.substates[substate].get_delta())
  320. # Format the delta.
  321. delta = utils.format_state(delta)
  322. # Return the delta.
  323. return delta
  324. def mark_dirty(self):
  325. """Mark the substate and all parent states as dirty."""
  326. if self.parent_state is not None:
  327. self.parent_state.dirty_substates.add(self.get_name())
  328. self.parent_state.mark_dirty()
  329. def clean(self):
  330. """Reset the dirty vars."""
  331. # Recursively clean the substates.
  332. for substate in self.dirty_substates:
  333. self.substates[substate].clean()
  334. # Clean this state.
  335. self.dirty_vars = set()
  336. self.dirty_substates = set()
  337. def dict(self, include_computed: bool = True, **kwargs) -> Dict[str, Any]:
  338. """Convert the object to a dictionary.
  339. Args:
  340. include_computed: Whether to include computed vars.
  341. **kwargs: Kwargs to pass to the pydantic dict method.
  342. Returns:
  343. The object as a dictionary.
  344. """
  345. base_vars = {
  346. prop_name: self.get_value(getattr(self, prop_name))
  347. for prop_name in self.base_vars
  348. }
  349. computed_vars = (
  350. {
  351. # Include the computed vars.
  352. prop_name: self.get_value(getattr(self, prop_name))
  353. for prop_name in self.computed_vars
  354. }
  355. if include_computed
  356. else {}
  357. )
  358. substate_vars = {
  359. k: v.dict(include_computed=include_computed, **kwargs)
  360. for k, v in self.substates.items()
  361. }
  362. vars = {**base_vars, **computed_vars, **substate_vars}
  363. return {k: vars[k] for k in sorted(vars)}
  364. class DefaultState(State):
  365. """The default empty state."""
  366. pass
  367. class StateUpdate(Base):
  368. """A state update sent to the frontend."""
  369. # The state delta.
  370. delta: Delta = {}
  371. # Events to be added to the event queue.
  372. events: List[Event] = []
  373. class StateManager(Base):
  374. """A class to manage many client states."""
  375. # The state class to use.
  376. state: Type[State] = DefaultState
  377. # The mapping of client ids to states.
  378. states: Dict[str, State] = {}
  379. # The token expiration time (s).
  380. token_expiration: int = constants.TOKEN_EXPIRATION
  381. # The redis client to use.
  382. redis: Any = None
  383. def setup(self, state: Type[State]):
  384. """Set up the state manager.
  385. Args:
  386. state: The state class to use.
  387. """
  388. self.state = state
  389. self.redis = utils.get_redis()
  390. def get_state(self, token: str) -> State:
  391. """Get the state for a token.
  392. Args:
  393. token: The token to get the state for.
  394. Returns:
  395. The state for the token.
  396. """
  397. if self.redis is not None:
  398. redis_state = self.redis.get(token)
  399. if redis_state is None:
  400. self.set_state(token, self.state())
  401. return self.get_state(token)
  402. return pickle.loads(redis_state)
  403. if token not in self.states:
  404. self.states[token] = self.state()
  405. return self.states[token]
  406. def set_state(self, token: str, state: State):
  407. """Set the state for a token.
  408. Args:
  409. token: The token to set the state for.
  410. state: The state to set.
  411. """
  412. if self.redis is None:
  413. return
  414. self.redis.set(token, pickle.dumps(state), ex=self.token_expiration)