无法在 dash 应用程序中加载静态 css 文件
unable to load static css file in the dash app
我构建了一个单页破折号应用程序,当 运行 作为单个文件时,它 运行 符合预期,但是当我尝试 运行 它作为整个应用程序时, CSS 没有正确加载。
下面是我的文件夹结构
当我通过 manage.py
加载整个应用程序时,下面是我得到的错误
Internal Server Error: /assets/internal.css
Traceback (most recent call last):
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\my_project\analyser_tool\views.py", line 32, in dash_index
return HttpResponse(dispatcher(request))
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\my_project\analyser_tool\views.py", line 27, in dispatcher
return response.get_data()
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\lib\site-packages\werkzeug\wrappers.py", line 986, in get_data
self._ensure_sequence()
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\lib\site-packages\werkzeug\wrappers.py", line 1035, in _ensure_sequence
raise RuntimeError('Attempted implicit sequence conversion '
RuntimeError: Attempted implicit sequence conversion but the response object is in direct passthrough mode.
这是我正在使用的唯一的第二个 dash 应用程序,不幸的是没有太多经验。如果有人可以帮我解决这个问题,我真的很高兴。我已经为此苦苦挣扎了几天。
提前致谢!!
通过继续研究,我能够通过将以下内容添加到我的 server.py
来解决问题
css_directory = os.getcwd()
stylesheets = ['stylesheet.css']
static_css_route = '/static/'
@app.server.route('{}<stylesheet>'.format(static_css_route))
def serve_stylesheet(stylesheet):
if stylesheet not in stylesheets:
raise Exception(
'"{}" is excluded from the allowed static files'.format(
stylesheet
)
)
return flask.send_from_directory(css_directory, stylesheet)
for stylesheet in stylesheets:
app.css.append_css({"external_url": "/static/{}".format(stylesheet)})
此答案之前已回答并摘自
https://community.plot.ly/t/serve-locally-option-with-additional-scripts-and-style-sheets/6974/6
我构建了一个单页破折号应用程序,当 运行 作为单个文件时,它 运行 符合预期,但是当我尝试 运行 它作为整个应用程序时, CSS 没有正确加载。
下面是我的文件夹结构
当我通过 manage.py
加载整个应用程序时,下面是我得到的错误
Internal Server Error: /assets/internal.css
Traceback (most recent call last):
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\my_project\analyser_tool\views.py", line 32, in dash_index
return HttpResponse(dispatcher(request))
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\my_project\analyser_tool\views.py", line 27, in dispatcher
return response.get_data()
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\lib\site-packages\werkzeug\wrappers.py", line 986, in get_data
self._ensure_sequence()
File "C:\Users\Tushar\Documents\serato_video_analyser\video_analyser\lib\site-packages\werkzeug\wrappers.py", line 1035, in _ensure_sequence
raise RuntimeError('Attempted implicit sequence conversion '
RuntimeError: Attempted implicit sequence conversion but the response object is in direct passthrough mode.
这是我正在使用的唯一的第二个 dash 应用程序,不幸的是没有太多经验。如果有人可以帮我解决这个问题,我真的很高兴。我已经为此苦苦挣扎了几天。
提前致谢!!
通过继续研究,我能够通过将以下内容添加到我的 server.py
来解决问题css_directory = os.getcwd()
stylesheets = ['stylesheet.css']
static_css_route = '/static/'
@app.server.route('{}<stylesheet>'.format(static_css_route))
def serve_stylesheet(stylesheet):
if stylesheet not in stylesheets:
raise Exception(
'"{}" is excluded from the allowed static files'.format(
stylesheet
)
)
return flask.send_from_directory(css_directory, stylesheet)
for stylesheet in stylesheets:
app.css.append_css({"external_url": "/static/{}".format(stylesheet)})
此答案之前已回答并摘自
https://community.plot.ly/t/serve-locally-option-with-additional-scripts-and-style-sheets/6974/6