Skip to main content

fresco-static - static file serving for fresco

Basic usage

Installation:

pip install fresco-static

# If you want brotli compression enabled
pip install fresco-static[brotli]

Simple configuration:

from fresco import FrescoApp
from fresco_static import serve_directory, serve_file

app = FrescoApp()

# Serve a directory
app.route_all("/static", ALL=serve_directory("path/to/static/files"))

# Serve a single file
app.route("/favicon.ico"", ALL=serve_file("path/to/favicon.ico"))

# Serve a directory that's part of a Python package
app.route_all(
    "/static",
    ALL=serve_package_directory("mypackage", "static/files")
)

Advanced options:

app = FrescoApp()
app.route_all(
    "/static",
    ALL=serve_directory(
        "path/to/static/files",

        # Specify custom headers. These will be set for all files served.
        headers={"cache-control": "max-age 86400"},

        # Compression algorithms to offer, in order of preference
        compression="brotli,gzip"

        # Directory to cache compressed copies of files. The same directory may
        # safely be shared between instances
        cachedir="/tmp/staticfilecache"
    )
)

App-wide usage

These instructions are for creating a single StaticFiles object that serves static requests across the entire application.

Create a fresco app:

app = FrescoApp()

Let’s assume this lives in an application that’s structured like this:

mypackage
├── __init__.rst
├── app.py
├── static
│   └── photo.jpg
└── setup.py

Once you have the app, you can create an instance of StaticFiles:

static = StaticFiles(app)

StaticFiles automatically adds routes for the path /static. Static files will be available under /static/<packagename>/<path> eg /static/mypackage/photo.jpg. You can change these defaults:

static = StaticFiles(app,
                     prefix='/media',
                     route_name='media',
                     cache_max_age=3600)

You can have multiple packages serving files through a single StaticFiles object without fear of conflict.

Now you can start configuring static source directories:

# Mount directory '/www/mysite/htdocs'. The first argument is an arbitrary
# name used to identify this source. You can use whatever string you like.
static.add_directory('site-htdocs', '/site/htdocs', cache_max_age=60)

# Serve files located in a 'subdir' directory within the python package
# 'mypackage'
static.add_package('mypackage', 'subdir', cache_max_age=86400)

The cache_max_age argument specifies for how long (in seconds) browsers and proxies can cache responses. For development you might want to set this to zero, but in production use you should set this to a reasonable value and configure a caching HTTP proxy server. When adding source directories you can omit this argument, and the default (configured when you created the StaticFiles object) will be used instead.

static.pathfor generates URLs for static content. You will probably want to include this in your templating system’s default namespace. How you do that depends on how you’ve integrated the templating system, but it would typically be something like this:

templating.contextprocessor({'static': static.pathfor})

Call this in templates to link to static files, eg:

<!-- Reference a file from the "site-htdocs" source
-->
<img src="{{ static('site-htdocs/photo.jpg') }}" alt="My photo" />

<!-- Reference a file from the "mypackage" source
-->
<img src="{{ static('mypackage/photo.jpg') }}" alt="My photo" />

<!-- Path doesn't begin with a source name — all sources will be
     searched for a matching file
-->
<img src="{{ static('cat-pictures/miaow.gif') }}" alt="My photo" />

0.5.1 (released 2026-11-15)

  • Fixed brotli compression

0.5 (released 2024-10-16)

  • Added serve_package_directory function

  • Improved support for HEAD requests

0.4 (released 2024-07-11)

  • Added view-based integration

0.3 (released 2024-05-02)

  • Added Python 3.12 support

  • Dropped support for Python 3.8 and older

0.2 (released 2018-08-01)

  • Source names are no longer required to be python identifiers

0.1

  • Initial release

Metadata

Release files for fresco-static 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fresco-static 0.5.1
File Size Uploaded Provenance Actions
fresco_static-0.5.1.tar.gz 15.2 kB Without provenance Details for fresco_static-0.5.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fresco-static 0.5.1
File Interpreter ABI Platform Actions
fresco_static-0.5.1-py3-none-any.whl Python 3 none any Details for fresco_static-0.5.1-py3-none-any.whl

Total release size: 29.5 kB

Release files / fresco_static-0.5.1.tar.gz

Download URL fresco_static-0.5.1.tar.gz
Size 15.2 kB
Tags Source
SHA-256 checksum
How to use checksums
43ed0f5dd0b18e94a00f925bcf6c5f4744b39120d4a9069e2d120f3008229235
BLAKE2b-256 checksum
How to use checksums
11e3dfd993257598a7791442e7abd9d55c86b7d414ac136b1b5ef248f1161959
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.15

Release files / fresco_static-0.5.1-py3-none-any.whl

Download URL fresco_static-0.5.1-py3-none-any.whl
Size 14.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0d3c62a8bebe187bc68537147873eccd14840b00727e12e925f59bfaf67d56c7
BLAKE2b-256 checksum
How to use checksums
9467ee6987128f28ce4f45657efb49d748f69f721b25b2ca94f517c8a958fd4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.15

Security for fresco-static 0.5.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page