Skip to main content

Adds server-side session support to your Starlette application

Project description

starlette-sessions

Adds server-side session support to your Starlette application

License

MIT License

Contributors

Installing

# with redis support, for example

# from source
poetry install --extras redis

Quick Start

from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.routing import Route
import uvicorn

from starlette_sessions.middleware import SessionMiddleware
from starlette_sessions.cookie.backend import CookieSessionBackend
from starlette_sessions.signedcookie.backend import SignedCookieSessionBackend
from starlette_sessions.memory.backend import MemorySessionBackend
from starlette_sessions.redis.backend import RedisSessionBackend


async def homepage(request: Request):
    return JSONResponse({"hello": "world"})


async def session_set(request: Request):
    counter = request.session.get("counter")
    response = JSONResponse({"counter": counter})
    request.session["counter"] = (counter or 0) + 1
    return response


async def session_read(request: Request):
    return JSONResponse(
        {k: v for k, v in request.session.items()}
    )


async def session_clear(request: Request):
    counter = request.session.get("counter")
    response = JSONResponse({"counter": counter})
    request.session.clear()
    return response


async def session_unsafe(request: Request):
    try:
        counter = request.session["counter"]
    except KeyError:
        counter = None
    return JSONResponse({"counter": counter})


#
# try out these backends:
#
session_backend = CookieSessionBackend()
# session_backend = SignedCookieSessionBackend(secret_key="s3cr3t", max_age=30)
# session_backend = MemorySessionBackend()
# session_backend = RedisSessionBackend(ttl=30)


app = Starlette(
    debug=True,
    routes=[
        Route("/", homepage),
        Route("/set", session_set),
        Route("/read", session_read),
        Route("/clear", session_clear),
        Route("/unsafe", session_unsafe),
    ],
    middleware=[
        Middleware(SessionMiddleware, backend=session_backend),
    ]
)

uvicorn.run(app, host="0.0.0.0", port=8080)

Development

# setup
poetry install --with dev --all-extras

# linting
poetry run ruff src tests

# formatting
poetry run black src tests
poetry run isort src tests

# type checking
poetry run mypy src tests

# testing
poetry run pytest -s tests/unit

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

starlette_sessions-0.3.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

starlette_sessions-0.3.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file starlette_sessions-0.3.0.tar.gz.

File metadata

  • Download URL: starlette_sessions-0.3.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for starlette_sessions-0.3.0.tar.gz
Algorithm Hash digest
SHA256 eb3700c7903b88154c867526f39da1408439c671311c9c371fed539f3e14986b
MD5 117d265bed3f3e2526049541b6327016
BLAKE2b-256 36c5dd8c349a0ea99030918c5a29856905178691e3b7fdb55e0259161ed54b66

See more details on using hashes here.

Provenance

The following attestation bundles were made for starlette_sessions-0.3.0.tar.gz:

Publisher: release-pypi.yml on nzjess/starlette-sessions

Attestations:

File details

Details for the file starlette_sessions-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for starlette_sessions-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 03750f1c5d31f38996c4ff3678ff4866f948258197d258e34f6c327d9527144f
MD5 367794e6b9456c89010fd7f901e7b799
BLAKE2b-256 5c0d0e8861d52715e24d6af71eceea374ed86fc2c44c137c910001252db8d45d

See more details on using hashes here.

Provenance

The following attestation bundles were made for starlette_sessions-0.3.0-py3-none-any.whl:

Publisher: release-pypi.yml on nzjess/starlette-sessions

Attestations:

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page