RuntimeError:资产实例未绑定到应用程序,在当前上下文中没有应用程序 [英] RuntimeError: assets instance not bound to an application, and no application in current context

查看:431
本文介绍了RuntimeError:资产实例未绑定到应用程序,在当前上下文中没有应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在修改 cookiecutter Flask app。我目前正在尝试添加一个日期选择器到一个页面。我找到 https://eonasdan.github.io/bootstrap-datetimepicker/ 。这个cookiecutter使用flask-assets来管理项目资产。

遵循 https:// adambard .com / blog / fresh-flask-setup / 我修改了我的assets.py文件:

  from flask_assets import Bundle,Environment 
import os

css = Bundle(
libs / bootstrap / dist / css / spacelab / bootstrap.css,
bower_components / / bss / bootstrap-datetimepicker.css,
css / style.css,
css / home.css,
#css / style .css,
filters =cssmin,
output =public / css / common.css


js = Bundle(
libs / jQuery / dist / jquery.js,
libs / bootstrap / dist / js / bootstrap.js,
bower_components / moment / moment.js,
bower_components /eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js,
js / plugins.js,
filters ='jsmin',
output =public /js/common.js


#电话l flask-assets在哪里查找我们的coffeescript和sass文件。
assets = Environment()
assets.load_path = [os.path.join(os.path.dirname(__ file__),'myflaskapp / static / bower_components')]

assets.register(js_all,js)
assets.register(css_all,css)

当我这样做时,我得到:
$ b $ pre $ code Traceback最近一次调用最后一个
文件C: /envs/r2/myproject/manage.py,第8行,在< module>
from myflaskapp.app import create_app
在< module>文件中的第8行C:\\\\\\\\\\\\\'
from myflaskapp.assets import assets
在< module>文件中,第41行的文件是C:\\\\\\\\\\\\\\\\\\\\\
assets.load_path = [os.path.join(os.path.dirname(__ FILE__), 'myflaskapp /静态/ bower_components')]
档C:\envs\virtalenvs\flask_myproject \lib\site-packages\webassets\env.py,第639行,在_set_load_path
self._storage ['load_path'] = load_path
文件C:\\\\\\\\ virtalenvs\flask_myproject\lib\site-packages\flask_assets.py,第104行,在__setitem__
self.env._app.config [self._transform_key(key)] = value
File C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ :资产实例不绑定到应用程序,并且在当前上下文中没有应用程序

我在做什么错误?

编辑:app.py初始化资源:

 从myflask导入Flask,render_template 
app.assets导入资产

:param config_object:要使用的配置对象。

app = Flask(__ name__)
app.config.from_object(config_object)
register_extensions(app)
register_blueprints(app)
register_errorhandlers (app)
返回应用程序

def register_extensions(app):
assets.init_app(app)
$ b $ def register_blueprints(app):
app.register_blueprint(public.blueprint)
app.register_blueprint(user.blueprint)


解决方案

正如我在我的评论中指出的那样,你必须将一个Flask应用绑定到该对象上,正如它在traceback中注释的那样:

>>> RuntimeError:资产实例未绑定到应用程序,并且在当前上下文中没有
应用程序

这将解决您的问题,无论是否与您的使用情况相关...:

  def register_extensions(app):
assets.init_app(app)
with app.app_context():
assets.loa d_path = ['static']

或者你可以重新编写你的创建应用程序来

  def create_assets(app):
assets = Environment(app)
...
assets .load_path ['static']
返回资产
$ b $ def create_app():
app = Flask()
....
assets = create_assets (app)
返回应用程序

错误的全部原因是您调用 LOAD_PATH 。这将尝试设置webassets属性,你可以在这里看到:的 https://github.com/miracle2k/webassets/blob/95dff0ad6dcc25b81790a1585c67f5393e7d32af/src/webassets/env.py#L656

  def _set_load_path(self,load_path):
self._storage ['load_path'] = load_path

反过来,现在激活你的flask-asset对象上的_storage的属性,这导致它试图做到这一点: https://github.com/miracle2k/flask-assets/blob/eb7f1905410828689086b80eb19be9331041ac52/src/flask_assets.py#L102 < (key,value):

  def __setitem __(self,key,value):
如果不是self._set_deprecated b self.env._app.config [self._t ransform_key(key)] = value

正如你所见,它需要访问一个应用程序,当你使用 load_path 的时候没有给出它,它会向你抱怨,因为它在Traceback中非常好的解释。我在flask-asset页面上找到了关于这个问题的讨论: https://github.com/你可能很正确地认为,当你调用 init_app()时, code>一切都应该没问题,但事实并非如此, init_app()不会引用应用程序的任何引用环境 https://github.com/miracle2k/flask-assets/blob/eb7f1905410828689086b80eb19be9331041ac52/src/flask_assets.py#L338

  def init_app(self,app):
app.jinja_env.add_extension('webassets.ext.jinja2.AssetsExtension')
app.jinja_env.assets_environment = self

我根本不使用烧瓶资产,所以我不确定他们为什么避难用 Environment()。init_app 引用 app ,所以如果别人知道的话,大喊一声。 p>

I'm working to modify a cookiecutter Flask app. I'm currently trying to add a datepicker to a page. I've found https://eonasdan.github.io/bootstrap-datetimepicker/. This cookiecutter uses flask-assets to manage the project assets.

Following https://adambard.com/blog/fresh-flask-setup/ I've modified my assets.py file :

from flask_assets import Bundle, Environment
import os

css = Bundle(
    "libs/bootstrap/dist/css/spacelab/bootstrap.css",
    "bower_components/eonasdan-bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.css",
    "css/style.css",
    "css/home.css",
    # "css/style.css",
    filters="cssmin",
    output="public/css/common.css"
)

js = Bundle(
    "libs/jQuery/dist/jquery.js",
    "libs/bootstrap/dist/js/bootstrap.js",
    "bower_components/moment/moment.js",
    "bower_components/eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js",
    "js/plugins.js",
    filters='jsmin',
    output="public/js/common.js"
)

# Tell flask-assets where to look for our coffeescript and sass files.
assets = Environment()
assets.load_path = [os.path.join(os.path.dirname(__file__), 'myflaskapp/static/bower_components')]

assets.register("js_all", js)
assets.register("css_all", css)

When I do this I get:

Traceback (most recent call last):
  File "C:/envs/r2/myproject/manage.py", line 8, in <module>
    from myflaskapp.app import create_app
  File "C:\envs\r2\myproject\myflaskapp\app.py", line 8, in <module>
    from myflaskapp.assets import assets
  File "C:\envs\r2\myproject\myflaskapp\assets.py", line 41, in <module>
    assets.load_path = [os.path.join(os.path.dirname(__file__), 'myflaskapp/static/bower_components')]
  File "C:\envs\virtalenvs\flask_myproject\lib\site-packages\webassets\env.py", line 639, in _set_load_path
    self._storage['load_path'] = load_path
  File "C:\envs\virtalenvs\flask_myproject\lib\site-packages\flask_assets.py", line 104, in __setitem__
    self.env._app.config[self._transform_key(key)] = value
  File "C:\envs\virtalenvs\flask_myproject\lib\site-packages\flask_assets.py", line 292, in _app
    'and no application in current context')
RuntimeError: assets instance not bound to an application, and no application in current context

What am I doing wrong?

edit: app.py initializes the assets:

from flask import Flask, render_template
from myflaskapp.assets import assets

    :param config_object: The configuration object to use.
    """
    app = Flask(__name__)
    app.config.from_object(config_object)
    register_extensions(app)
    register_blueprints(app)
    register_errorhandlers(app)
    return app

def register_extensions(app):
    assets.init_app(app)

def register_blueprints(app):
    app.register_blueprint(public.blueprint)
    app.register_blueprint(user.blueprint)

解决方案

As I noted in my comment, you have to a Flask app bound to the object, as it notes in the traceback:

>>> RuntimeError: assets instance not bound to an application, and no 
    application in current context

This will fix your problem, whether or not it's relevant for you use case...:

def register_extensions(app):
    assets.init_app(app)
    with app.app_context():
         assets.load_path = ['static']

or you could re-write your create app to have

def create_assets(app):
    assets = Environment(app)
    ....
    assets.load_path ['static']
    return assets

def create_app():
    app = Flask()
    ....
    assets = create_assets(app)
    return app

The whole reason for your errors is your call to load_path. This tries to set the attribute in webassets, which you can see here: https://github.com/miracle2k/webassets/blob/95dff0ad6dcc25b81790a1585c67f5393e7d32af/src/webassets/env.py#L656

def _set_load_path(self, load_path):
    self._storage['load_path'] = load_path

In turn, this now activates the attribute of _storage on your flask-asset object, which results in it trying to do this: https://github.com/miracle2k/flask-assets/blob/eb7f1905410828689086b80eb19be9331041ac52/src/flask_assets.py#L102

def __setitem__(self, key, value):
    if not self._set_deprecated(key, value):
        self.env._app.config[self._transform_key(key)] = value

As you see, it needs access to an app, and as you haven't given one when you used load_path it will complain to you, as it explains so nicely in the Traceback. I found a discussion about this on the flask-asset page: https://github.com/miracle2k/flask-assets/issues/35

You may, quite rightly, think that as you called init_app() that everything should be fine, but that's not the case, init_app() does not give any reference of the app to Environment: https://github.com/miracle2k/flask-assets/blob/eb7f1905410828689086b80eb19be9331041ac52/src/flask_assets.py#L338

def init_app(self, app):
    app.jinja_env.add_extension('webassets.ext.jinja2.AssetsExtension')
    app.jinja_env.assets_environment = self

I don't use flask-assets at all, and so I'm not particularly sure why they haven't referenced the app with the Environment().init_app, so if someone else knows, shout out.

这篇关于RuntimeError:资产实例未绑定到应用程序,在当前上下文中没有应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆