Integrates the webassets library with Flask, adding support for merging, minifying and compiling CSS and Javascript files.
- Documentation:
- https://flask-assets.readthedocs.io/
Integrates the webassets library with Flask, adding support for merging, minifying and compiling CSS and Javascript files.
Hello there, I think that webassets and the related Flask module are awesome and just had a few questions about the way files are read and structured within a Flask project.
Suppose we wish to keep track of 3 item types:
I think a suitable directory structure would probably look something like this:
flask_project/assets/less/... <- (1) Non-compiled assets
flask_project/assets/coffee/... <- (1) Non-compiled assets
flask_project/static/js/... <- (2) Compiled assets
flask_project/static/js/... <- (3) 3rd Party Libraries
flask_project/static/css/... <- (2) Compiled assets
flask_project/static/css/... <- (3) 3rd Party Libraries
I have been experimenting with Flask-Assets and can't seem to find a way to achieve this without doing something like this in the template:
{% assets filters="less", output="css/style.css",
"../assets/less/style.less", "../assets/less/import.less" %}
<link rel="stylesheet" type="text/css" media="screen" href="{{ ASSET_URL }}" />
{% endassets %}
I also attempted to use the ASSETS_LOAD_PATH config item but didn't have any luck.
Is there any way to specify the source directory independently from the destination directory?
Also another little question, is the cache really needed? For example, I pictured that it would work as follows.
Please let me know your thoughts and if I'm missing something :)
Thank you so much and keep up the great work! Fotis
Fixes the flask-assets ManageCommand for building. The Environment instance found is the environment itself, which does not have an attribute "environment". If I am simply using this wrong (i have tried letting it find my environment as well) please let me know
I have other packages in my environment having a requirement for the webassets package. These upgrade webassets to 0.9. And then while running pip installs with the --upgrade flag, I face an error which says:
error: Installed distribution webassets 0.9 conflicts with requirement webassets==0.8
On deeper probe, I figured it is flask-assets which has a specified dependency on webassets==0.8.
vagrant@dev:/usr/local/lib/python2.7/dist-packages$ sudo grep -r ==0.8 *
Flask_Assets-0.8-py2.7.egg/EGG-INFO/requires.txt:webassets==0.8
Any particular problem that holds flask-assets back from supporting webassets latest version?
As far as I can see the Flask-Script 0.6 is not supported. Running command results in
Traceback (most recent call last):
File "manage.py", line 15, in <module>
manager.run()
File "/blah/blah/blah/python2.7/site-packages/flask_script/__init__.py", line 366, in run
raise e
TypeError: create_parser() got an unexpected keyword argument 'parents'
Heya, this references issue #19. I thought I'd submit this pull request, which adds a Jinja2TemplateFilter
to flask-assets (exposed under the name jinja2template
). It is automatically registered when flask_assets is imported, so it should be usable out of the box.
This obviously requires the latest development release of webassets
to run (since I just got my initial jinja2
filter accepted there), so I'm not sure if you'd like to merge this now, or wait till webassets
makes a new stable release.
Either way, I thought I'd leave this here to see what you think.
Currently the latest stable release is not compatible with how Flask handles the exception, see this commit: 006864bc969cdc17d879635f94683d4d374305d1.
So we can’t use ManageAsset() with Flask-Script for example.
The fix is already present in master, when will you push the next major version?
In response to Issue #45, this commit adds support for use with the Flask S3 extension.
To use, add a config variable FLASK_ASSETS_USE_S3
and set it to True
, for example
app.config['FLASK_ASSETS_USE_S3'] = True
A full example can be seen here.
Small caveat: Anytime you change a bundle, you must run something to the effect of
from app import app
import flask_s3
flask_s3.create_all(app)
in order for the new changes to be pushed to S3. It is also recommended to use versioning within the filename rather than the query string when using with CloudFront, (you can tell CloutFront to recognize unique query strings, but it's not recommended), as is done in the above example. Also note that if you want to use CloutFront, you will need to install Flask S3 from my fork until this pull request is merged in.
If you setup your Flask installation as describe here: http://flask.pocoo.org/docs/patterns/packages/#modules-and-resources, flask-assets isn't able to find static files stored in the module's static directory.
I am getting the following error when I try to build assets using the --parse-templates
option. Im not really sure where to start debugging this...
Searching templates...
Traceback (most recent call last):
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/jinja2/nodes.py", line 386, in as_const
return f(self.left.as_const(eval_ctx), self.right.as_const(eval_ctx))
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/jinja2/nodes.py", line 365, in as_const
raise Impossible()
jinja2.nodes.Impossible
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/jinja2/nodes.py", line 386, in as_const
return f(self.left.as_const(eval_ctx), self.right.as_const(eval_ctx))
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/jinja2/nodes.py", line 388, in as_const
raise Impossible()
jinja2.nodes.Impossible
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 110, in <module>
manager.run()
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/flask_script/__init__.py", line 412, in run
result = self.handle(sys.argv[0], sys.argv[1:])
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/flask_script/__init__.py", line 383, in handle
res = handle(*args, **config)
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/flask_script/commands.py", line 216, in __call__
return self.run(*args, **kwargs)
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/flask_assets.py", line 422, in run
return impl.main(args)
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/webassets/script.py", line 557, in main
return self.run_with_argv(argv)
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/webassets/script.py", line 549, in run_with_argv
return self.run_with_ns(ns)
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/webassets/script.py", line 531, in run_with_ns
env = self._setup_assets_env(ns, log)
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/flask_assets.py", line 364, in _setup_assets_env
env.add(*[b for b in self.load_from_templates(env, ns.jinja_extension)
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/flask_assets.py", line 391, in load_from_templates
load_bundles()
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/webassets/ext/jinja2.py", line 218, in load_bundles
bundles.extend(self.with_file(filename, self._parse) or [])
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/webassets/loaders.py", line 301, in with_file
return then_run(filename, contents)
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/webassets/ext/jinja2.py", line 246, in _parse
_recurse_node(node)
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/webassets/ext/jinja2.py", line 244, in _recurse_node
_recurse_node(node)
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/webassets/ext/jinja2.py", line 237, in _recurse_node
*AssetsExtension.resolve_contents(files.as_const(), self.asset_env),
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/jinja2/nodes.py", line 490, in as_const
return [x.as_const(eval_ctx) for x in self.items]
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/jinja2/nodes.py", line 490, in <listcomp>
return [x.as_const(eval_ctx) for x in self.items]
File "/Users/joe/Sites/_projects/words/venv3/lib/python3.4/site-packages/jinja2/nodes.py", line 388, in as_const
raise Impossible()
jinja2.nodes.Impossible
@miracle2k This has gotten a bit out of hand. There are 3 open issue open that have to do with the inability to get the correct version of of webassets. Moreover, it has made it quite difficult to get new installations up and breaks builds.
The cleanest solution is to eliminate the parse_version
function in setup.py entirely and simply update the specific version in setup.py as things progress. As stated in the Zen of Python, "Explicit is better than implicit", "Simple is better than complex", and "Readability counts". Unfortunately, right now I think this setup is in contradiction with each of these.
Before version 0.8, flask used flaskext
namespace:
http://flask.pocoo.org/docs/extensions/#flask-before-0-8
But with 0.8 this behaviour was changed.
Probably, major version's increment needed. Otherwise, backward compatibility could be added, using:
import flaskext_compat
flaskext_compat.activate()
from flask.ext import script
Looks like flask-assets will not work in flask-2.3 and on:
.venv/lib/python3.11/site-packages/flask_assets.py:8
.venv/lib/python3.11/site-packages/flask_assets.py:8
/Users/patrick/dev/triplynx3/.venv/lib/python3.11/site-packages/flask_assets.py:8: DeprecationWarning: '_request_ctx_stack' is deprecated and will be removed in Flask 2.3.
from flask import _request_ctx_stack, current_app
.venv/lib/python3.11/site-packages/flask_assets.py:317: 11 warnings
tests/test_home.py: 239 warnings
/Users/patrick/dev/triplynx3/.venv/lib/python3.11/site-packages/flask_assets.py:317: DeprecationWarning: '_request_ctx_stack' is deprecated and will be removed in Flask 2.3. Use 'g' to store data, or 'request_ctx' to access the current context.
ctx = _request_ctx_stack.top
.venv/lib/python3.11/site-packages/flask_assets.py:322: 22 warnings
/Users/patrick/dev/triplynx3/.venv/lib/python3.11/site-packages/flask_assets.py:322: DeprecationWarning: '_app_ctx_stack' is deprecated and will be removed in Flask 2.3.
from flask import _app_ctx_stack
.venv/lib/python3.11/site-packages/flask_assets.py:323: 11 warnings
/Users/patrick/dev/triplynx3/.venv/lib/python3.11/site-packages/flask_assets.py:323: DeprecationWarning: '_app_ctx_stack' is deprecated and will be removed in Flask 2.3. Use 'g' to store data, or 'app_ctx' to access the current context.
app_ctx = _app_ctx_stack.top
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
I'm using the library with the template only method:
{% assets filters="jsmin", output="gen/packed.js",
"common/jquery.js", "site/base.js", "site/widgets.js" %}
<script type="text/javascript" src="{{ ASSET_URL }}"></script>
{% endassets %}
But I'm having an issue with the jsmin library stripping whitespace in my template strings, so that this is a test
is turned into thisisatest
. The jsmin library has the quote_chars=quote_chars="'\"`" parameter that should fix this, is there a way to pass the parameter to the template?
Would be great if flask-assets could be statically type checked with: https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-type-hints-for-third-party-library
According to the flask-assets documentation about blueprints, a path prefix is used to denote the use of blueprint-specific static folders when defining bundles. However, this caused issues for my project, because I'm using a single shared {APP_ROOT}/static
directory for all blueprints, in which I also have subdirectories; so if there's a subdirectory with the same name as a blueprint, flask-assets would give me an TypeError: The referenced blueprint <flask.blueprints.Blueprint object at ...> has no static folder.
, because it would look for a blueprint-specific static folder {BLUEPRINT_ROOT}/static
. In my case, I had a 'debug' blueprint for testing purposes and a 'debug' subdirectory within my {APP_ROOT}/static
.
The doc states
If you are using Flask blueprints, you can refer to a blueprint’s static files via a prefix, in the same way as Flask allows you to reference a blueprint’s templates
but I'm not sure the statement about Flask's way of referencing templates is (still) true; when I reference templates in my project, they're always interpreted as paths beneath the app root template directory or the specific blueprint template directory, so the blueprint name is never part of the template path either way.
The ambiguity of the first part of a bundle source path being interpreted as either a blueprint (but only if a blueprint of that name exists) or a subdirectory seems confusing to me. There might be ways to entirely circumvent that using flask-assets or Flask configurations or even the right parameters for the bundle, but I couldn't find what I was looking for in the docs. I'm sorry if I missed a solution that I could I have gotten by looking into it more carefully.
I'm not sure if there's a better way to do this (maybe a blueprint
keyword argument in the Bundle
constructor, which would apply to all input and output files though, or some other way to denote the use of blueprints on a file-by-file base), but I just wanted to point out that this can lead to some confusion or potentially to unnecessary limitations on how to name things in the project.
Flask.static_path
has been removed in Flask 1.0. See pallets/flask#2385.
Also, Flask.static_folder
does not work as expected when the path ends in a trailing slash. See pallets/flask#3452.
Flask-Pydantic Flask extension for integration of the awesome pydantic package with Flask. Installation python3 -m pip install Flask-Pydantic Basics v
Flask-SocketIO Socket.IO integration for Flask applications. Installation You can install this package as usual with pip: pip install flask-socketio
Flask-SocketIO Socket.IO integration for Flask applications. Installation You can install this package as usual with pip: pip install flask-socketio
Flask-Bcrypt Flask-Bcrypt is a Flask extension that provides bcrypt hashing utilities for your application. Due to the recent increased prevelance of
Flask-Rebar Flask-Rebar combines flask, marshmallow, and swagger for robust REST services. Features Request and Response Validation - Flask-Rebar reli
Flask-Bcrypt Flask-Bcrypt is a Flask extension that provides bcrypt hashing utilities for your application. Due to the recent increased prevelance of
Flask-Starter Flask-Starter is a boilerplate starter template designed to help you quickstart your Flask web application development. It has all the r
Brandnew-flask is still in the initial stage and needs to be updated and improved continuously. Everyone is welcome to maintain and improve this CLI.
Flask Project Template A full feature Flask project template. See also Python-Project-Template for a lean, low dependency Python app. HOW TO USE THIS
Flask-Fastx Flask-Fastx is a Fast API style support for Flask. It Gives you MyPy types with the flexibility of flask. Compatibility Flask-Fastx requir
Flask-app scaffold, generate flask restful backend
Flask pre-setup architecture. This can be used in any flask project for a faster and better project code structure. All the required libraries are already installed easily to use in any big project.
flask-reactize Purpose Developing a ReactJS application requires to use nodejs as back end server. What if you want to consume external APIs: how are
In the name of God, the Most Gracious, the Most Merciful. PF-Flask-Rest-Com Docu
Flask-Discord-Bot-Dashboard A simple discord Bot dashboard created in Flask Pyth
Open-source Flask Sample built on top of flask-dance library. The project implements the social login for Github and Twitter - Originally coded by TestDriven.IO.
Flask Red Mail: Email Sending for Flask Flask extension for Red Mail What is it?
Flask Sitemapper Flask Sitemapper is a small Python 3 package that generates XML sitemaps for Flask applications. This allows you to create a nice and
flask-template By GaGoU :3 a simple template for make an flask api notes: you ca