您可以在plotly 文档中找到一堆 Dash 示例,大多数示例都以有关如何使用 Dash 构建图形的注释结尾:
What About Dash? Dash is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.
Learn about how to install Dash at https://dash.plot.ly/installation.
但我想在 JupyterLab 中启动它们。那么我必须在以下“正常”Dash 应用程序中进行哪些更改才能使其在 JupyterLab 中运行?
代码示例:
import plotly.graph_objects as go
import plotly.express as px
import dash
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.layout = html.Div([
dcc.Graph(figure=fig)
])
# Launch Dash app
app.run_server(debug=True,
use_reloader=False # Turn off reloader if inside Jupyter
)
最佳答案
任何正在运行的 Dash 应用都可以通过在以下位置指定 use_reloader=False
来使用问题中描述的设置从 JupyterLab 启动:
app.run_server(debug=True,
use_reloader=False # Turn off reloader if inside Jupyter
)
但是,如果您想使用 JupyterLab 并在在默认浏览器中启动应用程序、在单元格中内联启动应用程序或直接在 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)
关于python - Plotly:如何重写标准破折号应用程序以在 JupyterLab 中启动它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63962975/