Skip to content

StaticFilesConfig

StaticFilesConfig is simple set of configurations that when passed enables the built-in of Esmerald. When a StaticFilesConfig object is passed to an application instance, it will enable the static files serving.

Check

StaticFiles are considered an app and they are pure Lilya app, so using Lilya StaticFiles will also work with Esmerald.

StaticFilesConfig and application

To use the StaticFilesConfig in an application instance.

from pathlib import Path

from esmerald import Esmerald, StaticFilesConfig

static_files_config = StaticFilesConfig(
    path="/static", packages=["mypackage"], directory=Path("static")
)

app = Esmerald(static_files_config=static_files_config)

Another example

from pathlib import Path

from esmerald import Esmerald, StaticFilesConfig

static_files_config = StaticFilesConfig(
    path="/static", packages=["mypackage"], directory=Path("static")
)

app = Esmerald(static_files_config=static_files_config)

With Packages and directory:

from pathlib import Path

from esmerald import Esmerald, StaticFilesConfig

static_files_config = StaticFilesConfig(
    path="/static", packages=["mypackage"], directory=Path("static")
)

app = Esmerald(static_files_config=static_files_config)

Parameters

All the parameters and defaults are available in the StaticFilesConfig Reference.

StaticFilesConfig and application settings

The StaticFilesConfig can be done directly via application instantiation but also via settings.

from pathlib import Path

from esmerald import EsmeraldAPISettings, StaticFilesConfig


class CustomSettings(EsmeraldAPISettings):
    @property
    def static_files_config(self) -> StaticFilesConfig:
        """
        Simple configuration indicating where the statics will be placed in
        the application.
        """
        return StaticFilesConfig(path="/static", packages=["mypackage"], directory=Path("static"))

This will make sure you keep the settings clean, separated and without a bloated Esmerald instance.

Multiple directories and multiple pathes (without fallthrough)

Imagine, for example, you have multiple directories you would like to access including a node_modules/ one. This is possible do do it by passing multiple StaticFilesConfig configurations and shown below:

from pathlib import Path

from esmerald import Esmerald, StaticFilesConfig

static_files_config = StaticFilesConfig(
    path="/static", packages=["mypackage"], directory=Path("static")
)

static_files_node_modules_config = StaticFilesConfig(
    path="/static/node_modules", directory=Path("node_modules")
)

app = Esmerald(static_files_config=[static_files_node_modules_config, static_files_config])
The advantage is a fine granular configuration. Different options and packages can be set.

Note

The first path match is used and there is currently no fallthrough in case no file is found, so the order is very important.

Multiple directories with fallthrough

Designers may want to provide overwrites to static files or have fallbacks. In the former example this wasn't possible. From the newest version of Lilya (0.11.5+) it is possible to provide multiple directories to lilya and get such a behavior

from pathlib import Path

from esmerald import Esmerald, StaticFilesConfig

static_files_config = StaticFilesConfig(
    path="/static", directory=["static/overwrites", "static", "static/defaults", "node_modules"]
)

app = Esmerald(static_files_config=static_files_config)

Both ways can be mixed.