StaticFiles¶
Lilya provides a convenient StaticFiles
class for serving files from a specified directory:
Parameters¶
directory
- A string or os.PathLike indicating the directory path. You can also provide a list or tuple for using multiple directories.packages
- A list of strings or list of tuples of strings representing Python packages.html
- Operate in HTML mode, automatically loadingindex.html
for directories if it exists.check_dir
- Ensure that the directory exists upon instantiation. Defaults toTrue
.follow_symlink
- A boolean indicating whether symbolic links for files and directories should be followed. Defaults toFalse
.
from lilya.apps import Lilya
from lilya.routing import Include
from lilya.staticfiles import StaticFiles
app = Lilya(routes=[
Include('/static', app=StaticFiles(directory='static'), name="static"),
])
For requests that do not match, static files will respond with "404 Not Found" or "405 Method Not Allowed" responses.
In HTML mode, if a 404.html
file exists, it will be displayed as the 404 response.
As directory also a tuple or list can be provided. This is useful for overwrites or multiple directories which should served under the same
location.
You can theoretically also provide multiple StaticFiles
but sacrifces the fallthrough behaviour this way.
from lilya.apps import Lilya
from lilya.routing import Include
from lilya.staticfiles import StaticFiles
app = Lilya(routes=[
Include('/static', app=StaticFiles(directory=['static/overwrites', 'static', 'static/fallback']), name="static"),
])
The packages
option allows inclusion of "static" directories from within a Python package.
The Python "bootstrap4" package serves as an example.
from lilya.apps import Lilya
from lilya.routing import Include
from lilya.staticfiles import StaticFiles
app = Lilya(routes=[
Include('/static', app=StaticFiles(directory='static', packages=['bootstrap4']), name="static"),
])
By default, StaticFiles
looks for the statics
directory in each package. You can modify the default directory by specifying a tuple of strings.
from lilya.apps import Lilya
from lilya.routing import Include
from lilya.staticfiles import StaticFiles
app = Lilya(routes=[
Include('/static', app=StaticFiles(directory='static', packages=[('bootstrap4', 'static')]), name="static"),
])
While you may choose to include static files directly within the "static" directory, using Python packaging to include static files can be beneficial for bundling reusable components.