情节:如何重写标准的破折号应用程序以在JupyterLab中启动它? [英] Plotly: How to rewrite a standard dash app to launch it in JupyterLab?

查看:108
本文介绍了情节:如何重写标准的破折号应用程序以在JupyterLab中启动它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在可打印的文档中可以找到许多Dash示例,并且大多数示例都以关于如何使用Dash构建图形的注释结尾:

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:

Dash呢? Dash是用于构建的开源框架 分析应用程序,不需要Java脚本,它是 与Plotly图形库紧密集成.

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.

通过 https://dash.plot.ly/installation 了解如何安装Dash.

Learn about how to install Dash at https://dash.plot.ly/installation.

但是我想改为在JupyterLab中启动它们.那么,为了在JupyterLab中运行它,我必须在以下常规" Dash应用程序中进行哪些更改?

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?

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
              ) 

推荐答案

可以通过问题中所述的设置,从JupyterLab 启动任何运行中的Dash应用程序,方法是在以下位置指定use_reloader=False

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
          ) 

但是,如果您想使用JupyterLab并选择在默认浏览器中启动应用程序,在单元格中内联或直接在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:

# 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 内联:

This will launch Dash inline directly in JupyterLab:

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

But you can also go for mode='external' to launch Dash it its own tab:

您可以设置mode='external'以在默认浏览器中启动它.

And you can set mode='external' to launch it in your default browser.

这篇关于情节:如何重写标准的破折号应用程序以在JupyterLab中启动它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆