Skip to content

Commit

Permalink
Create example & update docs for multi-page navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwillchen committed Feb 6, 2024
1 parent 7077e7d commit e3e6191
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
18 changes: 16 additions & 2 deletions docs/guides/pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ You can define multi-page Mesop applications by using the page feature you learn

## Simple, 1-page setup

If you want to create a simple Mesop app, you can do the following:
To create a simple Mesop app, you can use `me.page()` like this:

```python
import mesop as me
Expand All @@ -14,10 +14,12 @@ def foo():
me.text("bar")
```

This is the same as the following example which more explicitly sets the route to `"/"`.
> NOTE: If you do not provide a `path` argument, then it defaults to the root path `"/"`.
## Explicit 1-page setup

This is the same as the above example which explicitly sets the route to `"/"`.

```python
import mesop as me

Expand All @@ -39,3 +41,15 @@ def page1():
def page2():
me.text("page 2")
```

## Navigation

If you have multiple pages, you will typically want to navigate from one page to another when the user clicks a button. You can use `me.navigate("/to/path")` to navigate to another page.

**Example:**

```python
--8<-- "mesop/examples/docs/multi_page_nav.py"
```

> Note: you can re-use state across pages. See how the above example uses the `State#count` value across pages.
1 change: 1 addition & 0 deletions mesop/examples/docs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from mesop.examples.docs import counter as counter
from mesop.examples.docs import hello_world as hello_world
from mesop.examples.docs import loading as loading
from mesop.examples.docs import multi_page_nav as multi_page_nav
from mesop.examples.docs import streaming as streaming
23 changes: 23 additions & 0 deletions mesop/examples/docs/multi_page_nav.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import mesop as me


def on_click(e: me.ClickEvent):
state = me.state(State)
state.count += 1
me.navigate("/multi_page_nav/page_2")


@me.page(path="/multi_page_nav")
def main_page():
me.button("Navigate to Page 2", on_click=on_click)


@me.page(path="/multi_page_nav/page_2")
def page_2():
state = me.state(State)
me.text(f"Page 2 - count: {state.count}")


@me.stateclass
class State:
count: int

0 comments on commit e3e6191

Please sign in to comment.