Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions pynecone/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,20 +816,28 @@ def format_cond(
return expr


def format_event_fn(fn: Callable) -> str:
"""Format a function as an event.
def format_event_handler(handler: EventHandler) -> str:
"""Format an event handler.

Args:
fn: The function to format.
handler: The event handler to format.

Returns:
The formatted function.
"""
from pynecone.event import EventHandler
# Get the class that defines the event handler.
parts = handler.fn.__qualname__.split(".")

if isinstance(fn, EventHandler):
fn = fn.fn
return to_snake_case(fn.__qualname__)
# If there's no enclosing class, just return the function name.
if len(parts) == 1:
return parts[-1]

# Get the state and the function name.
state_name, name = parts[-2:]

# Construct the full event handler name.
state = vars(sys.modules[handler.fn.__module__])[state_name]
return ".".join([state.get_full_name(), name])


def format_event(event_spec: EventSpec) -> str:
Expand All @@ -842,7 +850,7 @@ def format_event(event_spec: EventSpec) -> str:
The compiled event.
"""
args = ",".join([":".join((name, val)) for name, val in event_spec.args])
return f"E(\"{format_event_fn(event_spec.handler.fn)}\", {wrap(args, '{')})"
return f"E(\"{format_event_handler(event_spec.handler)}\", {wrap(args, '{')})"


USED_VARIABLES = set()
Expand Down Expand Up @@ -1058,7 +1066,7 @@ def fix_events(events: Optional[List[Event]], token: str) -> List[Event]:
if isinstance(e, EventHandler):
e = e()
assert isinstance(e, EventSpec), f"Unexpected event type, {type(e)}."
name = format_event_fn(e.handler.fn)
name = format_event_handler(e.handler)
payload = dict(e.args)

# Create an event and append it to the list.
Expand Down
Loading