App Mode
App mode gives you multi-page routing, per-session state, and a structured way to build larger applications.
Basic App
import webwrench as ww
app = ww.App()
@app.page("/")
def home(ctx):
ctx.title("Home")
ctx.text("Welcome to the home page!")
@app.page("/about")
def about(ctx):
ctx.title("About")
ctx.text("This is the about page.")
app.serve()
The PageContext Object
Each page handler receives a ctx (PageContext) argument. It provides the same API as module-level functions:
| Script Mode | App Mode |
|---|---|
ww.title("Hi") | ctx.title("Hi") |
ww.chart(data) | ctx.chart(data) |
ww.slider("x") | ctx.slider("x") |
ww.theme("dark") | ctx.set_theme("dark") |
Per-Session State
Use ctx.state to store data that persists across interactions within a session:
@app.page("/counter")
def counter(ctx):
ctx.title("Counter")
if not hasattr(ctx.state, "count"):
ctx.state.count = 0
display = ctx.text(f"Count: {ctx.state.count}")
btn = ctx.button("Increment")
@btn.on_click
def clicked():
ctx.state.count += 1
display.update(f"Count: {ctx.state.count}")
Navigation Between Pages
@app.page("/")
def home(ctx):
ctx.title("Home")
ctx.text("Welcome!")
ctx.button("Go to Dashboard").on_click = lambda: ctx.redirect("/dashboard")
@app.page("/dashboard")
def dashboard(ctx):
ctx.title("Dashboard")
ctx.chart([10, 20, 30], type="bar", labels=["A", "B", "C"])
When to Use App Mode
- Multi-page applications with routing
- Apps that need per-session state
- Production dashboards with multiple views
Note: Script mode and app mode are mutually exclusive. Don't mix
ww.title() with @app.page() in the same script.