from the below doc, it says we can run app in notebook without using JupyterDash, instead we can just run app.run(jupyter_mode="external").
在下面的文档中,我们可以不使用JupyterDash在笔记本中运行应用程序,而只需运行app.run(jupyter_mode=“外部”)。
https://dash.plotly.com/dash-in-jupyter
Https://dash.plotly.com/dash-in-jupyter
However, I tried and I got an error of. I am running notbook in vs code. and version of dash is 2.7.0. How to solve it? Thanks
然而,我尝试了,但我得到了一个错误。我在VS代码中运行Notebook。DASH的版本是2.7.0。如何解决?谢谢
import dash
print(dash.__version__)
2.7.0
TypeError: run_simple() got an unexpected keyword argument 'jupyter_mode'
import time
from uuid import uuid4
import diskcache
from dash import Dash, html, DiskcacheManager, CeleryManager, Input, Output, callback
import dash_bootstrap_components as dbc
# from dash import jupyter_dash
# jupyter_dash.default_mode="external"
launch_uid = uuid4()
cache = diskcache.Cache("./cache")
background_callback_manager = DiskcacheManager(
cache, cache_by=[lambda: launch_uid], expire=60
)
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP],
background_callback_manager=background_callback_manager)
# Simple layout with Start and Cancel buttons as well as a progress bar
app.layout = html.Div(
[
html.Div(
[
dbc.Row(children=[
dbc.Col(),
dbc.Col(
children=[
html.P(id="paragraph_id", children=["Button not clicked"]),
# Progress component sets up the loading bar
dbc.Progress(id="progress-component", striped=True, value=5)
]
),
dbc.Col()
]
),
dbc.Row(
children=[
dbc.Col(),
dbc.Col(
children=[
# Button kicks off the background callback
html.Button(id="button_id", children="Run Job!"),
# Button cancels the background callback
html.Button(id="cancel_button_id", children="Cancel Running Job!"),
]
),
dbc.Col(),
]
)
]
),
]
)
def calculate_percent_progress(iteration, total):
percent = int((iteration / total) * 100)
f_percent = f"{int(percent)} %"
return percent, f_percent
def slow_process(set_progress):
"""
This would be the slow function, need to set the progress on each iteration to update the progress
bar.
The 'set_progress' is a special argument with a function handle to update the app on current progress
see: https://dash.plotly.com/background-callbacks
:param set_progress:
:return:
"""
total = 10
for i in range(total):
time.sleep(0.5)
percent, f_percent = calculate_percent_progress(iteration=i+1, total=total)
set_progress([int(percent), f"{int(percent)} %"])
@callback(
Output("paragraph_id", "children"),
Input("button_id", "n_clicks"),
running=[
(Output("button_id", "disabled"), True, False),
(Output("cancel_button_id", "disabled"), False, True),
(
Output("paragraph_id", "style"),
{"visibility": "hidden"},
{"visibility": "visible"},
)
],
cancel=[Input("cancel_button_id", "n_clicks")],
progress=[
Output("progress-component", "value"),
Output("progress-component", "label")
],
interval=1000,
background=True
)
def callback(set_progress, n_clicks):
slow_process(set_progress)
return [f"Clicked {n_clicks} times"]
if __name__ == "__main__":
app.run(jupyter_mode="external")
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_32408/3712129971.py in <module>
109
110 if __name__ == "__main__":
--> 111 app.run(jupyter_mode="external")
112 # app.run(debug=True)
c:\Users\test\miniconda3\envs\dash_tf\lib\site-packages\dash\dash.py in run(self, host, port, proxy, debug, dev_tools_ui, dev_tools_props_check, dev_tools_serve_dev_bundles, dev_tools_hot_reload, dev_tools_hot_reload_interval, dev_tools_hot_reload_watch_interval, dev_tools_hot_reload_max_retry, dev_tools_silence_routes_logging, dev_tools_prune_errors, **flask_run_options)
1978 extra_files.append(path)
1979
-> 1980 self.server.run(host=host, port=port, debug=debug, **flask_run_options)
1981
1982 def _import_layouts_from_pages(self):
c:\Users\test\miniconda3\envs\dash_tf\lib\site-packages\flask\app.py in run(self, host, port, debug, load_dotenv, **options)
988
989 try:
--> 990 run_simple(host, port, self, **options)
991 finally:
992 # reset the first request information if the development server
TypeError: run_simple() got an unexpected keyword argument 'jupyter_mode'
更多回答
Running in VSCode may be the issue? Where you link to shows actual Jupyter there in the example images and the title is 'Dash in Jupyter Environments'. I imagine where you are running doesn't look like the images. And you aren't running it in a browser like the documentation says?
在VSCode中运行可能是问题所在?链接到的位置在示例图像中显示了实际的Jupyter,标题为“Dash in Jupyter Environment”(在Jupyter环境中使用Dash)。我想你要跑的地方看起来不像图片。而且您没有像文档中所说的那样在浏览器中运行它?
Thanks Wayne, I just ran the code in jupyter notebook and I got same error. so it is not related to vs code.
谢谢韦恩,我刚刚在Jupyter笔记本上运行了代码,我得到了同样的错误。所以它与VS代码无关。
Hmmm...I was hoping it was that simple. Is your software running Jupyter older? Or brand new? I know the software running the server that allows it to use your browser recently changed dramatically to support the more JupyterLab-tech focused direction of development in Jupyter. And so it could just be a mismatch between what JupyterDash as the expected mode indicated and what your Jupyter currently indicates? Plus the Jupyter-Dash readme indicates major shifts there. ...
嗯……我还希望有那么简单呢。您的软件运行的是旧版的Jupyter吗?还是全新的?我知道运行服务器的软件允许它使用你的浏览器,最近发生了戏剧性的变化,以支持更注重JupyterLab技术的Jupyter开发方向。因此,这可能只是JupyterDash所指示的预期模式与您的Jupyter当前所指示的不匹配?此外,Jupyter-Dash自述文件显示了这方面的重大转变。..。
I feel this indicates here that your Dash is outdated because jupyter_mode
is definitely something it should be able to use. The current version is 2.13.0 which is much newer than you indicate using. Your version is almost a year old.
我觉得这表明您的Dash已经过时了,因为jupyter_mode绝对是它应该能够使用的东西。当前版本是2.13.0,这比您使用的版本要新得多。你的版本已经快一年了。
我是一名优秀的程序员,十分优秀!