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:

.
├── app.py
└── static
    └── css
        └── styles.css

You can modify the static files directory using the static_dir option:

app = App(static_dir='staticfiles')

To modify the root URL path, use static_root:

app = App(static_root='assets')

TIP

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

Extra static files directories

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

from bocadillo import App, static

app = App()

# Serve more static files located in the assets/ directory
app.mount(prefix='assets', app=static('assets'))

WhiteNoise configuration 0.12.2+

You can pass any extra WhiteNoise configuration attributes via the static_config parameter.

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

app = App(static_config={"max_age": 30})

Disabling static files

To prevent Bocadillo from serving static files altogether, you can use:

app = App(static_dir=None)
Last Updated: 3/30/2019, 10:34:39 AM