Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

A Step-by-Step Guide to Developing a Streamlit Application with Redis Data Storage and Deploying it Using Docker
Latest   Machine Learning

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

Image by Gerd Altmann from Pixabay

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

  1. Pre-requisites
  2. Setting up the environment
  3. Creating a Streamlit App
  4. Defining a Docker image
  5. Building and Running the Docker Image using a Docker compose file
  6. 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

  1. Redis, https://redis.io/
  2. Streamlit, https://streamlit.io/
  3. 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

Feedback ↓