I tried to make a machine learning application with Dash (+ Docker) part1 ~ Environment construction and operation check ~

Introduction

Motivation

While studying machine learning, I wanted to create something, and I also wanted to touch Plotly, a visualization tool, so I worked on developing an app with Dash, which uses Plotly. The actually created deliverable will be here. I wrote this article for the purpose of keeping a learning record. I am a beginner in application development, but thank you.

Target audience

I hope it will be helpful for those who are interested in application development with Dash, or those who want to make deliverables related to machine learning and data science but are wondering what to make. It is assumed that you have knowledge of Docker. About Docker, [Kame-san's blog](https://datawokagaku.com/category/%e8%ac%9b%e5%ba%a7%e4%b8%80%e8%a6%a7/docker%e8% b6% 85% e5% 85% a5% e9% 96% 80 /) (Udemy course if you want to learn deeply) is easy to understand and recommended.

What is Dash?

Dash is one of Python's web application frameworks. It is an open source framework developed by Plotly, and can be used in other languages such as R. Django, flask, etc. are famous in Python's web application framework, but Dash is characterized by ** specializing in visualization **. Flask, Plotly.js, and React.js are running on the back end of Dash, and the core Plotly.js is a JavaScript-based visualization tool that allows you to draw interactive diagrams. In summary, Dash makes it easy to create dynamic dashboard apps without any ** knowledge of JavaScript **. Even if you explain it in words, it's hard to get an image, so please take a look at the sample app published on the Official Site. There are many machine learning apps, and the source code is available on GitHub, but not all, so I think it will be useful as one of the output methods for machine learning.

Environment

The environment was built with Docker by referring to Article of this person. Prepare the following files in the directory for the Dash application ('Dash_App' this time).

<Directory structure>
Dash_App/
  ├ app.py
  ├ Dockerfile
  └ docker-compose.yml

Dockerfile


FROM python:3.7.3
USER root

RUN apt-get update
RUN apt-get install -y vim less
RUN apt-get install -y zsh less
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools

RUN pip install \
    dash==1.16.3 \
    pandas \
    category_encoders \
    scikit-learn

ENV LANG ja_JP.UTF-8
ENV LANGUAGE ja_JP:ja
ENV LC_ALL ja_JP.UTF-8

EXPOSE 5050
CMD ["bash"]

docker-compose.yml


version: '3'
services:
  dash-app:
    restart: always
    build: .
    container_name: 'dash-app'
    ports:
      - "5050:5050"
    working_dir: '/root/'
    tty: true
    volumes:
      - .:/root/src/
      - ~/.zshrc:/root/.zshrc

I will write the contents of the application in app.py. First of all, to check the operation, copy the code in the official tutorial.

app.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(host='0.0.0.0', port=5050, debug=True)

Note that host = '0.0.0.0' and port = 5050 are added and specified in the code part (bottom line) that starts the server. After creating the file, start the container with the following command.

$ docker-compose up -d --build

Once the container is up, enter the container and run app.py as in the following command. * Dash-app is the container name specified in docker-compose.yml.

$ docker-compose exec dash-app zsh -c "python src/app.py"

If there is no problem, the app will start. Access localhost: 5050 from your browser, and if the application screen shown below is displayed, it is successful. * Click "control + C" to exit the app. スクリーンショット 2020-11-12 0.27.48.png

in conclusion

I introduced how to build a Dash environment with a Docker container (although I just modified the Dockerfile a little while looking at another article ...). Docker is convenient because you can quickly build the most troublesome environment. In Next article (part2), I would like to introduce how to write app.py by focusing on the basic part while excerpting from the official tutorial. .. Thank you very much.

Recommended Posts

I tried to make a machine learning application with Dash (+ Docker) part1 ~ Environment construction and operation check ~
I tried to make a machine learning application with Dash (+ Docker) part3 ~ Practice ~
I tried to make a machine learning application with Dash (+ Docker) part2 ~ Basic way of writing Dash ~
I tried to build a Firebase application development environment with Docker in 2020
I tried to create a padrino development environment with Docker
[Rails 6.0, Docker] I tried to summarize the Docker environment construction and commands necessary to create a portfolio
[iOS] I tried to make a processing application like Instagram with Swift
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
I tried to make an introduction to PHP + MySQL with Docker
I tried to create a java8 development environment with Chocolatey
I tried to modernize a Java EE application with OpenShift.
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
Build a Node-RED environment with Docker to move and understand
I tried to develop a web application from a month and a half of programming learning history
Easy to make LINE BOT with Java Servlet Part 2: I tried image messages and templates
I tried to build the environment of PlantUML Server with Docker
I tried to make an Android application with MVC now (Java)
I tried to check the operation of gRPC server with grpcurl
I tried to build a laravel operating environment while remembering Docker
I tried to make a group function (bulletin board) with Rails
After learning Progate, I tried to make an SNS application using Rails in the local environment
I tried to take a look at the flow of Android development environment construction with Android Studio
Build TensorFlow operation check environment with Docker
I built a rails environment with docker and mysql, but I got stuck
I tried to make a simple face recognition Android application using OpenCV
I tried to make a Web API that connects to DB with Quarkus
Creating a java web application development environment with docker for mac part1
I made a virtual currency arbitrage bot and tried to make money
[Copy and paste] Build a Laravel development environment with Docker Compose Part 2
[Rails API x Docker] Easy environment construction with shell & operation check with Flutter
I tried to make a talk application in Java using AI "A3RT"
Create a java web application development environment with docker for mac part2
I tried to create a portfolio with AWS, Docker, CircleCI, Laravel [with reference link]
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to make Basic authentication with Java
A story when I tried to make a video by linking Processing and Resolume
I built a Java EE environment on AWS and tried running a web application
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished)
Rails6 I want to make an array of values with a check box
[Android] I tried to make a material list screen with ListView + Bottom Sheet
Prepare a scraping environment with Docker and Java
A reminder of Docker and development environment construction
I tried to clone a web application full of bugs with Spring Boot
[Introduction to Docker] Create a Docker image for machine learning and use Jupyter notebook
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world
I tried to break a block with java (1)
I built a CentOS 8 environment with Vagrant and tried to sync_folder, but I got an error, so I solved it.
I tried to make a web application that searches tweets with vue-word cloud and examines the tendency of what is written in the associated profile
I tried to create a shopping site administrator function / screen with Java and Spring
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished version ②)
I tried to check the operation of http request (Put) with Talented API Tester
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
I tried using Wercker to create and publish a Docker image that launches GlassFish 5.
I tried to make a message function of Rails Tutorial extension (Part 1): Create a model
Introduction to Machine Learning with Spark "Price Estimate" # 3 Make a [Price Estimate Engine] by learning with training data
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
JavaFX application development with IntelliJ IDEA and Gradle ~ From environment construction to sample code ~
[Azure] I tried to create a Java application for free ~ Connect with FTP ~ [Beginner]
I tried to read and output CSV with Outsystems
I started MySQL 5.7 with docker-compose and tried to connect