Google Colab ノートブックで Dash アプリを起動する 質問する

Google Colab ノートブックで Dash アプリを起動する 質問する

Dashアプリを起動する方法(翻訳元:) Google Colabより(出典:)?

ベストアンサー1

私の知る限り、現時点ではこれを行う簡単な方法はありません。

以下に、Tensorboard の設定に似た回避策を示します (https://www.dlology.com/blog/quick-guide-to-run-tensorboard-in-google-colab/)。

この回避策に必要なすべてのものを設定するコード セルから始めます。

# 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 アプリを停止するまでビジー状態になります)。

### Run Dash app
!python my_app1.py

Dashアプリにアクセスするには、ngrok.io-URL上記を新しいブラウザタブ(127.0.0.1:8050 ではない)にコピーし、数秒待ちます。

おすすめ記事