Skip to main content

Easily use XStatic files in flask

Project description


.. image::

.. highlight:: python

A `Flask`_ extionsion to serve `XStatic`_ files. Can be useful if you
don't use an asset pipeline and want to serve e.g. XStatic packaged
javascript files like `JQuery`_ directly.

Upon initialization tell Flask about the XStatic modules you want to
use. This example uses JQuery and D3::

app = Flask(__name__)
app.config.['XSTATIC_MODULES'] = ['jquery', 'd3']
xsf = XStaticFiles(app)

The modules can be given as list or comma separated (``jquery,d3``).

Serving Files
The extension already installs a route handler for you so files under
``/xstatic/<module>/`` are served automatically. To generate these
URLs you can use ``url_for``::

xsf.url_for('jquery', 'jquery.min.js')

In templates you can use ``xstatic_url_for``:

.. code-block:: html

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

to generate these URLs. 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.

In case you want to serve XStatic files from other URLs use ``serve``
or ``serve_or_404``::

def serve_jquery():
return xsf.serve_or_404('jquery', 'jquery.min.js')

.. _Flask:
.. _XStatic:
.. _JQuery:

Project details

Download files

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

Files for Flask-XStatic-Files, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size Flask-XStatic-Files-0.0.2.tar.gz (3.6 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page