RuntimeError: assets instance not bound to an application, and no application in current context
RuntimeError: assets instance not bound to an application, and no application in current context
我正在努力修改 cookiecutter Flask app. I'm currently trying to add a datepicker to a page. I've found 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/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)
当我这样做时,我得到:
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
我做错了什么?
编辑:app.py 初始化资产:
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)
正如我在评论中指出的那样,您必须将 Flask 应用程序绑定到该对象,正如它在回溯中指出的那样:
>>> RuntimeError: assets instance not bound to an application, and no
application in current context
这将解决您的问题,无论它是否与您的用例相关...:[=28=]
def register_extensions(app):
assets.init_app(app)
with app.app_context():
assets.load_path = ['static']
或者您可以重新编写您的创建应用程序以拥有
def create_assets(app):
assets = Environment(app)
....
assets.load_path ['static']
return assets
def create_app():
app = Flask()
....
assets = create_assets(app)
return 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
def __setitem__(self, key, value):
if not self._set_deprecated(key, value):
self.env._app.config[self._transform_key(key)] = value
如您所见,它需要访问应用程序,并且由于您在使用 load_path
时没有提供应用程序,它会向您抱怨,正如它在 Traceback 中解释得非常好。我在 flask-asset 页面上找到了关于这个的讨论:https://github.com/miracle2k/flask-assets/issues/35
你可能认为,正如你所说的 init_app()
一切都应该没问题,但事实并非如此,init_app()
没有给出 app
的任何参考至 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
我根本不使用烧瓶资产,所以我不是特别确定为什么他们没有用 Environment().init_app
引用 app
,所以如果其他人知道,大声喊出来。
我正在努力修改 cookiecutter Flask app. I'm currently trying to add a datepicker to a page. I've found 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/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)
当我这样做时,我得到:
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
我做错了什么?
编辑:app.py 初始化资产:
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)
正如我在评论中指出的那样,您必须将 Flask 应用程序绑定到该对象,正如它在回溯中指出的那样:
>>> RuntimeError: assets instance not bound to an application, and no
application in current context
这将解决您的问题,无论它是否与您的用例相关...:[=28=]
def register_extensions(app):
assets.init_app(app)
with app.app_context():
assets.load_path = ['static']
或者您可以重新编写您的创建应用程序以拥有
def create_assets(app):
assets = Environment(app)
....
assets.load_path ['static']
return assets
def create_app():
app = Flask()
....
assets = create_assets(app)
return 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
def __setitem__(self, key, value):
if not self._set_deprecated(key, value):
self.env._app.config[self._transform_key(key)] = value
如您所见,它需要访问应用程序,并且由于您在使用 load_path
时没有提供应用程序,它会向您抱怨,正如它在 Traceback 中解释得非常好。我在 flask-asset 页面上找到了关于这个的讨论:https://github.com/miracle2k/flask-assets/issues/35
你可能认为,正如你所说的 init_app()
一切都应该没问题,但事实并非如此,init_app()
没有给出 app
的任何参考至 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
我根本不使用烧瓶资产,所以我不是特别确定为什么他们没有用 Environment().init_app
引用 app
,所以如果其他人知道,大声喊出来。