[英]How to use packaged assets with Flask-Assets
How can I bundle assets using Flask-Assets that exist outside of Flasks default static/ directory? 如何使用Flasks默认静态/目录之外的Flask-Assets捆绑资产?
npm install
downloading assets into bower_components/
npm install
下载资产到bower_components/
javascripts/
javascripts/
assets instance not bound to an application, and no application in current context
exception. assets instance not bound to an application, and no application in current context
的assets instance not bound to an application, and no application in current context
异常中的assets instance not bound to an application, and no application in current context
。 Any help would be appreciated, especially if you can just give me an example on how to manage raw + packaged assets outside your apps static/ directory :P 任何帮助将不胜感激,特别是如果你能给我一个如何管理你的应用程序静态/目录之外的原始+打包资产的示例:P
app/
static/
__init__.py
assets.py
javascripts/
app.js
bower_components/
jquery.js
jquery,pjax,js
from flask.ext.assets import Bundle, Environment
js = Bundle(
'bower_components/jquery.js',
'bower_components/jquery.pjax.js',
'javascripts/app.js'
filters='jsmin',
output='static/packed.js'
)
assets = Environment()
assets.register('js_all', js)
from flask import Flask
from app.assets import assets
app = Flask(__name__)
assets.init_app(app)
I checked Flask-Assets source and found this in the docstring of FlaskResolver
class: 我检查了Flask-Assets源代码,并在
FlaskResolver
类的docstring中找到了它:
If a
Environment.load_path
is set, it is used to look up source files, replacing the Flask system.如果设置了
Environment.load_path
,它将用于查找源文件,替换Flask系统。 Blueprint prefixes are no longer resolved.蓝图前缀不再解析。
So you need to do the following in app/init.py: 所以你需要在app / init.py中执行以下操作:
from os.path import abspath, join
app = Flask(__name__)
assets.load_path = abspath(join(app.root_path, '..'))
assets.init_app(app)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.