# Sanic Application

# Instance

The most basic building block is the Sanic() instance. It is not required, but the custom is to instatiate this in a file called server.py.

# /path/to/server.py

from sanic import Sanic

app = Sanic("My Hello, world app")

# App Registry

When you instantiate a Sanic instance, that can be retrieved at a later time from the Sanic app registry. This can be useful, for example, if you need to access your Sanic instance from a location where it is not otherwise accessible.

# ./path/to/server.py
from sanic import Sanic

app = Sanic("my_awesome_server")

# ./path/to/somewhere_else.py
from sanic import Sanic

app = Sanic.get_app("my_awesome_server")

If you call Sanic.get_app("non-existing") on an app that does not exist, it will raise SanicException by default. You can, instead, force the method to return a new instance of Sanic with that name.

app = Sanic.get_app(
    "non-existing",
    force_create=True,
)

# Configuration

Sanic holds the configuration in the config attribute of the Sanic instance. Configuration can be modified either using dot-notation OR like a dictionary.

app = Sanic('myapp')

app.config.DB_NAME = 'appdb'
app.config['DB_USER'] = 'appuser'

db_settings = {
    'DB_HOST': 'localhost',
    'DB_NAME': 'appdb',
    'DB_USER': 'appuser'
}
app.config.update(db_settings)

Heads up

Config keys should be uppercase. But, this is mainly by convention, and lowercase will work most of the time.

app.config.GOOD = "yay!"
app.config.bad = "boo"

There is much more detail about configuration later on.

MIT Licensed | Copyright © 2018-present Sanic Community Organization