This guide shows how to navigate between screens and pass data using the use_navigation() hook.
Call pn.use_navigation() inside a @pn.component to get a NavigationHandle. Use .push() and .pop() to change screens, passing a component reference with optional args.
import pythonnative as pn
from app.second_page import SecondPage
@pn.component
def HomeScreen():
nav = pn.use_navigation()
return pn.Column(
pn.Text("Home", style={"font_size": 24}),
pn.Button(
"Go next",
on_click=lambda: nav.push(
SecondPage,
args={"message": "Hello from Home"},
),
),
style={"spacing": 12, "padding": 16},
)On the target screen, retrieve args with nav.get_args():
@pn.component
def SecondPage():
nav = pn.use_navigation()
message = nav.get_args().get("message", "Second Page")
return pn.Column(
pn.Text(message, style={"font_size": 20}),
pn.Button("Back", on_click=nav.pop),
style={"spacing": 12, "padding": 16},
)pn.use_navigation() returns a NavigationHandle with:
.push(component, args=...)— navigate to a new screen. Pass a component reference (the@pn.componentfunction itself), with an optionalargsdict..pop()— go back to the previous screen..get_args()— retrieve the args dict passed by the caller.
PythonNative forwards lifecycle events from the host:
on_create— triggers the initial renderon_starton_resumeon_pauseon_stopon_destroyon_restart(Android only)on_save_instance_stateon_restore_instance_state
- On Android,
pushnavigates viaNavControllerto aPageFragmentand passespage_pathand optional JSONargs. - On iOS,
pushuses the rootUINavigationControllerto push a newViewControllerand passes page info via KVC.
- Each PythonNative screen is hosted by a Swift
ViewControllerinstance. - Screens are pushed and popped on a root
UINavigationController. - Lifecycle is forwarded from Swift to the registered Python component.
- Single host
MainActivitysets aNavHostFragmentcontaining a navigation graph. - Each PythonNative screen is represented by a generic
PageFragmentwhich instantiates the Python component and attaches its root view. push/popdelegate toNavController(via a smallNavigatorhelper).- Arguments live in Fragment arguments and restore across configuration changes.
- React Native: Android: single
Activity, screens managed viaFragments. iOS: screens map toUIViewControllers pushed onUINavigationController. - NativeScript: Android: single
Activity, pages asFragments. iOS: pages asUIViewControllers onUINavigationController. - Flutter: Android: single
Activity. iOS:FlutterViewControllerhosts Flutter's navigator.