event.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. """Define event classes to connect the frontend and backend."""
  2. from __future__ import annotations
  3. import inspect
  4. from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union
  5. from pynecone import constants
  6. from pynecone.base import Base
  7. from pynecone.utils import format
  8. from pynecone.var import BaseVar, Var
  9. class Event(Base):
  10. """An event that describes any state change in the app."""
  11. # The token to specify the client that the event is for.
  12. token: str
  13. # The event name.
  14. name: str
  15. # The routing data where event occurred
  16. router_data: Dict[str, Any] = {}
  17. # The event payload.
  18. payload: Dict[Any, Any] = {}
  19. class EventHandler(Base):
  20. """An event handler responds to an event to update the state."""
  21. # The function to call in response to the event.
  22. fn: Callable
  23. class Config:
  24. """The Pydantic config."""
  25. # Needed to allow serialization of Callable.
  26. frozen = True
  27. def __call__(self, *args: Var) -> EventSpec:
  28. """Pass arguments to the handler to get an event spec.
  29. This method configures event handlers that take in arguments.
  30. Args:
  31. *args: The arguments to pass to the handler.
  32. Returns:
  33. The event spec, containing both the function and args.
  34. Raises:
  35. TypeError: If the arguments are invalid.
  36. """
  37. # Get the function args.
  38. fn_args = inspect.getfullargspec(self.fn).args[1:]
  39. fn_args = (Var.create_safe(arg) for arg in fn_args)
  40. # Construct the payload.
  41. values = []
  42. for arg in args:
  43. # Special case for file uploads.
  44. if isinstance(arg, FileUpload):
  45. return EventSpec(handler=self, upload=True)
  46. # Otherwise, convert to JSON.
  47. try:
  48. values.append(Var.create(arg, is_string=type(arg) is str))
  49. except TypeError as e:
  50. raise TypeError(
  51. f"Arguments to event handlers must be Vars or JSON-serializable. Got {arg} of type {type(arg)}."
  52. ) from e
  53. payload = tuple(zip(fn_args, values))
  54. # Return the event spec.
  55. return EventSpec(handler=self, args=payload)
  56. class EventSpec(Base):
  57. """An event specification.
  58. Whereas an Event object is passed during runtime, a spec is used
  59. during compile time to outline the structure of an event.
  60. """
  61. # The event handler.
  62. handler: EventHandler
  63. # The local arguments on the frontend.
  64. local_args: Tuple[Var, ...] = ()
  65. # The arguments to pass to the function.
  66. args: Tuple[Tuple[Var, Var], ...] = ()
  67. # Whether to upload files.
  68. upload: bool = False
  69. class Config:
  70. """The Pydantic config."""
  71. # Required to allow tuple fields.
  72. frozen = True
  73. class EventChain(Base):
  74. """Container for a chain of events that will be executed in order."""
  75. events: List[EventSpec]
  76. # Whether events are in fully controlled input.
  77. full_control: bool = False
  78. # State name when fully controlled.
  79. state_name: str = ""
  80. class Target(Base):
  81. """A Javascript event target."""
  82. checked: bool = False
  83. value: Any = None
  84. class FrontendEvent(Base):
  85. """A Javascript event."""
  86. target: Target = Target()
  87. key: str = ""
  88. # The default event argument.
  89. EVENT_ARG = BaseVar(name="_e", type_=FrontendEvent, is_local=True)
  90. class FileUpload(Base):
  91. """Class to represent a file upload."""
  92. pass
  93. # Special server-side events.
  94. def server_side(name: str, **kwargs) -> EventSpec:
  95. """A server-side event.
  96. Args:
  97. name: The name of the event.
  98. **kwargs: The arguments to pass to the event.
  99. Returns:
  100. An event spec for a server-side event.
  101. """
  102. def fn():
  103. return None
  104. fn.__qualname__ = name
  105. return EventSpec(
  106. handler=EventHandler(fn=fn),
  107. args=tuple(
  108. (Var.create_safe(k), Var.create_safe(v, is_string=type(v) is str))
  109. for k, v in kwargs.items()
  110. ),
  111. )
  112. def redirect(path: Union[str, Var[str]]) -> EventSpec:
  113. """Redirect to a new path.
  114. Args:
  115. path: The path to redirect to.
  116. Returns:
  117. An event to redirect to the path.
  118. """
  119. return server_side("_redirect", path=path)
  120. def console_log(message: Union[str, Var[str]]) -> EventSpec:
  121. """Do a console.log on the browser.
  122. Args:
  123. message: The message to log.
  124. Returns:
  125. An event to log the message.
  126. """
  127. return server_side("_console", message=message)
  128. def window_alert(message: Union[str, Var[str]]) -> EventSpec:
  129. """Create a window alert on the browser.
  130. Args:
  131. message: The message to alert.
  132. Returns:
  133. An event to alert the message.
  134. """
  135. return server_side("_alert", message=message)
  136. def set_value(ref: str, value: Any) -> EventSpec:
  137. """Set the value of a ref.
  138. Args:
  139. ref: The ref.
  140. value: The value to set.
  141. Returns:
  142. An event to set the ref.
  143. """
  144. return server_side(
  145. "_set_value", ref=Var.create_safe(format.format_ref(ref)), value=value
  146. )
  147. def get_event(state, event):
  148. """Get the event from the given state.
  149. Args:
  150. state: The state.
  151. event: The event.
  152. Returns:
  153. The event.
  154. """
  155. return f"{state.get_name()}.{event}"
  156. def get_hydrate_event(state) -> str:
  157. """Get the name of the hydrate event for the state.
  158. Args:
  159. state: The state.
  160. Returns:
  161. The name of the hydrate event.
  162. """
  163. return get_event(state, constants.HYDRATE)
  164. def call_event_handler(event_handler: EventHandler, arg: Var) -> EventSpec:
  165. """Call an event handler to get the event spec.
  166. This function will inspect the function signature of the event handler.
  167. If it takes in an arg, the arg will be passed to the event handler.
  168. Otherwise, the event handler will be called with no args.
  169. Args:
  170. event_handler: The event handler.
  171. arg: The argument to pass to the event handler.
  172. Returns:
  173. The event spec from calling the event handler.
  174. """
  175. args = inspect.getfullargspec(event_handler.fn).args
  176. if len(args) == 1:
  177. return event_handler()
  178. assert (
  179. len(args) == 2
  180. ), f"Event handler {event_handler.fn} must have 1 or 2 arguments."
  181. return event_handler(arg)
  182. def call_event_fn(fn: Callable, arg: Var) -> List[EventSpec]:
  183. """Call a function to a list of event specs.
  184. The function should return either a single EventSpec or a list of EventSpecs.
  185. If the function takes in an arg, the arg will be passed to the function.
  186. Otherwise, the function will be called with no args.
  187. Args:
  188. fn: The function to call.
  189. arg: The argument to pass to the function.
  190. Returns:
  191. The event specs from calling the function.
  192. Raises:
  193. ValueError: If the lambda has an invalid signature.
  194. """
  195. # Import here to avoid circular imports.
  196. from pynecone.event import EventHandler, EventSpec
  197. # Get the args of the lambda.
  198. args = inspect.getfullargspec(fn).args
  199. # Call the lambda.
  200. if len(args) == 0:
  201. out = fn()
  202. elif len(args) == 1:
  203. out = fn(arg)
  204. else:
  205. raise ValueError(f"Lambda {fn} must have 0 or 1 arguments.")
  206. # Convert the output to a list.
  207. if not isinstance(out, List):
  208. out = [out]
  209. # Convert any event specs to event specs.
  210. events = []
  211. for e in out:
  212. # Convert handlers to event specs.
  213. if isinstance(e, EventHandler):
  214. if len(args) == 0:
  215. e = e()
  216. elif len(args) == 1:
  217. e = e(arg)
  218. # Make sure the event spec is valid.
  219. if not isinstance(e, EventSpec):
  220. raise ValueError(f"Lambda {fn} returned an invalid event spec: {e}.")
  221. # Add the event spec to the chain.
  222. events.append(e)
  223. # Return the events.
  224. return events
  225. def get_handler_args(event_spec: EventSpec, arg: Var) -> Tuple[Tuple[Var, Var], ...]:
  226. """Get the handler args for the given event spec.
  227. Args:
  228. event_spec: The event spec.
  229. arg: The controlled event argument.
  230. Returns:
  231. The handler args.
  232. Raises:
  233. ValueError: If the event handler has an invalid signature.
  234. """
  235. args = inspect.getfullargspec(event_spec.handler.fn).args
  236. if len(args) < 2:
  237. raise ValueError(
  238. f"Event handler has an invalid signature, needed a method with a parameter, got {event_spec.handler}."
  239. )
  240. return event_spec.args if len(args) > 2 else ((Var.create_safe(args[1]), arg),)
  241. def fix_events(
  242. events: Optional[List[Union[EventHandler, EventSpec]]], token: str
  243. ) -> List[Event]:
  244. """Fix a list of events returned by an event handler.
  245. Args:
  246. events: The events to fix.
  247. token: The user token.
  248. Returns:
  249. The fixed events.
  250. """
  251. # If the event handler returns nothing, return an empty list.
  252. if events is None:
  253. return []
  254. # If the handler returns a single event, wrap it in a list.
  255. if not isinstance(events, List):
  256. events = [events]
  257. # Fix the events created by the handler.
  258. out = []
  259. for e in events:
  260. if not isinstance(e, (EventHandler, EventSpec)):
  261. e = EventHandler(fn=e)
  262. # Otherwise, create an event from the event spec.
  263. if isinstance(e, EventHandler):
  264. e = e()
  265. assert isinstance(e, EventSpec), f"Unexpected event type, {type(e)}."
  266. name = format.format_event_handler(e.handler)
  267. payload = {k.name: v.name for k, v in e.args}
  268. # Create an event and append it to the list.
  269. out.append(
  270. Event(
  271. token=token,
  272. name=name,
  273. payload=payload,
  274. )
  275. )
  276. return out
  277. # A set of common event triggers.
  278. EVENT_TRIGGERS: Set[str] = {
  279. "on_focus",
  280. "on_blur",
  281. "on_click",
  282. "on_context_menu",
  283. "on_double_click",
  284. "on_mouse_down",
  285. "on_mouse_enter",
  286. "on_mouse_leave",
  287. "on_mouse_move",
  288. "on_mouse_out",
  289. "on_mouse_over",
  290. "on_mouse_up",
  291. "on_scroll",
  292. }