playgrdstar
quaintitative
Published in
2 min readAug 31, 2018

--

Setting Up a Data Lab Environment — Part 6 — Serve a Flask

[Post is also available at quaintitative.com]

Adding Flask to the mix isn’t strictly required for this. But I think it’s great to be able to

  • do the analysis;
  • save the data to either the Postgres or Mongo database; and
  • serve the analysis from a Flask server

I explained how to set up a Flask server in a previous post. Here, we show how to use Docker to do the same.

First, we have to add Flask to the docker-compose.yml file.

flaskapp:
build: docker/flask
ports:
- "80:80"
volumes:
- ./docker/flask/app:/app
- ./data:/app/data
environment:
- FLASK_APP=main.py
- FLASK_DEBUG=1
- 'RUN=flask run --host=0.0.0.0 --port=80'
command: flask run --host=0.0.0.0 --port=80

We build an image from Dockerfile in the folder ‘docker/flask’ (which I will go through next); connect the container’s port 80 to port 80 in the outside world, map the volumes, and then set the variables and commands needed to get Flask up and running.

Next, we create a folder for flask in the docker folder that we had created previously. Within it, we create an app folder, and a Dockerfile.

In the Dockerfile, we pull a Docker image, and then install some libraries and copy the Flask app files from the local machine to the container’s app folder.

FROM tiangolo/uwsgi-nginx-flask:python3.6

RUN pip install pymongo
RUN pip install psycopg2
RUN pip install tweepy

COPY ./app /app

And that’s it. You can just adapt the files in the app folder I provided. It goes slightly beyond what I covered on Flask previously, but I will go into more details on Flask in subsequent posts.

The files for this tutorial are available here.

--

--

playgrdstar
quaintitative

ming // gary ang // illustration, coding, writing // portfolio >> playgrd.com | writings >> quaintitative.com