标签: flask-assets

使用Flask-Assets编译较少的文件

我目前正在尝试设置Flask Web应用程序,并尝试使用Flask-Assets将较少的文件编译为缩小的CSS.

这是我创建捆绑包的assets.py文件.

from flask_assets import Bundle

common_css = Bundle(
    'vendor/less/theme.less',
    filters='less',
    output='static/css/common.css',
    )
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

OSError: [Errno 2] No such file or directory
Run Code Online (Sandbox Code Playgroud)

较少过滤器webassets文档中,它说:

This depends on the NodeJS implementation of less, installable via npm. To use the old Ruby-based version (implemented in the 1.x Ruby gem), see Less.

...

LESS_BIN (binary)
    Path to the less executable used to compile source files. By default, the filter will attempt to run lessc via the system …
Run Code Online (Sandbox Code Playgroud)

python less flask webassets flask-assets

6
推荐指数
1
解决办法
2682
查看次数

在uWSGI下运行时,Flask Assets包不会编译

当我直接运行烧瓶应用程序时它们正常运行但在uWSGI下运行时不编译或替换模板中的地址.

我该怎么调试呢?

编辑:

代码:assets =环境(app)

...

if __name__ == "__main__":        
    assets.register(YAMLLoader(os.path.join(DIR,"assets.yml")).load_bundles())

    if os.environ.get("DEBUG_FLASK"):
        app.run()
    else:
        app.run(debug=True)
Run Code Online (Sandbox Code Playgroud)

assets.yml:

style_css:
    filters: less
    output: css/style.css
    contents:
        - css/style.less
Run Code Online (Sandbox Code Playgroud)

flask uwsgi webassets flask-assets

6
推荐指数
1
解决办法
341
查看次数

全局覆盖 url_for 以便扩展程序也使用它

我正在尝试url_for全局覆盖该函数。我想这样做,以便我可以从多个网址加载我的资源,以克服浏览器的最大并发连接数限制。

我曾经app.context_processor在模板中替换我自己的函数,但与 Flask-Assets 一起使用时它不起作用。

STATIC_URLS = [
    static.1.domain.com,
    static.2.domain.com,
    static.3.domain.com
]

@app.context_processor
def override_url_for():
    return dict(url_for=static_urls)

def static_urls(endpoint, **values):
    if endpoint == 'static':
        filename = values.get('filename', None)
        static_urls = app.config.get('STATIC_URLS', None)
        if filename and static_urls:
            hashed = int(hashlib.md5(filename.encode()).hexdigest(), 16)
            index = hashed % len(static_urls) - 1
            url = static_urls[index]
            url = app.url_map.bind(url)
            return url.build(endpoint, values=values, force_external=True)

    return url_for(endpoint, **values)
Run Code Online (Sandbox Code Playgroud)

在模板中,url_for可以正常工作,但ASSET_URL不能。

STATIC_URLS = [
    static.1.domain.com,
    static.2.domain.com,
    static.3.domain.com
]

@app.context_processor
def override_url_for():
    return dict(url_for=static_urls) …
Run Code Online (Sandbox Code Playgroud)

python flask flask-assets

5
推荐指数
0
解决办法
1133
查看次数

当启用调试模式时,如何使 Flask-Assets 重新生成每个请求的 SCSS?

以下是我对 scss 的设置flask-assets

def configure_extensions(app):
  # Web Assets
  from app.extensions import assets
  scss = Bundle(
    'scss/all.scss',
    filters='scss',
    output='scss_all.css'
  )
  assets.register('scss_all', scss)
  assets.init_app(app)
Run Code Online (Sandbox Code Playgroud)

在我的配置中,我设置了ASSETS_DEBUG = True

这有效,并生成文件app/static/scss_all.scss和文件夹app/static/.webassets.cache。样式按预期显示在网站上。

但问题是,如果我想重新生成 scss 样式表,我必须删除上面提到的文件。当使用 scss 时,这很乏味。

app.debug当设置为时,有没有办法使用重新加载器自动重新生成这些文件True


(旁白:我使用的是 Flask 的开发版本)

python flask flask-assets

3
推荐指数
1
解决办法
1380
查看次数

标签 统计

flask ×4

flask-assets ×4

python ×3

webassets ×2

less ×1

uwsgi ×1