This section of the documentation is a simple collection of example code that can help you get a quick start on your application development. Most of these examples are categorized and provide you with a link to the working code example in the Sanic Repository
This section of the examples are a collection of code that provide a simple use case example of the sanic application.
A simple sanic application with a single async
method with text
and json
type response.
../../examples/teapot.py
../../examples/simple_server.py
Showcasing the simple mechanism of using sanic.viewes.HTTPMethodView
as well as a way to extend the same into providing a custom async
behavior for view
.
../../examples/simple_async_view.py
../../examples/redirect_example.py
Sanic
provides an easy to use way of redirecting the requests via a helper method called url_for
that takes a unique url name as argument and returns you the actual route assigned for it. This will help in simplifying the efforts required in redirecting the user between different section of the application.
../../examples/url_for_example.py
Sanic
provides an amazing feature to group your APIs and routes under a logical collection that can easily be imported and plugged into any of your sanic application and it's called blueprints
../../examples/blueprints.py
Even though Sanic
comes with a battery of Logging support it allows the end users to customize the way logging is handled in the application runtime.
../../examples/override_logging.py
The following sample provides an example code that demonstrates the usage of sanic.app.Sanic.middleware
in order to provide a mechanism to assign a unique request ID for each of the incoming requests and log them via aiotask-context.
../../examples/log_request_id.py
Sanic
framework comes with in-built support for streaming large files and the following code explains the process to setup a Sanic
application with streaming support.
../../examples/request_stream/server.py
Sample Client app to show the usage of streaming application by a client code.
../../examples/request_stream/client.py
Sanic
supports the ability to start an app with multiple worker support. However, it's important to be able to limit the concurrency per process/loop in order to ensure an efficient execution. The following section of the code provides a brief example of how to limit the concurrency with the help of asyncio.Semaphore
../../examples/limit_concurrency.py
Deploying a sanic
app via docker
and docker-compose
is an easy task to achieve and the following example provides a deployment of the sample simple_server.py
../../examples/Dockerfile
../../examples/docker-compose.yml
Sanic
provides an extendable bare minimum implementation of a global exception handler via sanic.handlers.ErrorHandler
. This example shows how to extend it to enable some custom behaviors.
../../examples/exception_monitoring.py
../../examples/logdna_example.py
../../examples/raygun_example.py
../../examples/rollbar_example.py
../../examples/sentry_example.py
The following sample code shows a simple decorator based authentication and authorization mechanism that can be setup to secure your sanic
api endpoints.
../../examples/authorized_sanic.py
Sanic
provides an ability to easily add a route and map it to a websocket
handlers.
../../examples/websocket.html
../../examples/websocket.py
../../examples/vhosts.py
The following example shows you how to get up and running with unit testing sanic
application with parallel test execution support provided by the pytest-xdist
plugin.
../../examples/pytest_xdist.py
The request
object in Sanic
is a kind of dict
object, this means that request
object can be manipulated as a regular dict
object.
../../examples/amending_request_object.py
For more examples and useful samples please visit the Huge-Sanic's GitHub Page