Movatterモバイル変換


[0]ホーム

URL:


Skip to content
❤️ Support Starlette viasponsors! 📚 Do you like the new docs?Let us know!

Applications

API Reference

starlette.applications.Starlette

Creates an Starlette application.

Parameters:

Starlette includes an application classStarlette that nicely ties together all ofits other functionality.

fromcontextlibimportasynccontextmanagerfromstarlette.applicationsimportStarlettefromstarlette.responsesimportPlainTextResponsefromstarlette.routingimportRoute,Mount,WebSocketRoutefromstarlette.staticfilesimportStaticFilesdefhomepage(request):returnPlainTextResponse('Hello, world!')defuser_me(request):username="John Doe"returnPlainTextResponse('Hello,%s!'%username)defuser(request):username=request.path_params['username']returnPlainTextResponse('Hello,%s!'%username)asyncdefwebsocket_endpoint(websocket):awaitwebsocket.accept()awaitwebsocket.send_text('Hello, websocket!')awaitwebsocket.close()@asynccontextmanagerasyncdeflifespan(app):print('Startup')yieldprint('Shutdown')routes=[Route('/',homepage),Route('/user/me',user_me),Route('/user/{username}',user),WebSocketRoute('/ws',websocket_endpoint),Mount('/static',StaticFiles(directory="static")),]app=Starlette(debug=True,routes=routes,lifespan=lifespan)

Storing state on the app instance

You can store arbitrary extra state on the application instance, using thegenericapp.state attribute.

For example:

app.state.ADMIN_EMAIL='[email protected]'

Accessing the app instance

Where arequest is available (i.e. endpoints and middleware), the app is available onrequest.app.


[8]ページ先頭

©2009-2026 Movatter.jp