state.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. """Define the pynecone state specification."""
  2. from __future__ import annotations
  3. import asyncio
  4. import functools
  5. import traceback
  6. from abc import ABC
  7. from typing import Any, Callable, ClassVar, Dict, List, Optional, Sequence, Set, Type
  8. import cloudpickle
  9. from redis import Redis
  10. from pynecone import constants, utils
  11. from pynecone.base import Base
  12. from pynecone.event import Event, EventHandler, window_alert
  13. from pynecone.var import BaseVar, ComputedVar, PCList, Var
  14. Delta = Dict[str, Any]
  15. class State(Base, ABC):
  16. """The state of the app."""
  17. # A map from the var name to the var.
  18. vars: ClassVar[Dict[str, Var]] = {}
  19. # The base vars of the class.
  20. base_vars: ClassVar[Dict[str, BaseVar]] = {}
  21. # The computed vars of the class.
  22. computed_vars: ClassVar[Dict[str, ComputedVar]] = {}
  23. # Vars inherited by the parent state.
  24. inherited_vars: ClassVar[Dict[str, Var]] = {}
  25. # Backend vars that are never sent to the client.
  26. backend_vars: ClassVar[Dict[str, Any]] = {}
  27. # The parent state.
  28. parent_state: Optional[State] = None
  29. # The substates of the state.
  30. substates: Dict[str, State] = {}
  31. # The set of dirty vars.
  32. dirty_vars: Set[str] = set()
  33. # The set of dirty substates.
  34. dirty_substates: Set[str] = set()
  35. # The routing path that triggered the state
  36. router_data: Dict[str, Any] = {}
  37. def __init__(self, *args, **kwargs):
  38. """Initialize the state.
  39. Args:
  40. *args: The args to pass to the Pydantic init method.
  41. **kwargs: The kwargs to pass to the Pydantic init method.
  42. """
  43. super().__init__(*args, **kwargs)
  44. # Setup the substates.
  45. for substate in self.get_substates():
  46. self.substates[substate.get_name()] = substate().set(parent_state=self)
  47. self._init_mutable_fields()
  48. def _init_mutable_fields(self):
  49. """Initialize mutable fields.
  50. So that mutation to them can be detected by the app:
  51. * list
  52. """
  53. for field in self.base_vars.values():
  54. value = getattr(self, field.name)
  55. value_in_pc_data = _convert_mutable_datatypes(
  56. value, self._reassign_field, field.name
  57. )
  58. if utils._issubclass(field.type_, List):
  59. setattr(self, field.name, value_in_pc_data)
  60. self.clean()
  61. def _reassign_field(self, field_name: str):
  62. """Reassign the given field.
  63. Primarily for mutation in fields of mutable data types.
  64. Args:
  65. field_name: The name of the field we want to reassign
  66. """
  67. setattr(
  68. self,
  69. field_name,
  70. getattr(self, field_name),
  71. )
  72. def __repr__(self) -> str:
  73. """Get the string representation of the state.
  74. Returns:
  75. The string representation of the state.
  76. """
  77. return f"{self.__class__.__name__}({self.dict()})"
  78. @classmethod
  79. def __init_subclass__(cls, **kwargs):
  80. """Do some magic for the subclass initialization.
  81. Args:
  82. **kwargs: The kwargs to pass to the pydantic init_subclass method.
  83. """
  84. super().__init_subclass__(**kwargs)
  85. # Get the parent vars.
  86. parent_state = cls.get_parent_state()
  87. if parent_state is not None:
  88. cls.inherited_vars = parent_state.vars
  89. cls.backend_vars = {
  90. name: value
  91. for name, value in cls.__dict__.items()
  92. if utils.is_backend_variable(name)
  93. }
  94. # Set the base and computed vars.
  95. skip_vars = set(cls.inherited_vars) | {
  96. "parent_state",
  97. "substates",
  98. "dirty_vars",
  99. "dirty_substates",
  100. "router_data",
  101. }
  102. cls.base_vars = {
  103. f.name: BaseVar(name=f.name, type_=f.outer_type_).set_state(cls)
  104. for f in cls.get_fields().values()
  105. if f.name not in skip_vars
  106. }
  107. cls.computed_vars = {
  108. v.name: v.set_state(cls)
  109. for v in cls.__dict__.values()
  110. if isinstance(v, ComputedVar)
  111. }
  112. cls.vars = {
  113. **cls.inherited_vars,
  114. **cls.base_vars,
  115. **cls.computed_vars,
  116. }
  117. # Setup the base vars at the class level.
  118. for prop in cls.base_vars.values():
  119. cls._init_var(prop)
  120. # Set up the event handlers.
  121. events = {
  122. name: fn
  123. for name, fn in cls.__dict__.items()
  124. if not name.startswith("_") and isinstance(fn, Callable)
  125. }
  126. for name, fn in events.items():
  127. event_handler = EventHandler(fn=fn)
  128. setattr(cls, name, event_handler)
  129. @classmethod
  130. @functools.lru_cache()
  131. def get_parent_state(cls) -> Optional[Type[State]]:
  132. """Get the parent state.
  133. Returns:
  134. The parent state.
  135. """
  136. parent_states = [
  137. base
  138. for base in cls.__bases__
  139. if utils._issubclass(base, State) and base is not State
  140. ]
  141. assert len(parent_states) < 2, "Only one parent state is allowed."
  142. return parent_states[0] if len(parent_states) == 1 else None # type: ignore
  143. @classmethod
  144. @functools.lru_cache()
  145. def get_substates(cls) -> Set[Type[State]]:
  146. """Get the substates of the state.
  147. Returns:
  148. The substates of the state.
  149. """
  150. return set(cls.__subclasses__())
  151. @classmethod
  152. @functools.lru_cache()
  153. def get_name(cls) -> str:
  154. """Get the name of the state.
  155. Returns:
  156. The name of the state.
  157. """
  158. return utils.to_snake_case(cls.__name__)
  159. @classmethod
  160. @functools.lru_cache()
  161. def get_full_name(cls) -> str:
  162. """Get the full name of the state.
  163. Returns:
  164. The full name of the state.
  165. """
  166. name = cls.get_name()
  167. parent_state = cls.get_parent_state()
  168. if parent_state is not None:
  169. name = ".".join((parent_state.get_full_name(), name))
  170. return name
  171. @classmethod
  172. @functools.lru_cache()
  173. def get_class_substate(cls, path: Sequence[str]) -> Type[State]:
  174. """Get the class substate.
  175. Args:
  176. path: The path to the substate.
  177. Returns:
  178. The class substate.
  179. Raises:
  180. ValueError: If the substate is not found.
  181. """
  182. if len(path) == 0:
  183. return cls
  184. if path[0] == cls.get_name():
  185. if len(path) == 1:
  186. return cls
  187. path = path[1:]
  188. for substate in cls.get_substates():
  189. if path[0] == substate.get_name():
  190. return substate.get_class_substate(path[1:])
  191. raise ValueError(f"Invalid path: {path}")
  192. @classmethod
  193. def get_class_var(cls, path: Sequence[str]) -> Any:
  194. """Get the class var.
  195. Args:
  196. path: The path to the var.
  197. Returns:
  198. The class var.
  199. Raises:
  200. ValueError: If the path is invalid.
  201. """
  202. path, name = path[:-1], path[-1]
  203. substate = cls.get_class_substate(tuple(path))
  204. if not hasattr(substate, name):
  205. raise ValueError(f"Invalid path: {path}")
  206. return getattr(substate, name)
  207. @classmethod
  208. def _init_var(cls, prop: BaseVar):
  209. """Initialize a variable.
  210. Args:
  211. prop (BaseVar): The variable to initialize
  212. Raises:
  213. TypeError: if the variable has an incorrect type
  214. """
  215. if not utils.is_valid_var_type(prop.type_):
  216. raise TypeError(
  217. "State vars must be primitive Python types, "
  218. "Plotly figures, Pandas dataframes, "
  219. "or subclasses of pc.Base. "
  220. f'Found var "{prop.name}" with type {prop.type_}.'
  221. )
  222. cls._set_var(prop)
  223. cls._create_setter(prop)
  224. cls._set_default_value(prop)
  225. @classmethod
  226. def add_var(cls, name: str, type_: Any, default_value: Any = None):
  227. """Add dynamically a variable to the State.
  228. The variable added this way can be used in the same way as a variable
  229. defined statically in the model.
  230. Args:
  231. name: The name of the variable
  232. type_: The type of the variable
  233. default_value: The default value of the variable
  234. Raises:
  235. NameError: if a variable of this name already exists
  236. """
  237. if name in cls.__fields__:
  238. raise NameError(
  239. f"The variable '{name}' already exist. Use a different name"
  240. )
  241. # create the variable based on name and type
  242. var = BaseVar(name=name, type_=type_)
  243. var.set_state(cls)
  244. # add the pydantic field dynamically (must be done before _init_var)
  245. cls.add_field(var, default_value)
  246. cls._init_var(var)
  247. # update the internal dicts so the new variable is correctly handled
  248. cls.base_vars.update({name: var})
  249. cls.vars.update({name: var})
  250. @classmethod
  251. def _set_var(cls, prop: BaseVar):
  252. """Set the var as a class member.
  253. Args:
  254. prop: The var instance to set.
  255. """
  256. setattr(cls, prop.name, prop)
  257. @classmethod
  258. def _create_setter(cls, prop: BaseVar):
  259. """Create a setter for the var.
  260. Args:
  261. prop: The var to create a setter for.
  262. """
  263. setter_name = prop.get_setter_name(include_state=False)
  264. if setter_name not in cls.__dict__:
  265. setattr(cls, setter_name, prop.get_setter())
  266. @classmethod
  267. def _set_default_value(cls, prop: BaseVar):
  268. """Set the default value for the var.
  269. Args:
  270. prop: The var to set the default value for.
  271. """
  272. # Get the pydantic field for the var.
  273. field = cls.get_fields()[prop.name]
  274. default_value = prop.get_default_value()
  275. if field.required and default_value is not None:
  276. field.required = False
  277. field.default = default_value
  278. def get_token(self) -> str:
  279. """Return the token of the client associated with this state.
  280. Returns:
  281. The token of the client.
  282. """
  283. return self.router_data.get(constants.RouteVar.CLIENT_TOKEN, "")
  284. def get_sid(self) -> str:
  285. """Return the session ID of the client associated with this state.
  286. Returns:
  287. The session ID of the client.
  288. """
  289. return self.router_data.get(constants.RouteVar.SESSION_ID, "")
  290. def get_headers(self) -> Dict:
  291. """Return the headers of the client associated with this state.
  292. Returns:
  293. The headers of the client.
  294. """
  295. return self.router_data.get(constants.RouteVar.HEADERS, {})
  296. def get_client_ip(self) -> str:
  297. """Return the IP of the client associated with this state.
  298. Returns:
  299. The IP of the client.
  300. """
  301. return self.router_data.get(constants.RouteVar.CLIENT_IP, "")
  302. def get_current_page(self) -> str:
  303. """Obtain the path of current page from the router data.
  304. Returns:
  305. The current page.
  306. """
  307. return self.router_data.get(constants.RouteVar.PATH, "")
  308. def get_query_params(self) -> Dict[str, str]:
  309. """Obtain the query parameters for the queried page.
  310. The query object contains both the URI parameters and the GET parameters.
  311. Returns:
  312. The dict of query parameters.
  313. """
  314. return self.router_data.get(constants.RouteVar.QUERY, {})
  315. @classmethod
  316. def setup_dynamic_args(cls, args: dict[str, str]):
  317. """Set up args for easy access in renderer.
  318. Args:
  319. args: a dict of args
  320. """
  321. def argsingle_factory(param):
  322. @ComputedVar
  323. def inner_func(self) -> str:
  324. return self.get_query_params().get(param, "")
  325. return inner_func
  326. def arglist_factory(param):
  327. @ComputedVar
  328. def inner_func(self) -> List:
  329. return self.get_query_params().get(param, [])
  330. return inner_func
  331. for param, value in args.items():
  332. if value == constants.RouteArgType.SINGLE:
  333. func = argsingle_factory(param)
  334. elif value == constants.RouteArgType.LIST:
  335. func = arglist_factory(param)
  336. else:
  337. continue
  338. cls.computed_vars[param] = func.set_state(cls) # type: ignore
  339. setattr(cls, param, func)
  340. def __getattribute__(self, name: str) -> Any:
  341. """Get the state var.
  342. If the var is inherited, get the var from the parent state.
  343. Args:
  344. name: The name of the var.
  345. Returns:
  346. The value of the var.
  347. """
  348. # Get the var from the parent state.
  349. if name in super().__getattribute__("inherited_vars"):
  350. return getattr(super().__getattribute__("parent_state"), name)
  351. elif name in super().__getattribute__("backend_vars"):
  352. return super().__getattribute__("backend_vars").__getitem__(name)
  353. return super().__getattribute__(name)
  354. def __setattr__(self, name: str, value: Any):
  355. """Set the attribute.
  356. If the attribute is inherited, set the attribute on the parent state.
  357. Args:
  358. name: The name of the attribute.
  359. value: The value of the attribute.
  360. """
  361. # Set the var on the parent state.
  362. if name in self.inherited_vars:
  363. setattr(self.parent_state, name, value)
  364. return
  365. if utils.is_backend_variable(name):
  366. self.backend_vars.__setitem__(name, value)
  367. self.mark_dirty()
  368. return
  369. # Set the attribute.
  370. super().__setattr__(name, value)
  371. # Add the var to the dirty list.
  372. if name in self.vars:
  373. self.dirty_vars.add(name)
  374. self.mark_dirty()
  375. def reset(self):
  376. """Reset all the base vars to their default values."""
  377. # Reset the base vars.
  378. fields = self.get_fields()
  379. for prop_name in self.base_vars:
  380. setattr(self, prop_name, fields[prop_name].default)
  381. # Recursively reset the substates.
  382. for substate in self.substates.values():
  383. substate.reset()
  384. # Clean the state.
  385. self.clean()
  386. def get_substate(self, path: Sequence[str]) -> Optional[State]:
  387. """Get the substate.
  388. Args:
  389. path: The path to the substate.
  390. Returns:
  391. The substate.
  392. Raises:
  393. ValueError: If the substate is not found.
  394. """
  395. if len(path) == 0:
  396. return self
  397. if path[0] == self.get_name():
  398. if len(path) == 1:
  399. return self
  400. path = path[1:]
  401. if path[0] not in self.substates:
  402. raise ValueError(f"Invalid path: {path}")
  403. return self.substates[path[0]].get_substate(path[1:])
  404. async def process(self, event: Event) -> StateUpdate:
  405. """Process an event.
  406. Args:
  407. event: The event to process.
  408. Returns:
  409. The state update after processing the event.
  410. """
  411. # Get the event handler.
  412. path = event.name.split(".")
  413. path, name = path[:-1], path[-1]
  414. substate = self.get_substate(path)
  415. handler = getattr(substate, name)
  416. # Process the event.
  417. fn = functools.partial(handler.fn, substate)
  418. try:
  419. if asyncio.iscoroutinefunction(fn.func):
  420. events = await fn(**event.payload)
  421. else:
  422. events = fn(**event.payload)
  423. except Exception:
  424. error = traceback.format_exc()
  425. print(error)
  426. return StateUpdate(
  427. events=[window_alert("An error occurred. See logs for details.")]
  428. )
  429. # Fix the returned events.
  430. events = utils.fix_events(events, event.token)
  431. # Get the delta after processing the event.
  432. delta = self.get_delta()
  433. # Reset the dirty vars.
  434. self.clean()
  435. # Return the state update.
  436. return StateUpdate(delta=delta, events=events)
  437. def get_delta(self) -> Delta:
  438. """Get the delta for the state.
  439. Returns:
  440. The delta for the state.
  441. """
  442. delta = {}
  443. # Return the dirty vars, as well as all computed vars.
  444. subdelta = {
  445. prop: getattr(self, prop)
  446. for prop in self.dirty_vars | self.computed_vars.keys()
  447. }
  448. if len(subdelta) > 0:
  449. delta[self.get_full_name()] = subdelta
  450. # Recursively find the substate deltas.
  451. substates = self.substates
  452. for substate in self.dirty_substates:
  453. delta.update(substates[substate].get_delta())
  454. # Format the delta.
  455. delta = utils.format_state(delta)
  456. # Return the delta.
  457. return delta
  458. def mark_dirty(self):
  459. """Mark the substate and all parent states as dirty."""
  460. if self.parent_state is not None:
  461. self.parent_state.dirty_substates.add(self.get_name())
  462. self.parent_state.mark_dirty()
  463. def clean(self):
  464. """Reset the dirty vars."""
  465. # Recursively clean the substates.
  466. for substate in self.dirty_substates:
  467. self.substates[substate].clean()
  468. # Clean this state.
  469. self.dirty_vars = set()
  470. self.dirty_substates = set()
  471. def dict(self, include_computed: bool = True, **kwargs) -> Dict[str, Any]:
  472. """Convert the object to a dictionary.
  473. Args:
  474. include_computed: Whether to include computed vars.
  475. **kwargs: Kwargs to pass to the pydantic dict method.
  476. Returns:
  477. The object as a dictionary.
  478. """
  479. base_vars = {
  480. prop_name: self.get_value(getattr(self, prop_name))
  481. for prop_name in self.base_vars
  482. }
  483. computed_vars = (
  484. {
  485. # Include the computed vars.
  486. prop_name: self.get_value(getattr(self, prop_name))
  487. for prop_name in self.computed_vars
  488. }
  489. if include_computed
  490. else {}
  491. )
  492. substate_vars = {
  493. k: v.dict(include_computed=include_computed, **kwargs)
  494. for k, v in self.substates.items()
  495. }
  496. vars = {**base_vars, **computed_vars, **substate_vars}
  497. return {k: vars[k] for k in sorted(vars)}
  498. class DefaultState(State):
  499. """The default empty state."""
  500. pass
  501. class StateUpdate(Base):
  502. """A state update sent to the frontend."""
  503. # The state delta.
  504. delta: Delta = {}
  505. # Events to be added to the event queue.
  506. events: List[Event] = []
  507. class StateManager(Base):
  508. """A class to manage many client states."""
  509. # The state class to use.
  510. state: Type[State] = DefaultState
  511. # The mapping of client ids to states.
  512. states: Dict[str, State] = {}
  513. # The token expiration time (s).
  514. token_expiration: int = constants.TOKEN_EXPIRATION
  515. # The redis client to use.
  516. redis: Optional[Redis] = None
  517. def setup(self, state: Type[State]):
  518. """Set up the state manager.
  519. Args:
  520. state: The state class to use.
  521. """
  522. self.state = state
  523. self.redis = utils.get_redis()
  524. def get_state(self, token: str) -> State:
  525. """Get the state for a token.
  526. Args:
  527. token: The token to get the state for.
  528. Returns:
  529. The state for the token.
  530. """
  531. if self.redis is not None:
  532. redis_state = self.redis.get(token)
  533. if redis_state is None:
  534. self.set_state(token, self.state())
  535. return self.get_state(token)
  536. return cloudpickle.loads(redis_state)
  537. if token not in self.states:
  538. self.states[token] = self.state()
  539. return self.states[token]
  540. def set_state(self, token: str, state: State):
  541. """Set the state for a token.
  542. Args:
  543. token: The token to set the state for.
  544. state: The state to set.
  545. """
  546. if self.redis is None:
  547. return
  548. self.redis.set(token, cloudpickle.dumps(state), ex=self.token_expiration)
  549. def _convert_mutable_datatypes(
  550. field_value: Any, reassign_field: Callable, field_name: str
  551. ) -> Any:
  552. """Recursively convert mutable data to the Pc data types.
  553. Note: right now only list & dict would be handled recursively.
  554. Args:
  555. field_value: The target field_value.
  556. reassign_field:
  557. The function to reassign the field in the parent state.
  558. field_name: the name of the field in the parent state
  559. Returns:
  560. The converted field_value
  561. """
  562. if isinstance(field_value, list):
  563. for index in range(len(field_value)):
  564. field_value[index] = _convert_mutable_datatypes(
  565. field_value[index], reassign_field, field_name
  566. )
  567. field_value = PCList(
  568. field_value, reassign_field=reassign_field, field_name=field_name
  569. )
  570. if isinstance(field_value, dict):
  571. for key, value in field_value.items():
  572. field_value[key] = _convert_mutable_datatypes(
  573. value, reassign_field, field_name
  574. )
  575. return field_value