Static files

Bocadillo uses WhiteNoise to serve static assets for you in an efficient manner.

Basic usage

Place files in the static folder at the root location, and they will be available at the corresponding URL:

/* static/css/styles.css */
h1 {
  color: red;
}
curl "http://localhost:8000/static/css/styles.css"
h1 { color: red; }

Static files location

By default, static assets are served at the /static/ URL root and are searched for in a static/ directory relative to where the app is executed. For example:

.
├── static
│   └── css
│       └── styles.css
└── myproject
    ├── app.py
    └── settings.py

You can modify the static files directory using the STATIC_DIR setting:

# myproject/settings.py
STATIC_DIR = "staticfiles"

To modify the root URL path, use the STATIC_ROOT setting:

# myproject/settings.py
STATIC_ROOT = "assets"

If the STATIC_DIR does not exist, Bocadillo won't attempt to serve assets from it, and no errors/warnings will be raised.

Extra static files directories

You can serve extra static directories using app.mount() and the static helper:

from bocadillo import static

app.mount(prefix="assets", app=static("assets"))

WhiteNoise configuration

You can pass any extra WhiteNoise configuration attributes via the STATIC_CONFIG setting.

For example, to set the time browsers and proxies should cache files to 30 seconds, use:

# myproject/settings.py
STATIC_CONFIG = {"max_age": 30}

Disabling static files

To prevent Bocadillo from serving static files altogether, use:

# myproject/settings.py
STATIC_DIR = None