Streamlit: Simplifying the Creation of Web Apps for AI and Data

Streamlit: Simplifying the Creation of Web Apps for AI and Data

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

  1. Easy to Use: Streamlit lets you build a fully functional web interface without needing in-depth web development knowledge.
  2. Fast and Iterative: As you modify your code, Streamlit automatically updates the app, enabling fast prototyping and immediate feedback.
  3. Interactivity: It supports interactive components such as sliders, buttons, checkboxes, file uploaders, etc., allowing users to interact directly with your data or models.
  4. Seamless Python Integration: Streamlit integrates smoothly with popular Python libraries like Pandas, NumPy, Matplotlib, Seaborn, Scikit-learn, TensorFlow, and others, making it easy to visualize data and integrate it into machine learning workflows.

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

  1. Install Virtualenv: If you don’t have virtualenv installed yet, install it using the following command:

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:

  1. Streamlit: Install Streamlit, which will be the core of our application:

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.

  1. In the terminal, run:

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

  1. Keep Code Simple: While Streamlit allows you to build complex apps, it’s important to keep the code organized. Break down the logic into modular functions for easy maintenance.
  2. Maximize User Interaction: Make the most of Streamlit’s interactive features like sliders, buttons, and selectors. This improves user experience by allowing them to adjust parameters and interact with data in real-time.
  3. Documentation: Always document your apps and widgets, explaining what they do—especially if others will interact with your app. Streamlit makes it easy to add descriptions using the st.markdown() function to include markdown text.
  4. Simple Deployment: Streamlit Cloud (formerly Streamlit Sharing) makes it easy to deploy apps, allowing you to share your apps with the world in minutes. For more complex apps, you can use services like Heroku, AWS, or Google Cloud.

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!


要查看或添加评论,请登录

Juliano Souza的更多文章

社区洞察

其他会员也浏览了