简体   繁体   English

如何使用Flask-Assets打包资产

[英]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捆绑资产?

  • I have npm install downloading assets into bower_components/ 我有npm install下载资产到bower_components/
  • I have other javascripts that exist in javascripts/ 我还有其他javascripts存在于javascripts/
  • I am using Flasks app factory pattern, and no matter how I've tried configuring Flask-Assets - I cannot get around the assets instance not bound to an application, and no application in current context exception. 我正在使用Flasks app工厂模式,无论我如何尝试配置Flask-Assets - 我无法绕过assets instance not bound to an application, and no application in current contextassets 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 structure 应用结构

app/
    static/
    __init__.py
    assets.py
javascripts/
    app.js
bower_components/
    jquery.js
    jquery,pjax,js

app/assets.py 应用程序/ assets.py

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)

app/ init .py app / init .py

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM