event.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. """Define event classes to connect the frontend and backend."""
  2. from __future__ import annotations
  3. import inspect
  4. import json
  5. from typing import Any, Callable, Dict, List, Set, Tuple
  6. from pynecone.base import Base
  7. from pynecone.var import BaseVar, Var
  8. class Event(Base):
  9. """An event that describes any state change in the app."""
  10. # The token to specify the client that the event is for.
  11. token: str
  12. # The event name.
  13. name: str
  14. # The routing data where event occurred
  15. router_data: Dict[str, Any] = {}
  16. # The event payload.
  17. payload: Dict[str, Any] = {}
  18. class EventHandler(Base):
  19. """An event handler responds to an event to update the state."""
  20. # The function to call in response to the event.
  21. fn: Callable
  22. class Config:
  23. """The Pydantic config."""
  24. # Needed to allow serialization of Callable.
  25. frozen = True
  26. def __call__(self, *args: Var) -> EventSpec:
  27. """Pass arguments to the handler to get an event spec.
  28. This method configures event handlers that take in arguments.
  29. Args:
  30. *args: The arguments to pass to the handler.
  31. Returns:
  32. The event spec, containing both the function and args.
  33. Raises:
  34. TypeError: If the arguments are invalid.
  35. """
  36. # Get the function args.
  37. fn_args = inspect.getfullargspec(self.fn).args[1:]
  38. # Construct the payload.
  39. values = []
  40. for arg in args:
  41. # If it is a Var, add the full name.
  42. if isinstance(arg, Var):
  43. values.append(arg.full_name)
  44. continue
  45. # Otherwise, convert to JSON.
  46. try:
  47. values.append(json.dumps(arg))
  48. except TypeError as e:
  49. raise TypeError(
  50. f"Arguments to event handlers must be Vars or JSON-serializable. Got {arg} of type {type(arg)}."
  51. ) from e
  52. payload = tuple(zip(fn_args, values))
  53. # Return the event spec.
  54. return EventSpec(handler=self, args=payload)
  55. class EventSpec(Base):
  56. """An event specification.
  57. Whereas an Event object is passed during runtime, a spec is used
  58. during compile time to outline the structure of an event.
  59. """
  60. # The event handler.
  61. handler: EventHandler
  62. # The local arguments on the frontend.
  63. local_args: Tuple[str, ...] = ()
  64. # The arguments to pass to the function.
  65. args: Tuple[Any, ...] = ()
  66. class Config:
  67. """The Pydantic config."""
  68. # Required to allow tuple fields.
  69. frozen = True
  70. class EventChain(Base):
  71. """Container for a chain of events that will be executed in order."""
  72. events: List[EventSpec]
  73. class Target(Base):
  74. """A Javascript event target."""
  75. checked: bool = False
  76. value: Any = None
  77. class FrontendEvent(Base):
  78. """A Javascript event."""
  79. target: Target = Target()
  80. # The default event argument.
  81. EVENT_ARG = BaseVar(name="_e", type_=FrontendEvent, is_local=True)
  82. # Special server-side events.
  83. def redirect(path: str) -> Event:
  84. """Redirect to a new path.
  85. Args:
  86. path: The path to redirect to.
  87. Returns:
  88. An event to redirect to the path.
  89. """
  90. return Event(
  91. token="",
  92. name="_redirect",
  93. payload={"path": path},
  94. )
  95. def console_log(message: str) -> Event:
  96. """Do a console.log on the browser.
  97. Args:
  98. message: The message to log.
  99. Returns:
  100. An event to log the message.
  101. """
  102. return Event(
  103. token="",
  104. name="_console",
  105. payload={"message": message},
  106. )
  107. def window_alert(message: str) -> Event:
  108. """Create a window alert on the browser.
  109. Args:
  110. message: The message to alert.
  111. Returns:
  112. An event to alert the message.
  113. """
  114. return Event(
  115. token="",
  116. name="_alert",
  117. payload={"message": message},
  118. )
  119. # A set of common event triggers.
  120. EVENT_TRIGGERS: Set[str] = {
  121. "on_focus",
  122. "on_blur",
  123. "on_click",
  124. "on_context_menu",
  125. "on_double_click",
  126. "on_mouse_down",
  127. "on_mouse_enter",
  128. "on_mouse_leave",
  129. "on_mouse_move",
  130. "on_mouse_out",
  131. "on_mouse_over",
  132. "on_mouse_up",
  133. "on_scroll",
  134. }