Streamlit: A Tool for Rapid Data Application Development

Have you ever thought that with just a few lines of code, you can turn your Python script into a beautiful web application? Whether it’s data visualization, showcasing machine learning models, or developing interactive tools, Streamlit allows you to achieve this easily.

Today, let’s get to know Streamlit, a tool designed for data scientists and developers to rapidly build web applications.

What is Streamlit?

Streamlit is an open-source Python library focused on enabling users to convert Python scripts into interactive web applications in the fastest and simplest way. Its design philosophy is “code-centric”, meaning you only need to write Python code without needing knowledge of HTML, CSS, or JavaScript to quickly create a powerful data application.

Why Use Streamlit?

Suppose you have developed a machine learning model and want to share the results with your team. With Streamlit, you only need to write a few lines of code to visualize the model’s results, allowing others to interact through a webpage in real-time without needing to learn complex web development technologies.

The advantages of Streamlit include:

  1. 1. Rapid Development: Build a usable application in just a few minutes.

  2. 2. Simple and Intuitive: Completely based on Python, no front-end development knowledge required.

  3. 3. Highly Interactive: Supports various interactive components such as buttons, sliders, and text boxes.

  4. 4. Great for Data Presentation: Built-in visualization tools to easily display data and models.

Getting Started with Streamlit

  1. 1. Install Streamlit First, install Streamlit:

    pip install streamlit
  2. 2. Create a Simple Application Create a file named app.py and write the following code:

    import streamlit as st
    
    st.title("Hello, Streamlit!")
    st.write("This is a simple Streamlit application")
  3. 3. Run the Application Run the following command in the terminal:

    streamlit run app.py

    Your browser will automatically open and display your application.

Core Features of Streamlit

  1. 1. Display Titles and Text Streamlit provides various methods to display text:

    st.title("Application Title")
    st.header("This is a small title")
    st.subheader("This is a sub-title")
    st.text("This is plain text")
    st.markdown("**Markdown** format support")  # Supports Markdown
  2. 2. Interactive Components Streamlit supports various interactive components like buttons, sliders, and radio buttons:

    # Button
    if st.button("Click Me"):
        st.write("Button was clicked!")
    
    # Radio button
    option = st.radio("Choose an option", ["Option A", "Option B", "Option C"])
    st.write(f"You selected: {option}")
    
    # Slider
    value = st.slider("Choose a number", 0, 100, 50)
    st.write(f"Slider value: {value}")
  3. 3. Data Tables and Charts Streamlit can easily display tables and plot charts:

    import pandas as pd
    import numpy as np
    
    # Table
    data = pd.DataFrame({
        "Column 1": [1, 2, 3, 4],
        "Column 2": [10, 20, 30, 40]
    })
    st.write("Data Table:", data)
    
    # Line chart
    chart_data = pd.DataFrame(
        np.random.randn(20, 3),
        columns=["A", "B", "C"]
    )
    st.line_chart(chart_data)
  4. 4. File Upload Streamlit supports users uploading files and processing them:

    uploaded_file = st.file_uploader("Upload a file", type=["csv", "txt"])
    if uploaded_file is not None:
        data = pd.read_csv(uploaded_file)
        st.write(data)
  5. 5. Multi-Page Applications You can easily build multi-page applications using st.sidebar:

    page = st.sidebar.selectbox("Choose a page", ["Home", "About"])
    if page == "Home":
        st.write("This is the home page")
    elif page == "About":
        st.write("This is the about page")

A Comprehensive Example: Stock Data Visualization Tool

Suppose you want to develop an application that allows users to choose stocks and view historical price data. Here’s the complete code:

import streamlit as st
import yfinance as yf
import pandas as pd

# Set title
st.title("Stock Data Visualization Tool")

# Stock selection
stock_symbol = st.text_input("Enter stock code (e.g., AAPL, GOOGL, MSFT)", "AAPL")

# Date selection
start_date = st.date_input("Select start date")
end_date = st.date_input("Select end date")

# Get stock data
if st.button("Get Data"):
    try:
        stock_data = yf.download(stock_symbol, start=start_date, end=end_date)
        st.write(f"Stock data ({stock_symbol}):", stock_data.tail())

        # Plot closing price line chart
        st.line_chart(stock_data["Close"])
    except Exception as e:
        st.error(f"Unable to fetch stock data: {e}")

After running this application, you can enter the stock code and time range to get the historical data and price charts in real-time.

Practical Applications of Streamlit

  1. 1. Data Analysis and Presentation Quickly create data dashboards to showcase analysis results.

  2. 2. Machine Learning Model Display Showcase trained models through interactive interfaces, such as classification predictions or recommendation systems.

  3. 3. Educational Tools Create simple interactive tools to help students learn math, statistics, or programming knowledge.

  4. 4. Rapid Prototyping Quickly build prototypes to share or test features with your team.

Pros and Cons of Streamlit

Pros:

  • • Rapid development, focused on functionality.

  • • Supports a rich set of interactive components, suitable for non-technical users.

  • • Easy to integrate with data science and machine learning projects.

  • • Completely open-source with active community support.

Cons:

  • • Suitable for data presentation; complex applications require more professional frameworks.

  • • Limited support for extensive front-end customization.

Conclusion

Streamlit is a simple and efficient web application development tool, very suitable for data scientists and Python developers to quickly build interactive applications. If you are interested in data visualization, machine learning model deployment, or tool development, Streamlit would be a great choice.

Try developing your data application with Streamlit today!

Leave a Comment