KnowHow

技術的なメモを中心にまとめます。
検索にて調べることができます。

DushをVPSサーバで公開するには?

登録日 :2023/04/08 16:44
カテゴリ :python dash

VPSサーバにて、gunicornを使ってDushを公開する

0 環境構築
python3 のインストール および、dash, gunicornなどライブラリインストール

1 wsgi.pyを作成する
from sample import app
application = app.server

if name == "main":
application.run()

2 Dushのプログラム(sample.py)でのポイントは2行を記述しておくこと
app = dash.Dash(name, external_stylesheets=external_stylesheets)
server = app.server
※サンプルコード全体は下段に記述する

3 gunicornを起動して、アクセスしてみる
gunicorn --bind=0.0.0.0:8000 wsgi:application

4 serviceファイルを作成して、systemctlにてサービス化する
(パスについては、各アプリケーションや環境に合わせて変える)
sudo vi /etc/systemd/system/mydush.service

(mydash) test $ cat /etc/systemd/system/mydush.service
[Unit]
Description=mydush
After=network.target

[Service]
WorkingDirectory=/home//test
ExecStart=/bin/gunicorn --bind 0.0.0.0:8000 wsgi:application

[Install]
WantedBy=multi-user.target

参考サイト)
https://community.plotly.com/t/unable-to-deploy-dash-application-on-linux-using-gunicorn/52166

sample.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)
server = app.server

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': 'Montréal'},
            ],
            'layout': {
                'title': 'Dash Data Visualization'
            }
        }
    )
])

if __name__ == '__main__':
    app.run_server()