Page stacks
push, pop, replace and reset; props and events for pages; results with await; how pages fill their space; transitions and the Back keys.
janelkit-router Navigation between screens: page stacks that keep their state, parallel sections, results with await and transitions. pip install janelkit-routerpush, pop, replace and reset
Each operation decides which pages are kept. A discarded page is destroyed and its on_destroy callbacks run; a covered page is kept. Keyword arguments are the new page’s props, and on_name arguments connect its events, as in open_window: nav.push(Detail, item=item, on_deleted=remove). Pages inherit context (inject) from the component that declares the stack.
| Call | Effect on the stack |
|---|---|
| push(Page, **props) | Adds Page on top; keeps the current page underneath. |
| pop() | Discards the top page. Does nothing on the first page. |
| back(value) | Like pop, and delivers value to whoever awaited the push. |
| replace(Page, **props) | Discards the current page and shows Page in its place. |
| reset(Page, **props) | Discards every page and starts again from Page. |
<!-- In the component that owns nav = pages(Home): a header with Back. -->
<row gap={8}>
<button on:click={nav.pop()} enabled={nav.can_go_back}>← Back</button>
<text>{nav.current}</text>
</row>
<Pages stack={nav} transition:slide />CopyFragment · Add it to an existing component; it is not a complete file.
How it works
- nav.can_go_back, nav.current (the name of the visible page) and nav.depth are reactive; nav.page is the visible page’s instance.
- transition:slide animates the page that enters: from the right going forward, from the left going back.
Common mistakes
- push always creates a new instance; it never reuses an existing page of the same type.
- Do not reset just to switch between destinations users expect to return to; use sections.
Wait for a page to return a result
push can be awaited. await nav.push(Picker) waits until Picker calls nav.back(value) and returns that value, or None if the page was left another way (pop, replace or reset). Use it in an async def handler, as if you were calling a function that happens to show a screen.
<script lang="python">
from janelkit_router import pages, Pages
from pages.Home import Home
nav = pages(Home)
</script>
<window title="Results" width={400} height={240}>
<Pages stack={nav} />
</window>Copy<script lang="python">
from janelkit import state
from janelkit_router import navigation
from pages.Picker import Picker
nav = navigation()
chosen = state("nobody")
async def choose():
person = await nav.push(Picker)
if person is not None:
chosen.value = person
</script>
<column padding={24} gap={12}>
<text>Chosen: {chosen}</text>
<button on:click={choose}>Choose a person</button>
</column>Copy<script lang="python">
from janelkit_router import navigation
nav = navigation()
</script>
<column padding={24} gap={8}>
<text>Pick one</text>
<button on:click={nav.back("Ada")}>Ada</button>
<button on:click={nav.back("Grace")}>Grace</button>
</column>CopyComplete example in 3 files · Save each one at the path shown above it.
What you should see
Chosen: nobody at first. Choose a person shows the picker; clicking Grace returns to Home with Chosen: Grace.
Pages fill their space
Each page fills its <Pages> (and each section its <Sections>) and shrinks with the window down to the minimum of its content; what should scroll, scrolls with overflow: auto. The root of a page can change that with CSS, like a block in a column.
.page { max-width: 720px; margin: 0 auto; } /* a centered column */
.page { width: 400px; height: 300px; } /* fixed size, top left */
.page { align-self: center; } /* its natural width, centered */
.page { margin: 24px; } /* fills, with a margin around */CopyTransitions, and Back from the keyboard and mouse
<Pages stack={nav} transition:slide /> animates the page that enters. fade and scale also exist, and the duration can be given in milliseconds: transition:fade={150}. In <Sections>, the transition applies when the section changes and to the pages of each one.
Alt+Left and the mouse’s Back button go back to the previous page in the innermost visible stack. Alt+Left acts when the focus is inside the stack, and the mouse button when the press is over it. If an element of the page already uses Alt+Left, for example to move a card, the element wins.
Covered pages wait
While a page is covered, what it shows waits, as in {#hidden}: its texts, styles and lists are not recalculated when data changes, and they catch up once when the page is seen again. Its script keeps running, timers included. A page that polls a device or a camera should pause while covered: is_visible() is False there, and interval(ms, fn, when=is_visible()) stops counting.
Learn moreTimers ↗Real-time applications ↗
English documentation based on the JanelKit source. For additional details, browse the source docs ↗.