Top 10 Streamlit API Solutions for Building Interactive Web Apps

Jennie Lee
8 min readMar 27, 2024

--

Looking for a Postman alternative?

Try APIDog, the Most Customizable Postman Alternative, where you can connect to thousands of APIs right now!

Introduction: Building an API-Driven Streamlit Python Microservice on AWS

In today’s digital world, building interactive web applications that provide a seamless user experience is crucial for businesses to thrive. These web applications often rely on APIs (Application Programming Interfaces) to enable communication between various software components. Streamlit, a powerful Python library, offers a user-friendly and efficient way to build data-driven web applications, making it an excellent choice for developers.

This article serves as a comprehensive guide on how to build and deploy an API-driven Streamlit Python microservice on Amazon Web Services (AWS). The motivation behind this project was to add a new feature to a trading platform by building it as a web app API-based microservice. By leveraging Streamlit and AWS services, we can create an efficient and scalable solution.

Before diving into the implementation details, we need to understand the platform’s API and gather the necessary parameters.

Understanding the Platform’s API and Gathering Parameters

The first step in building an API-driven microservice is to familiarize ourselves with the platform’s API documentation. This documentation provides crucial information about the available endpoints, authentication requirements, and input/output parameters.

By carefully studying the API documentation, we can identify the specific parameters needed to achieve our desired functionality within the web application. These parameters may include authentication tokens, request payloads, or query parameters. Gathering this information upfront will simplify the development process and ensure the correct implementation.

Once we have a clear understanding of the API’s parameters, we can proceed to build the initial command-line interface (CLI) version and test the API parameters.

Building the CLI Version and Testing the API Parameters

To ensure a smooth development process, it is recommended to start building a CLI version of the microservice. This allows us to test the individual API parameters and verify their correctness in a controlled environment.

Using a programming language like Python, we can utilize libraries such as requests to make HTTP requests to the API endpoints. By constructing the requests with the appropriate parameters, we can validate the API responses and troubleshoot any errors or inconsistencies.

During this testing phase, it is crucial to handle various scenarios, including success responses, error responses, and edge cases. By thoroughly testing the API parameters in this isolated development environment, we can ensure the correctness of the microservice’s functionality.

Introducing Streamlit Framework for Building Web App Version

After successfully testing the API parameters in the CLI version, we can now move on to building the web application version of our microservice. This is where Streamlit comes into play.

Streamlit is a powerful Python library that simplifies the process of building web applications and data visualizations. Its intuitive and declarative syntax allows developers to focus on the core functionality of the application without worrying about the underlying web technologies.

Streamlit seamlessly integrates with popular Python libraries such as Pandas, Matplotlib, and TensorFlow, enabling us to leverage their capabilities in our web application. By utilizing Streamlit’s functions and decorators, we can create interactive and responsive user interfaces with minimum effort.

Moreover, Streamlit provides features like caching mechanisms, real-time data updates, and deployment options, making it an ideal choice for building data-driven web applications.

Steps to Build the Streamlit Microservice

Building the Streamlit microservice involves several steps. Let’s go through each of them in detail.

Step 1: Creating an Amazon ECR Repository

Amazon Elastic Container Registry (ECR) provides a secure and scalable option for storing Docker container images. In this step, we need to create an ECR repository to store our Docker base image.

  1. Open the AWS Management Console and navigate to the Amazon ECR service.
  2. Click on “Create repository” and specify a name for your repository.
  3. Configure the repository settings, such as encryption, image scanning, and lifecycle policies, as per your requirements.
  4. Click on “Create repository” to create the ECR repository.

Step 2: Pushing the Docker Base Image to ECR

Once the ECR repository is set up, we can now push our Docker base image to it. The Docker base image serves as the foundation for our web application container.

  1. Build the Docker base image using the appropriate Dockerfile, including all the necessary dependencies and libraries.
  2. Login to the ECR repository using the AWS CLI by running the command: aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <your-account-id>.dkr.ecr.<your-region>.amazonaws.com.
  3. Tag the Docker image with the ECR repository URI using the command: docker tag <image-id> <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/<repository-name>:<tag>.
  4. Push the Docker image to the ECR repository using the command: docker push <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/<repository-name>:<tag>.

Step 3: Creating a GitHub Repository and Pushing Source Code

To maintain version control and enable collaborative development, it is recommended to use a version control system like Git. In this step, we will create a GitHub repository and push our microservice’s source code to it.

  1. Create a new GitHub repository or navigate to an existing one.
  2. Initialize a local Git repository using the command: git init.
  3. Add the necessary files and commit them using the commands: git add . and git commit -m "Initial commit".
  4. Add the GitHub repository as a remote origin using the command: git remote add origin <github-repo-url>.
  5. Push the source code to the GitHub repository using the command: git push origin master.

Step 4: Creating a Dockerfile and Configuring CICD Pipeline

To automate the build and deployment process, we can utilize AWS CodePipeline, a fully managed continuous delivery service. In this step, we will create a Dockerfile for our web application and configure the CICD pipeline using AWS CodePipeline.

  1. Create a Dockerfile in your local project directory with the necessary instructions to build the Streamlit web application.
  2. Commit the Dockerfile to your Git repository.
  3. Open the AWS Management Console and navigate to the AWS CodePipeline service.
  4. Click on “Create pipeline” and specify a name for your pipeline.
  5. Configure the source settings by selecting your GitHub repository and specifying the branch to monitor.
  6. Specify the build settings by selecting AWS CodeBuild as the build provider and configuring the necessary build specifications.
  7. Configure the deployment settings by selecting Amazon Elastic Compute Cloud (EC2) as the deployment provider and specifying the necessary deployment options.
  8. Review the pipeline settings and click on “Create pipeline” to create the CICD pipeline.

Step 5: Deployment Options on AWS (AWS App Runner, Fargate, EC2)

AWS provides multiple compute options for deploying your containerized Streamlit microservice. Depending on your requirements and preferences, you can choose from the following:

  • AWS App Runner: A fully managed service that automatically builds and deploys containerized applications.
  • AWS Fargate: A serverless compute engine for containers that allows you to deploy and manage containers without managing the underlying infrastructure.
  • Amazon EC2: Launching EC2 instances and managing the container deployment manually.

Evaluate each deployment option based on factors like scalability, cost, management overhead, and resource requirements. Select the most suitable option for your project and proceed with the deployment.

Creating Resources for Web App Deployment

To deploy the Streamlit microservice as a web application, we need to create several resources on AWS.

Creating an EC2 Instance as Host

In this step, we will create an Amazon EC2 instance to host the Streamlit microservice container.

  1. Open the AWS Management Console and navigate to the Amazon EC2 service.
  2. Launch a new EC2 instance by selecting the appropriate Amazon Machine Image (AMI), instance type, storage options, and network settings.
  3. Configure the security groups to allow inbound traffic on the required ports (e.g., port 80 for HTTP).
  4. Review the instance details and launch the EC2 instance.
  5. Connect to the EC2 instance using SSH and ensure that Docker is installed.

Setting Up Application Load Balancer (ALB)

An Application Load Balancer (ALB) enables traffic distribution across multiple instances of the Streamlit microservice container, ensuring high availability and scalability.

  1. Open the AWS Management Console and navigate to the Amazon EC2 service.
  2. Create a new Application Load Balancer and configure the necessary settings, such as listeners, target groups, and routing rules.
  3. Add the EC2 instance(s) hosting the Streamlit microservice as target(s) in the target group.
  4. Configure the ALB’s security groups to allow inbound traffic on the required ports (e.g., port 80 for HTTP).

Creating Build Project and CodeDeploy Application

AWS CodeBuild and AWS CodeDeploy are crucial components for automating the build and deployment process.

  1. Open the AWS Management Console and navigate to the AWS CodeBuild service.
  2. Create a new build project by specifying the source settings, build specifications, and optional environment settings.
  3. Open the AWS Management Console and navigate to the AWS CodeDeploy service.
  4. Create a new application and specify the deployment settings, such as deployment groups, revision locations, and deployment configurations.

Configuring CodePipeline for CICD

AWS CodePipeline acts as the orchestrator for the entire CICD process, connecting the source, build, and deploy stages seamlessly.

  1. Open the AWS Management Console and navigate to the AWS CodePipeline service.
  2. Create a new pipeline and configure the source settings by selecting the appropriate source provider (e.g., GitHub) and repository details.
  3. Specify the build settings by selecting AWS CodeBuild as the build provider and configuring the necessary build specifications.
  4. Configure the deployment settings by selecting AWS CodeDeploy as the deployment provider and specifying the necessary deployment options.
  5. Review the pipeline settings and click on “Create pipeline” to create the CICD pipeline.

Publishing the Web App using CloudFront and Route 53

To make the web application accessible to users, we can leverage Amazon CloudFront and Route 53 to provide reliable and efficient content delivery.

Optional Usage of CloudFront

Amazon CloudFront is a content delivery network (CDN) that caches and delivers web content from edge locations globally, resulting in low latency and improved performance. If you choose to use CloudFront, follow these steps:

  1. Open the AWS Management Console and navigate to the Amazon CloudFront service.
  2. Create a new distribution and configure the necessary settings, such as the origin, cache behaviors, and SSL certificates.
  3. Update the DNS configuration to point to the CloudFront distribution.

Steps for Publishing Web App

If you decide not to use CloudFront and directly use Route 53 for DNS management, follow these steps:

  1. Open the AWS Management Console and navigate to the Amazon Route 53 service.
  2. Create a hosted zone for your domain and configure the necessary DNS records, such as A, AAAA, and CNAME.
  3. Specify the appropriate values for each DNS record, including the ALB DNS name.

After completing these steps, your Streamlit microservice web application will be published and accessible to users.

Conclusion: Benefits of Microservice Architecture and CI/CD with AWS CodePipeline

In conclusion, building an API-driven Streamlit Python microservice on AWS offers numerous benefits, including scalability, flexibility, and efficient resource utilization. By adopting a microservice architecture, we can modularize our application, making it easier to develop, test, and maintain.

Integrating continuous integration and continuous deployment (CI/CD) with AWS CodePipeline enables an automated and streamlined development workflow. It eliminates the manual steps involved in building, testing, and deploying our microservice and ensures the rapid delivery of new features and updates.

Furthermore, leveraging AWS services like ECR, EC2, ALB, CloudFront, and Route 53 provides a scalable and reliable infrastructure for hosting and delivering our Streamlit microservice web application.

By following the steps outlined in this guide, you can successfully build and deploy an API-driven Streamlit Python microservice on AWS, taking full advantage of the Streamlit framework and AWS’s robust suite of services. With this knowledge and experience, you can create interactive and data-driven web applications that cater to the needs and preferences of your users.

Looking for a Postman alternative?

Try APIDog, the Most Customizable Postman Alternative, where you can connect to thousands of APIs right now!

--

--

Jennie Lee
Jennie Lee

Written by Jennie Lee

Software Testing Blogger, #API Testing

No responses yet