Back to Examples Run Example

examples/session_counter.py

from py4web import action

from .common import session


@action("session_counter")
@action.uses("examples/session_counter.html", session)
def session_counter():
    session["counter"] = session.get("counter", 0) + 1
    return {"counter": session.get("counter")}

templates/examples/session_counter.html

[[extend "layout.html"]]

<h2 class="subtitle">Counter value: [[=counter]]</h2>

<a role="button" href="[[=URL('session_counter')]]">Click to reload and increment counter</a>
<a role="button" href="[[=URL('session_clear')]]">Click to reload and reset counter</a>