https://github.com/agx/flask-xstatic-files
Flask extension to serve xstatic packaged files
https://github.com/agx/flask-xstatic-files
flask flask-extensions
Last synced: 24 days ago
JSON representation
Flask extension to serve xstatic packaged files
- Host: GitHub
- URL: https://github.com/agx/flask-xstatic-files
- Owner: agx
- Created: 2017-12-21T09:04:44.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-10-09T07:33:39.000Z (over 7 years ago)
- Last Synced: 2025-09-06T22:05:14.485Z (9 months ago)
- Topics: flask, flask-extensions
- Language: Python
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
Awesome Lists containing this project
README
Flask-XStatic-Files
===================
.. image:: https://travis-ci.org/agx/flask-xstatic-files.svg?branch=master
:target: https://travis-ci.org/agx/flask-xstatic-files
.. 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.
Setup
-----
Upon initialization tell Flask about the XStatic modules you want to
use. This example uses JQuery and D3::
app = Flask(__name__)
app.config.from_object(__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//`` 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
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``::
@app.route('/somewhere-else/jquery/jquery.min.js')
def serve_jquery():
return xsf.serve_or_404('jquery', 'jquery.min.js')
.. _Flask: http://flask.pocoo.org/
.. _XStatic: https://xstatic.readthedocs.io/en/latest/
.. _JQuery: https://pypi.python.org/pypi/XStatic-jQuery