A Step-by-Step Guide to Developing a Streamlit Application with Redis Data Storage and Deploying it Using Docker
Last Updated on April 1, 2023 by Editorial Team
Author(s): Rahul Veettil
Originally published on Towards AI.
Deploying a Streamlit App with Docker
Introduction
In this tutorial, we will build a Streamlit app that uses Redis for data storage and deploy it using Docker. Redis[1] is an in-memory data structure store that can be used as a database, cache, and message broker, Streamlit[2] is a popular Python library for creating web apps for data science and machine learning, and Docker[3] is a containerization platform that allows you to package your applications and all their dependencies into a single container.
When it comes to large-scale applications where multiple technologies needed to be used and deployed simultaneously, software engineers and data engineers, make use of architectural patterns to develop each of the applications independently and dockerize them using Docker. The architecture patterns used for such development are called microservice architecture. Microservice architecture has become increasingly popular in recent years, as it allows for the development of complex applications by breaking them down into smaller, more manageable services. This approach offers a number of benefits, including scalability, flexibility, and easier maintenance.
In this tutorial, the objective is to show you building a small web application using Streamlit and Redis so that it is easier for beginners to get started and pave way for developing skills in building large-scale applications.
Table of contents
- Pre-requisites
- Setting up the environment
- Creating a Streamlit App
- Defining a Docker image
- Building and Running the Docker Image using a Docker compose file
- Using the Streamlit application
Prerequisites
Before we begin, you will need to have the following installed:
- Python 3.7 or higher
- Docker
You should also have a basic understanding of Python and web development.
Setting up the Environment
First, we will set up the environment by creating a new directory for our project and creating a virtual environment.
mkdir microservice-website cd microservice-website
python3 -m venv venv
source venv/bin/activate
Next, we will install the necessary dependencies.
pip install streamlit==1.20.0 redis==4.5.3
Creating a Streamlit App
First, we will create a simple Streamlit app that allows users to enter their name and age, and stores this data in the Redis database.
Create a new file named βapp.pyβ and add the following code:
import streamlit as st import redis
r = redis.Redis(host=’redisdb’, port=6379, db=0)
def app():
st.title(‘Welcome’)
name = st.text_input(‘Name and Surname:’)
age = st.number_input(‘Age:’)
if st.button(‘Submit’):
r.set(‘name’, name)
r.set(‘age’, age)
st.success(‘Data is stored successfully!’)
if __name__ = “__main__”:
app()
This code first imports the necessary libraries and connects to the Redis database. We name the Redis database βredisdbβ, and provide Redisβs default port number 6379.
We further define the Streamlit app, which includes a title, text input for the userβs name, number input for the userβs age, and a submit button. When the user clicks the submit button, the app stores the userβs data in the Redis database.
Defining a Docker Image
Now that we have defined our Streamlit app and Redis database, we will package them into a single container using Docker.
Create a new file named βDockerfileβ in the root directory of your project and add the following code:
FROM python:3.9-slim-buster WORKDIR /app COPY . . RUN pip install --no-cache-dir -r requirements.txt EXPOSE 8501 CMD ["streamlit", "run", "app.py", "--server.port", "8501"]
This code specifies that we will use the official Python 3.9 slim-buster image as our base image, sets the working directory to β/appβ, copies all the files in our project to the docker container, installs the necessary dependencies, exposes port 8501, and runs the Streamlit app.
Building and Running the Docker Image using a Docker compose file
To build the Docker image, create a βdocker-compose.ymlβ file.
version: '3.8'
volumes:
data:
services:
redisdb:
image: redis:latest
volumes:
– data:/data
ports:
– 6379:6379
streamlit:
image: streamlit/sl:1
build:
context: .
ports:
– “8501:8501”
entrypoint: streamlit run app.py
Here, we defined two services βredisdbβ and βstreamlitβ, specifying how to build the Redis image (redis:latest), and Streamlit image (streamlit/sl:1). We also specified the corresponding port numbers 6379 and 8501 for Redis database and Streamlit app, respectively.
Letβs finally build our image using the following command
docker-compose up
This will automatically make use of the βDockerfileβ we defined previously and create:
- A Redis container named βredisdbβ, and expose port 6379.
- A Streamlit app making use of our code defined inside the βapp.pyβ file, and expose port 8501.
- A new Docker image named βmicroservice-websiteβ, and link ports 8501 and 6379.
Using the Streamlit application
To use the application, open your browser and navigate to http://localhost:8501 or http://192.168.1.194:8501. You should see the Streamlit app with the input fields for name and age.
Enter a name and age, and click the submit button. This will store your data in the Redis database and show a message that your data is stored successfully.
To verify that your data has been stored, you can run the following command in another terminal:
docker exec -it redisdb redis-cli
This will open the Redis command line interface. You can then run the following command to view all the keys in the database:
keys *
You should see a key with the name you entered, along with its value.
Conclusion
This tutorial explored how to build a web application using Streamlit, Redis, and Docker. We created a Streamlit app that allows users to enter their name and age, and stores this data in a Redis database. We then packaged the Streamlit app and Redis database into a single container using Docker. Finally, we tested the microservice website by entering data into the app and verifying that it was stored in the Redis database.
If you would like to take the lessons learned further, you can consider extending the app to store data retrieved from websites or databases. For example, you can consider scraping movie data from IMDB and store it inside Redis or you can retrieve bioactivity data from chembl and store it inside Redis.
Another idea would be to develop an API using FastAPI to retrieve the data stored within the Redis database. Try to develop it inside a separate container and deploy the whole service as a multi-container dockerized app. You can find more information about this here.
If you liked my article, keep watching, I plan to write on several topics in data science, problem-solving, cancer medicine, and psychology for success.
If you would like to connect, please add me on Linkedin.
References
- Redis, https://redis.io/
- Streamlit, https://streamlit.io/
- Docker, https://www.docker.com/
Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.
Published via Towards AI