Skip to content

Routing

Going through the example store: the product list loads, a product opens, back with Alt+Left, the docs and the settings

Pyzed’s router lets you build an app with many screens the way the Next.js App Router builds a website: every folder of app/ is a part of the address, and a few files with fixed names say what each address shows. If you know Next.js, you already know how to use it; if you don’t, it takes ten minutes to learn, and the same folders keep working as the app grows to fifty screens.

app/
├── layout.py wraps every page (a sidebar, a header) and keeps its state
├── page.py /
├── loading.py shown while a page loads its data
├── error.py shown when something fails, with "Try again"
├── not_found.py addresses that don't exist
├── middleware.py runs before every navigation (a login check)
├── products/
│ ├── page.py /products
│ └── [id]/page.py /products/3 params["id"] == "3"
└── settings/
├── layout.py tabs around the settings pages
└── profile/page.py /settings/profile
  1. Create a project with the router starter:

    Terminal window
    pyzed new shop --router # a uv project
    pyzed new shop --script --router # or just main.py and app/, without uv
  2. Run it with hot reload: saved files show up at once and new folders become new pages.

    Terminal window
    cd shop
    uv run pyzed dev main.py # or: python main.py
  3. Add pages from the command line, or by creating the folders yourself:

    Terminal window
    pyzed route add /orders/[id] --load --loading
    pyzed route list

main.py only starts the router; everything else lives in app/:

import pyzed as pz
class Shop(pz.RouterApp):
routes = "app" # the folder, next to this file
title = "Shop" # the window's title when a page has none
size = (1000, 680)
if __name__ == "__main__":
pz.run(Shop)
  • Folders are addresses: static parts, [id], [...slug] and [[...slug]], (groups) that only organize and _private folders for helpers.
  • Layouts that keep their state (a collapsed sidebar stays collapsed) and templates that start over on every navigation.
  • Data loading off the UI thread: async def load(params) next to a page; the closest loading.py shows meanwhile, and the data is cached and refreshed like Next.js does.
  • Actions for changes (@pz.action): pending, result and error for the button, and the page’s data reloads when it is done.
  • Global state (pz.Store): the cart, the session, the preferences, read by any page; only what read it draws again.
  • Middleware (middleware.py): redirects, rewrites and guards before a page shows.
  • Native notifications (pz.notify) whose click opens a page of the app.
  • History: back and forward with Alt+Left/Right, the mouse’s side buttons and Android’s back button, and the window’s title follows the page.
  • Addresses are URLs, so the same app/ folder maps to a browser’s address bar when the app goes to the web.

The example used in these pages is the store in examples/routing/ (also in the welcome screen’s gallery of Pyzed Design): run it with python examples/routing/main.py.