Skip to main content

Static file handling for fresco

Project description

Basic usage

First you need a fresco app. Here’s one I made earlier:

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 have multiple packages serving files through a single StaticFiles object without fear of conflict.

Now you can start configuring static source directories:

static.add_source('mypackage', 'static', cache_max_age=120)

The first two arguments are the name of your python package and the directory where the static content is, relative to the package root.

The cache_max_age argument specifies how long (in seconds) browsers and proxies can cache responses. For development you might want to omit this, but in production use you should set this to a reasonable value and configure a caching HTTP proxy server.

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:

<img src="{{ static('mypackage/photo.jpg') }}" alt="My photo" />

0.1

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fresco-static-0.1.tar.gz (8.2 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page