1 回答

TA貢獻1963條經驗 獲得超6個贊
任何可用的 Dash 應用程序都可以通過JupyterLab啟動,并使用問題中描述的設置,方法是use_reloader=False在以下位置指定:
app.run_server(debug=True,
use_reloader=False # Turn off reloader if inside Jupyter
)
但如果您想使用 JupyterLab 并launching the app in your default browser, inline in a cell or directly in Jupyter在其自己的選項卡中進行選擇,只需按照以下簡單步驟操作:
更改以下行
# 1
import dash
# 2
app = dash.Dash()
# 3
app.run_server(debug=True,
use_reloader=False # Turn off reloader if inside Jupyter
)
對此:
# 1
from jupyter_dash import JupyterDash
# 2
app = JupyterDash(__name__)
# 3
app.run_server(mode='inline', port = 8070, dev_tools_ui=True,
dev_tools_hot_reload =True, threaded=True)
這將直接在 JupyterLab 中內聯啟動 Dash :
但您也可以mode='external'
啟動 Dash 它自己的選項卡:
您可以設置mode='external'在默認瀏覽器中啟動它。
經過更改的完整代碼:'
import plotly.graph_objects as go
import plotly.express as px
# import dash
from jupyter_dash import JupyterDash
import dash_core_components as dcc
import dash_html_components as html
# data and plotly figure
df = px.data.gapminder().query("country=='Canada'")
fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada')
# Set up Dash app
# app = dash.Dash()
app = JupyterDash(__name__)
app.layout = html.Div([
dcc.Graph(figure=fig)
])
# Launch Dash app
# app.run_server(debug=True,
# use_reloader=False # Turn off reloader if inside Jupyter
# )
app.run_server(mode='inline', port = 8070, dev_tools_ui=True,
dev_tools_hot_reload =True, threaded=True)
添加回答
舉報