Skip to main content

Easily use XStatic files in flask

Project description

https://travis-ci.org/agx/flask-xstatic-files.svg?branch=master

A flask extionsion to serve xstatic files. Can be useful if you don’t use an asset pipeline and server e.g. xstatic packaged javascript files directly.

Setup

Upon initialization tell flask about the xstatic modules you want to use:

app = Flask(__name__)
app.config.from_object(__name__)
app.config.update(dict(
    XSTATIC_MODULES="jquery,d3",
))
xsf = XStaticFiles(app)

Serving files

Install a minimal route handler:

@app.route('/xstatic/<module>/<path:filename>')
def xstatic(module, filename):
    return xsf.serve_or_404(module, filename)

In your templates you can then use xstatic_url_for:

<script type=text/javascript src="{{ xstatic_url_for(module='jquery', path='jquery.min.js') }}"></script>

This has the advantage that you can later serve files from a static web server by adjusting ‘XSTATIC_ROOT’ and ‘XSTATIC_PROTO’ without having to modify any code.

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

Flask-XStatic-Files-0.0.1.tar.gz (3.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