Plotly: How to rewrite a standard dash app to launch it in JupyterLab?(Plotly:如何重写标准仪表板应用程序以在 JupyterLab 中启动它?)
问题描述
您可以在 plotly 文档中找到一堆 Dash 示例,并且大多数示例都以关于如何使用 Dash 构建图形的说明结尾:<块引用>
达世币呢?Dash 是一个用于构建的开源框架分析应用程序,无需 Javascript,它是与 Plotly 图形库紧密集成.
在
但您也可以使用 mode='external'
来启动 Dash 它自己的标签:
您可以设置 mode='external'
在默认浏览器中启动它.
包含更改的完整代码:'
导入 plotly.graph_objects将 plotly.express 导入为 px# 导入破折号从 jupyter_dash 导入 JupyterDash将 dash_core_components 导入为 dcc将 dash_html_components 导入为 html# 数据和绘图图df = px.data.gapminder().query("country=='Canada'")fig = px.line(df, x="year", y="lifeExp", title="5Yqg5ou/5aSn55qE6aKE5pyf5a+/5ZG9")# 设置 Dash 应用程序# app = dash.Dash()应用程序 = JupyterDash(__name__)app.layout = html.Div([dcc.Graph(图=图)])# 启动 Dash 应用程序# app.run_server(debug=True,# use_reloader=False # 如果在 Jupyter 内部,则关闭重新加载器# )app.run_server(mode='inline', 端口 = 8070, dev_tools_ui=True,dev_tools_hot_reload =真,线程=真)
You can find a bunch of Dash examples in the plotly docs, and most examples end with a note on how to build figures using 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.
But I'd like to fire them up in JupyterLab instead. So what changes would I have to make in the following 'normal' Dash app to make it run in JupyterLab?
Code sample:
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="TGlmZSBleHBlY3RhbmN5IGluIENhbmFkYQ==")
# 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
)
Any working Dash app can be launched from JupyterLab with the setup described in the question by specifying use_reloader=False
in:
app.run_server(debug=True,
use_reloader=False # Turn off reloader if inside Jupyter
)
But if you'd like to use JupyterLab and select between launching the app in your default browser, inline in a cell or directly in Jupyter
in its own tab, just follow these simple steps:
Change the following lines
# 1
import dash
# 2
app = dash.Dash()
# 3
app.run_server(debug=True,
use_reloader=False # Turn off reloader if inside Jupyter
)
To this:
# 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)
This will launch Dash inline directly in JupyterLab:
But you can also go for mode='external'
to launch Dash it its own tab:
And you can set mode='external'
to launch it in your default browser.
Complete code with changes:'
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="TGlmZSBleHBlY3RhbmN5IGluIENhbmFkYQ==")
# 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)
这篇关于Plotly:如何重写标准仪表板应用程序以在 JupyterLab 中启动它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Plotly:如何重写标准仪表板应用程序以在 JupyterLab 中启动它?


- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01