Streamlit: Simplifying the Creation of Web Apps for AI and Data
Juliano Souza
Director of Information Technology | Technology Mentor for Startups in the EMEA Region.
What is Streamlit?
Streamlit is an open-source Python framework that makes it easy to create interactive, dynamic web applications for data science and machine learning projects. Instead of building a web interface from scratch, which can be complex and often requires frontend skills (such as JavaScript, HTML, and CSS), Streamlit allows you to write Python code and quickly turn your scripts into a shareable web app.
It is widely used by data scientists, machine learning engineers, and analysts to share data insights, demonstrate AI models, or create interactive dashboards with minimal effort.
Key Benefits of Streamlit
How Streamlit Works
Unlike other web development frameworks, such as Flask or Django, which require a clear separation between backend (server logic) and frontend (user interface), Streamlit lets you write a single Python script. Streamlit then handles both backend and frontend interactions automatically.
You write your code just like a typical data analysis script, add a few commands for interactivity (like sliders or buttons), and Streamlit turns it into a fully interactive web app.
When to Use Streamlit
Interactive Dashboards: Ideal for creating dashboards that feature dynamic data visualizations and interactive plots.
AI/ML Model Demonstrations: Use Streamlit to demonstrate how your machine learning models make predictions or analyze data in real-time.
Rapid Prototyping: When you need to quickly share data analysis or machine learning models with a client or team, Streamlit lets you create functional prototypes in minutes.
Data-Driven Applications: Enable users to upload datasets, adjust analysis parameters, or interact with AI models directly from a web interface.
Tutorial: Building a Data Profiling App with Streamlit and ydata-profiling
Now that we understand what Streamlit is, let’s build a real application that performs data profiling—an automatic exploratory analysis of a dataset—using the ydata-profiling library (formerly known as pandas-profiling).
Step 1: Setting Up the Environment with Virtualenv
pip install virtualenv
2. Create a Virtual Environment: In the folder where you want to create your project, run the following command to create a virtual environment:
virtualenv venv
3. Activate the Virtual Environment: To activate the virtual environment, use one of the following commands:
Linux/macOS:
source venv/bin/activate
Now, the name of the virtual environment (e.g., venv) should appear in your terminal/prompt.
Step 2: Installing the Required Libraries
With the virtual environment activated, let's install the necessary libraries:
pip install ydata-profiling
Step 3: Creating the Application
Now, let’s create the main file for our app called app.py.
Inside it, add the following code:
import streamlit as st
import pandas as pd
from ydata_profiling import ProfileReport
# App title
st.title("AI-Powered Data Profiling App")
# File uploader
uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
# If the file is uploaded, show its content and generate a profiling report
if uploaded_file is not None:
data = pd.read_csv(uploaded_file)
st.write("Dataset Preview:", data.head())
# Generate the profiling report
领英推荐
profile = ProfileReport(data, title="Pandas Profiling Report", explorative=True)
# Display the report in the app as HTML
st_profile_report_html = profile.to_html()
st.components.v1.html(st_profile_report_html, height=1000, scrolling=True)
else:
st.write("Please upload a CSV file to continue.")
Step 4: Running the Application
Now you’re ready to run the application.
streamlit run app.py
2. What happens: This will open your default web browser with the Streamlit interface. You’ll see the app title and an interface where you can upload a CSV file. Once you upload the file, Streamlit will display a full data profiling analysis, including statistical insights, correlation plots, and more.
Best Practices for Using Streamlit
This command will bring up a web interface to manage csv data into graphs and cubes/approaches;
Let's retrieve some csv dataset from wine database:
https://archive.ics.uci.edu/dataset/186/wine+quality
Now select both csv files:
Some insights from web interface are:
Conclusion
Streamlit is a powerful tool to turn data scripts into interactive, dynamic web applications. With it, you can create interactive dashboards, demonstrate AI models, and share your data insights in a professional and accessible manner. By following this tutorial, you can quickly start building your own customized applications, and with good practices, take your data-driven projects to a wider and more diverse audience.
You can use any CSV or data, create your web app and deploy it in any infrastructure!