Serving Static Files

Xyra can efficiently serve static files like CSS, JavaScript, and images, which are essential for your application's frontend.

Serving Static Files

Xyra allows you to serve static files such as images, CSS, and JavaScript using the app.static_files() method. This method maps a URL path to a directory on your filesystem.

Python
from xyra import App

app = App()

# Serve files from the "public" directory under the "/static" URL path
app.static_files("/static", "public")

With this configuration, a file at public/css/style.css will be accessible at http://localhost:8000/static/css/style.css.

Multiple Static Directories

You can serve multiple static directories by calling app.static_files() multiple times with different paths.

Python
from xyra import App

app = App()

# Serve CSS and JS from different directories
app.static_files("/css", "public/css")
app.static_files("/js", "public/js")
app.static_files("/images", "public/images")

This setup allows for better organization of your static assets.