Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add onload event handler #337

Merged
merged 1 commit into from
Jan 25, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion pynecone/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pynecone.compiler import compiler
from pynecone.compiler import utils as compiler_utils
from pynecone.components.component import Component, ComponentStyle
from pynecone.event import Event
from pynecone.event import Event, EventHandler
from pynecone.middleware import HydrateMiddleware, Middleware
from pynecone.model import Model
from pynecone.state import DefaultState, Delta, State, StateManager, StateUpdate
Expand Down Expand Up @@ -45,6 +45,9 @@ class App(Base):
# Middleware to add to the app.
middleware: List[Middleware] = []

# events handlers to trigger when a page load
load_events: Dict[str, EventHandler] = {}

def __init__(self, *args, **kwargs):
"""Initialize the app.

Expand Down Expand Up @@ -165,6 +168,7 @@ def add_page(
title: str = constants.DEFAULT_TITLE,
description: str = constants.DEFAULT_DESCRIPTION,
image=constants.DEFAULT_IMAGE,
on_load: Optional[EventHandler] = None,
):
"""Add a page to the app.

Expand All @@ -178,6 +182,7 @@ def add_page(
title: The title of the page.
description: The description of the page.
image: The image to display on the page.
on_load: The event handler that will be called each time the page load.
"""
if path is not None:
utils.deprecate(
Expand Down Expand Up @@ -213,6 +218,9 @@ def add_page(
self._check_routes_conflict(route)
self.pages[route] = component

if on_load:
self.load_events[route] = on_load

def _check_routes_conflict(self, new_route: str):
"""Verify if there is any conflict between the new route and any existing route.

Expand Down
9 changes: 9 additions & 0 deletions pynecone/middleware/hydrate_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,13 @@ def preprocess(self, app: App, state: State, event: Event) -> Optional[Delta]:
An optional state to return.
"""
if event.name == utils.get_hydrate_event(state):
route = event.router_data.get("pathname", "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice using the router_data

if route == "/":
load_event = app.load_events.get("index")
elif route:
load_event = app.load_events.get(route.lstrip("/"))
else:
load_event = None
if load_event:
load_event.fn(state)
return utils.format_state({state.get_name(): state.dict()})