This guide shows how to navigate between pages and pass data.
Use push and pop on your Page to change screens. Pass a dotted path string or a class reference, with optional args.
import pythonnative as pn
class MainPage(pn.Page):
def __init__(self, native_instance):
super().__init__(native_instance)
def render(self):
return pn.Column(
pn.Text("Main Page", font_size=24),
pn.Button(
"Go next",
on_click=lambda: self.push(
"app.second_page.SecondPage",
args={"message": "Hello from Main"},
),
),
spacing=12,
padding=16,
)On the target page, retrieve args with self.get_args():
class SecondPage(pn.Page):
def __init__(self, native_instance):
super().__init__(native_instance)
def render(self):
message = self.get_args().get("message", "Second Page")
return pn.Column(
pn.Text(message, font_size=20),
pn.Button("Back", on_click=self.pop),
spacing=12,
padding=16,
)PythonNative forwards lifecycle events from the host:
on_create— triggers the initialrender()on_starton_resumeon_pauseon_stopon_destroyon_restart(Android only)on_save_instance_stateon_restore_instance_state
Override any of these on your Page subclass to respond to lifecycle changes.
- On Android,
pushnavigates viaNavControllerto aPageFragmentand passespage_pathand optional JSONargs. - On iOS,
pushuses the rootUINavigationControllerto push a newViewControllerand passes page info via KVC.
- Each PythonNative page is hosted by a Swift
ViewControllerinstance. - Pages are pushed and popped on a root
UINavigationController. - Lifecycle is forwarded from Swift to the registered Python page instance.
- Single host
MainActivitysets aNavHostFragmentcontaining a navigation graph. - Each PythonNative page is represented by a generic
PageFragmentwhich instantiates the Python page 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.