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

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