生活分享

  • 首页
HETAO
业余码农、业余机械爱好者
  1. 首页
  2. 未分类
  3. 正文

How to use JAC (jinja-assets-compressor) in Flask and with Flask-User

2017年10月14日 508点热度 0人点赞 0条评论

If you use flask-user in your flask application , and jac won't work, also flask_static_crompress package (which do not have even one line ,only jac flask contrb file reference, its funny).

Problem HOW to make it work.

In your Init_app function, if you do not have it please try to use this method create app once ,its very smart way .code like below:

 

app=Flask(__name__)
jac = JAC()
def init_app():
  jac.init_app(app)
  app.jinja_env.compressor_debug = app.config.get('DEBUG')
  app.jinja_env.compressor_output_dir = './static'
  app.jinja_env.compressor_static_prefix = '/static'

If you do not use Flask-User, its done otherwise you have to custom your static_finder function (file jac/contrib/flask.py ) and add one line to above config:

 app.jinja_env.compressor_source_dirs = static_finder(app) 

 

def static_finder(app):
    def find(path=None):
        if path is None:
            folders = set()
            for blueprint in app.blueprints.values():
                if blueprint.static_folder is not None:
                    folders.update([blueprint.static_folder])
            folders.update([app.static_folder])
            return folders
        else:
            bp_values = app.blueprints.values()
            # dectect flask-user
            if 'flask_user' in [x.name for x in bp_values]:
                app.blueprints['flask_user'].name = 'user'

            for rule in app.url_map.iter_rules():
                if '.' in rule.endpoint:
                    with_blueprint = True
                    blueprint_name = rule.endpoint.rsplit('.', 1)[0]
                    for x in bp_values:
                        if x.name == blueprint_name:
                            blueprint = x

                    data = rule.match(u('{subdomain}|{path}').format(
                        subdomain=blueprint.subdomain or '',
                        path=path,
                    ))
                else:
                    with_blueprint = False
                    data = rule.match(u('|{0}').format(path))

                if data:
                    static_folder = blueprint.static_folder if with_blueprint and blueprint.static_folder is not None else app.static_folder
                    return os.path.join(static_folder, data['filename'])

        raise IOError(2, u('File not found {0}.').format(path))

    return find

 

Post Views: 633
标签: 暂无
最后更新:2017年10月14日

jinzhao

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

COPYRIGHT © 2022 生活分享. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

浙ICP备14004285号-1