在Google Colab笔记本中启动Dash应用 [英] Launch a Dash app in a Google Colab Notebook
问题描述
如何从Google Colab启动Dash应用( http://dash.plot.ly )( https://colab.research.google.com )?
How to launch a Dash app (http://dash.plot.ly) from Google Colab (https://colab.research.google.com)?
推荐答案
据我所知,目前尚无直接方法可以做到这一点.
To my knowledge there is currently no straightforward way to do this.
在下面找到一种类似于设置Tensorboard的变通方法( https://www.dlology.com/blog/quick-guide-to-run-tensorboard-in-google-colab/).
Find below a workaround that is similar to setting up Tensorboard (https://www.dlology.com/blog/quick-guide-to-run-tensorboard-in-google-colab/).
从一个代码单元开始,该单元设置了此替代方法所需的所有内容:
Start with a code cell that sets up all things required for this workaround:
# 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应用程序中添加另一个代码单元:
Add another code cell with your Dash app:
## 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应用程序为止.)
In a last code cell you can then start your Dash app (this cell will be busy until you stop the exection and thus, stop your Dash app).
### Run Dash app
!python my_app1.py
要访问Dash应用副本&将上面的 ngrok.io-URL 粘贴到新的浏览器标签(不是127.0.0.1:8050),然后等待几秒钟.
To access the Dash app copy & paste the ngrok.io-URL above to a new brower tab (NOT 127.0.0.1:8050) and wait a few seconds.
这篇关于在Google Colab笔记本中启动Dash应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!