plotly-dash 是一個很不錯的dashboard 開發平台,基於python 編寫,提供了很便捷的dashboard 開發模型
同時擴展上也比較靈活我們可以編寫自己的組件。
以下是一個簡單的項目以及集成docker 運行(實際通過gunicorn,uwsgi運行應用)
本地方式運行
使用venv 進行python 環境管理
- 初始化venv 項目
python3 -m venv venv
- 激活環境
source venv/bin/activate
- 添加依賴
pip install dash==1.1.1
pip install dash-daq==0.1.0
- 簡單代碼
# -*- coding: utf-8 -*-
import dash
import flask
import dash_core_components as dcc
import dash_html_components as html
server = flask.Flask(__name__)
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': u'Montréal'},
],
'layout': {
'title': 'Dash Data Visualization'
}
}
)
])
if __name__ == '__main__':
app.run_server(debug=False)
- 啟動
python app.py
- 效果
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:8050/ (Press CTRL+C to quit)
docker 運行
docker 提供了兩種方式的運行,gunicorn以及uwsgi
- docker-compose 文件
version: "3"
services:
dash-gunicorn:
build: ./
image: dalongrong/dash-demo:gunicorn
ports:
- "5000:5000"
dash-uwsgi:
build:
context: ./
dockerfile: Dockerfile-uwsgi
image: dalongrong/dash-demo:uwsgi
ports:
- "5001:5001"
- gunicorn 方式dockerfile
FROM python:3.5.7-alpine
RUN pip install dash==1.1.1 \
&& pip install dash-daq==0.1.0 \
&& pip install gunicorn
WORKDIR /app
COPY . /app
EXPOSE 5000
ENTRYPOINT [ "gunicorn","-b",":5000","app:server"]
- uwsgi 方式dockerfile
FROM python:3.5.7-alpine
RUN apk add --no-cache uwsgi uwsgi-python3 uwsgi-http
RUN pip install dash==1.1.1 \
&& pip install dash-daq==0.1.0
WORKDIR /app
COPY . /app
EXPOSE 5001
ENTRYPOINT [ "uwsgi","--plugins","http,python3","--http","0.0.0.0:5001","--module","app:server","--pythonpath","/usr/local/lib/python3.5/site-packages"]
- 運行
docker-compose build
docker-compose up -d
- 效果
- 幾點說明
使用uwsgi的時候碰到了pip 包查找的問題,問題如下:
Traceback (most recent call last):
dash-uwsgi_1 | File "./app.py", line 2, in <module>
dash-uwsgi_1 | import dash
dash-uwsgi_1 | ModuleNotFoundError: No module named 'dash'
dash-uwsgi_1 | unable to load app 0 (mountpoint='') (callable not found or import error)
dash-uwsgi_1 | *** no app loaded. going in full dynamic mode ***
解決方法,添加pythonpath 如下:
ENTRYPOINT [ "uwsgi","--plugins","http,python3","--http","0.0.0.0:5001","--module","app:server","--pythonpath","/usr/local/lib/python3.5/site-packages"]
uwsgi 只安裝了uwsgi python 無法運行,問題
uwsgi http is ambiguous
問題原因,因為我是通過alpine 的apk 安裝的,需要添加http 以及python 的支持,解決方法
RUN apk add --no-cache uwsgi uwsgi-python3 uwsgi-http
注意對於python模塊的支持需要uwsgi-python3 因為我們使用的是python3 的基礎鏡像
說明
plotly-dash 功能很強大,開發模型也比較簡單,后邊會寫一些相關的學習
參考資料
https://stackoverflow.com/questions/35460816/uwsgi-http-is-ambiguous
https://uwsgi-docs.readthedocs.io/en/latest/Python.html
https://dash.plot.ly/installation
https://github.com/rongfengliang/plot_dash_docker_running