state.py 21 KB

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