从下面的文档中,它说我们可以在不使用 JupyterDash 的情况下在笔记本中运行应用程序,相反,我们可以只运行 app.run(jupyter_mode="external")。
https://dash.plotly.com/dash-in-jupyter
但是,我尝试了,但出现了错误。我在 vs code 中运行笔记本。 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'
此链接 (https://dash.plotly.com/dash-in-jupyter) 表示 dash 中的 jupyter 支持从 2.11 开始。如果您使用的是 dash 2.7,您可能需要更新 dash。