state.py 23 KB

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