# Routing

> Multi-page Python desktop apps with Next.js' App Router workflow: folders as addresses, nested layouts, loading and error files, data loading, actions, global state, middleware and native notifications in Pyzed.

Source: https://albakore.github.io/pyzed/routing/

(Image: 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.

```text
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
```

**In plain words:**

An *address* is where you are in the app, like a web page's URL: `/products/3` is the product
number 3. Going to another address is *navigating*. A *page* is what one address shows, and a
*layout* is the frame around pages (a sidebar, a menu) that stays put while you move.

## Start in one minute

1. Create a project with the router starter:

```bash
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.

```bash
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:

```bash
pyzed route add /orders/[id] --load --loading
pyzed route list
```

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

```python
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)
```

## What you get

- **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.

## Read next

- [Folders and files](https://albakore.github.io/pyzed/routing/basics/project-structure/): Every file name, dynamic folders, groups and the commands that create them.
- [Pages, layouts and templates](https://albakore.github.io/pyzed/routing/basics/pages-and-layouts/): What a page receives, layouts that keep their state, titles.
- [Navigating](https://albakore.github.io/pyzed/routing/basics/navigation/): Links, the router, history, keys and hooks.
- [Errors and missing pages](https://albakore.github.io/pyzed/routing/basics/errors/): error.py, not_found.py, redirects.
- [Calling the server](https://albakore.github.io/pyzed/routing/data/data-fetching/): load, loading.py, caching, refreshing and prefetching.
- [Changing data: actions](https://albakore.github.io/pyzed/routing/data/actions/): @pz.action: pending, errors and fresh data after a change.
- [State](https://albakore.github.io/pyzed/routing/data/state/): Component state, the address, context and the global pz.Store.
- [How a page is drawn](https://albakore.github.io/pyzed/routing/data/rendering/): The order of the files, threads, and what draws again.
- [Middleware](https://albakore.github.io/pyzed/routing/advanced/middleware/): Login checks, redirects and rewrites before a page shows.
- [Desktop notifications](https://albakore.github.io/pyzed/routing/advanced/notifications/): pz.notify on Windows, macOS and Linux, with buttons and clicks that open pages.
- [Routes in code](https://albakore.github.io/pyzed/routing/advanced/routes-in-code/): A dict of pages, a router inside your own app, forms and the web.
- [Good practices](https://albakore.github.io/pyzed/routing/advanced/best-practices/): Where each piece of code goes, testing and performance.
- [Coming from Next.js](https://albakore.github.io/pyzed/routing/advanced/nextjs/): Every Next.js concept and its Pyzed name.

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`.
