Python 在Google Colab笔记本中启动Dash应用程序

Python 在Google Colab笔记本中启动Dash应用程序,python,dashboard,plotly-dash,Python,Dashboard,Plotly Dash,如何从Google Colab()启动Dash应用程序()?据我所知,目前还没有直接的方法 在下面找到一个类似于设置张力板()的解决方法 从设置此解决方案所需的所有内容的代码单元开始: # How to run a Dash app in Google Colab ## Requirements ### Install ngrok !wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip !unzip ng

如何从Google Colab()启动Dash应用程序()?

据我所知,目前还没有直接的方法

在下面找到一个类似于设置张力板()的解决方法

从设置此解决方案所需的所有内容的代码单元开始:

# How to run a Dash app in Google Colab

## Requirements

### Install ngrok
!wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
!unzip ngrok-stable-linux-amd64.zip

### Run ngrok to tunnel Dash app port 8050 to the outside world. 
### This command runs in the background.
get_ipython().system_raw('./ngrok http 8050 &')

### Get the public URL where you can access the Dash app. Copy this URL.
! curl -s http://localhost:4040/api/tunnels | python3 -c \
    "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"

### Install Dash
!pip install dash==0.31.1  # The core dash backend
!pip install dash-html-components==0.13.2  # HTML components
!pip install dash-core-components==0.39.0  # Supercharged components
!pip install dash-table==3.1.7  # Interactive DataTable component (new!)
在Dash应用程序中添加另一个代码单元:

## Dash app (https://dash.plot.ly/getting-started)

### Save file with Dash app on the Google Colab machine
%%writefile my_app1.py
import dash
import dash_core_components as dcc
import dash_html_components as html

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div(children=[
    html.H1(children='Hello Dash'),

    html.Div(children='''
        Dash: A web application framework for Python.
    '''),

    dcc.Graph(
        id='example-graph',
        figure={
            'data': [
                {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
                {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
            ],
            'layout': {
                'title': 'Dash Data Visualization'
            }
        }
    )
])

if __name__ == '__main__':
    app.run_server(debug=True)
在最后一个代码单元中,您可以启动Dash应用程序(该单元将一直忙到您停止执行,从而停止Dash应用程序)

要访问Dash应用程序,请将上面的ngrok.io URL复制并粘贴到新的浏览器选项卡(不是127.0.0.1:8050),然后等待几秒钟。

JupyterDash(笔记本电脑中运行Dash的官方库)现在支持在Colab上运行应用程序

您可以将此代码粘贴到colab笔记本中,您的应用程序将以内联方式显示:

!pip install jupyter-dash

import plotly.express as px
from jupyter_dash import JupyterDash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
# Load Data
df = px.data.tips()
# Build App
app = JupyterDash(__name__)
app.layout = html.Div([
    html.H1("JupyterDash Demo"),
    dcc.Graph(id='graph'),
    html.Label([
        "colorscale",
        dcc.Dropdown(
            id='colorscale-dropdown', clearable=False,
            value='plasma', options=[
                {'label': c, 'value': c}
                for c in px.colors.named_colorscales()
            ])
    ]),
])
# Define callback to update graph
@app.callback(
    Output('graph', 'figure'),
    [Input("colorscale-dropdown", "value")]
)
def update_figure(colorscale):
    return px.scatter(
        df, x="total_bill", y="tip", color="size",
        color_continuous_scale=colorscale,
        render_mode="webgl", title="Tips"
    )
# Run app and display result inline in the notebook
app.run_server(mode='inline')
输出的外观。你也可以退房

以下是一些更有用的链接:


    • 谢谢。伙计们,如果邢的建议没有按预期运行,你可以像这样编辑最后一行:

      app.run_server(mode='inline',host="0.0.0.0",port=1005)
      

      我得到这个错误:这是一个开发服务器。不要在生产部署中使用它。这只是一个警告。一切正常。然而,正如警告说,这是一个你不应该考虑的“生产环境”。请参阅:它适用于此示例,您只需当场构建一个小字典,并将其用作示例数据。如何将pandas数据帧传递给my_app1.py?您可以在jupyter笔记本中作为常规应用程序运行dash应用程序,并通过刷新隧道链接查看结果。请检查下面我创建的示例链接:
      app.run_server(mode='inline',host="0.0.0.0",port=1005)