event.py 9.5 KB

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