The goal of this project is to demonstrate how to set up a simple application with three components:
- A Python program using
requests
to collect data. - The redis database to store data
- A Flask server launched via gunicorn to server up the data.
The data used in this example is the current number of confirmed COVID-19 cases from the COVID-19 API.
The "application" modeled is intentionally minimal: Every 15 minutes, the collector obtains the current count and stores it in the redis database. The Flask server has a single end point that allows a user to fetch this data.
In the collector
folder:
-
Create a virtual environment for the collector and install the required libraries:
python3 -m venv .venv source .venv/bin/activate pip install -r requirements.txt
-
Create a file
.env
that contains the hostname and port number for Redis. We will run Redis on our laptop (localhost
) using the standard Redis port (6379):REDIS_HOST=localhost REDIS_PORT=6379
In the root directory of the project:
-
Download the latest stable version of Redis
-
Extract the tar ball (archive)
tar xvfz ~/Downloads/redis-6.2.6.tar.gz
-
Go into the redis directory and build Redis
cd redis-6.2.6 make
-
Instal Redis on your system
make install
In the server
folder:
-
Create a virtual environment for the collector and install the required libraries:
python3 -m venv .venv source .venv/bin/activate pip install -r requirements.txt
-
Create a file
.env
that contains the hostname and port number for Redis. We will run Redis on our laptop (localhost
) using the standard Redis port (6379):REDIS_HOST=localhost REDIS_PORT=6379
We will launch the system in three terminal windows:
-
Terminal #1 (In the project root) Launch Redis:
redis-server
-
Terminal #2 (In the
collector
folder) Launch the collector:source .venv/bin/activate python collector.py
-
Terminal #3 (In the
server
folder) Launch the server:source .venv/bin/activate python server.py
In each terminal window, press ctrl-c to stop the program.