plotly-dash 简单使用(一)

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

原文地址:https://www.cnblogs.com/rongfengliang/p/11390650.html