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.