Good practices
Where each thing goes
Section titled “Where each thing goes”shop/├── main.py only starts the router├── api.py every call to the server or the database├── state.py the stores: session, cart, preferences├── app/│ ├── layout.py the frame: sidebar, header│ ├── middleware.py login checks and redirects│ ├── _components/ pieces of UI shared by the routes│ └── products/│ ├── page.py what /products shows, and its load│ └── loading.py└── tests/| Code | Goes in |
|---|---|
| reading from a server or a database | load, calling api.py |
| a change (save, order, sign in) | a @pz.action, calling api.py |
| values many pages share | a pz.Store in state.py |
| a filter, a tab, a page number | the query (?sort=price) |
| what a component types or opens | pz.state |
| checks before a page | middleware.py |
| telling the user something happened | pz.notify or a toast |
| helpers only one route uses | a file next to its page.py, imported by name (import helpers) |
- Read in
load, change in actions. The page only decides what to show withdata. - One request per need. Combine what a page needs in one
loadwithasyncio.gather, rather than starting requests from components. - Let the cache work. Don’t copy
datainto a store; after an action the page refreshes by itself, andinvalidatemarks other pages. - Fail loudly. Raise from
load(response.raise_for_status()):error.pyshows it with “Try again”. Usepz.not_found()for things that don’t exist.
- One store per subject (
Session,Cart,Preferences), one instance each, in a module. - Replace values (
cart.items = [...]) instead of changing them in place, or callchanged()after. persistonly what the user expects to find next time (preferences, a draft), never tokens or passwords: they are saved as plain JSON.
- Nothing that waits in a page, a layout or
render(). The awaits go inloador an action. - Put a heavy piece (a big table, a chart) in a component, so it is only drawn again when its inputs change.
- Add
loading.pywhere a page takes time, near the page, so the layouts stay put. - Links prefetch on hover; for an obvious next step,
router.prefetch(href)it.
Testing
Section titled “Testing”A router works without a window, so a test can navigate and check what each page shows:
import pyzed as pz
import api
def test_product_page(monkeypatch): monkeypatch.setattr(api, "DELAY", 0) router = pz.Router("app", initial="/products/3") router.view() router.wait() # the loads assert router.title == "4K monitor · Shop"
def test_account_needs_a_session(): router = pz.Router("app", initial="/account") router.view() router.wait() assert router.pathname == "/login"
def test_add_to_cart(): add_to_cart.run(api.PRODUCTS[0]) # waits for the action assert cart.count == 1pyzed route list shows every address and its files, a quick check that the folders say what
you meant.