python - Plotly:如何重写标准破折号应用程序以在 JupyterLab 中启动它?

标签 python plotly plotly-dash jupyter-lab plotly-python

您可以在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:

enter image description here

但您也可以使用 mode='external' 来启动 Dash 自己的选项卡:

enter image description here

您可以设置 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/

相关文章:

python - 一个类的成员元组(而不是成员变量)?

python - 检查变量是否不等于向量的任何元素

python - Plotly python offline - 点击访问 url?

python - Multi-Class 分类 : SMOTE oversampling for multiple columns in a row

python - Plotly:如何使用 plotly express 为多个轨迹设置标记符号形状?

python - 如何在 plotly scattergeo 美国 map 中使用离散色标

python - 在 Dash Python 中下载具有列格式的 Excel 文件

plotly 表达 : How to add comma separators for thousands on data labels

python - 如何使 dash 中的选项卡具有不同的 URL?

python - 在 python 2.7 [windows - 64 位] 上安装 xlwt 模块