Streamlit in Python

Streamlit is an open-source Python library that makes it easy to create custom web apps for machine learning and data science. With Streamlit, you can turn data scripts into shareable web apps in just a few lines of code. It provides a simple way to create interactive applications and dashboards.



First, you need to install Streamlit using pip:

pip install streamlit

Basic Example

Here’s a basic example to demonstrate how you can use Streamlit to create a simple app.

import streamlit as st

st.title('My First Streamlit App')

user_name = st.text_input("Enter your name:")
if st.button('Say Hello'):
    st.write(f'Hello {user_name}!')

Save the code above in a file, say, and run it using the following command in your terminal:

streamlit run


  • st.title('My First Streamlit App'): This line of code adds a title to the app.
  • user_name = st.text_input("Enter your name:"): This line creates a text input box where the user can enter their name.
  • if st.button('Say Hello'):: Adds a button and checks whether it’s been clicked.
  • st.balloons(): When the button is clicked, balloons animation will appear on the screen.
  • st.write(f'Hello {user_name}!'): Outputs a personalized greeting.


  • Easy to Use: You can create web apps with just a few lines of Python code.
  • Customizable: Although Streamlit is easy for beginners, it’s also customizable for advanced users.
  • Interactive Widgets: Streamlit includes interactive widgets for user input, including sliders, buttons, and text input.
  • Data Integration: It easily integrates with Pandas, Numpy, and other data science libraries.
  • Visualizations: You can integrate charts and graphs using Matplotlib, Plotly, and other visualization libraries.
  • Machine Learning: It supports displaying predictions and results from machine learning models.
  • Sharing Insights: It helps in sharing insights and results from data analysis and machine learning models with stakeholders in a visually appealing manner.

More Advanced Use-Cases:

  • Data Exploration Apps: Create apps to explore datasets and share insights.
  • Machine Learning Apps: Build and share machine learning models.
  • Dashboard and Reporting Apps: Develop custom dashboards and reports.
  • Prototyping: Quickly prototype new app ideas and interfaces.

Mini Project: A Simple Book Recommender App using Streamlit


In this mini-project, let’s create a simple book recommender application using Streamlit. We will use a small dataset for simplicity. The user will input a book title, and the app will recommend a similar book.

Step 1: Installation

Ensure Streamlit is installed in your Python environment:

pip install streamlit

Step 2: Prepare the Data

Create a small dataset containing book titles and their genres. In real-world scenarios, you could use a large dataset and more sophisticated recommendation algorithms. Here’s a simple CSV (books.csv) example:

Harry Potter,Fantasy
To Kill a Mockingbird,Fiction
The Great Gatsby,Fiction
The Hobbit,Fantasy

Step 3: Create the Streamlit App

Now, create a Python file (e.g., and build the app.

import streamlit as st
import pandas as pd
import random

# Load Data
def load_data():
    return pd.read_csv("books.csv")

data = load_data()

# UI
st.title("Simple Book Recommender")
selected_book = st.selectbox("Select a book:", data['title'].values)

# Recommend a Book
if st.button("Recommend"):
    recommended_books = data[data['genre'] == data[data['title'] == selected_book]['genre'].values[0]]['title'].values
    recommended_books = [book for book in recommended_books if book != selected_book]

    if recommended_books:
        st.write(f"If you liked {selected_book}, you might also like: {random.choice(recommended_books)}")
        st.write("Sorry, no recommendations available for this book.")


  • Load Data: Load the book data using Pandas and cache it with @st.cache to enhance performance.
  • UI: Use Streamlit functions to create a simple UI that allows users to select a book.
  • Recommend a Book: When the “Recommend” button is clicked, find a book of the same genre and display it as a recommendation.

Step 4: Run the App

Run the Streamlit app with the following command in your terminal:

streamlit run

Step 5: Interaction

Interact with the app in the browser, select a book, and get a simple recommendation based on the genre.


This is a very basic recommender system and serves just as a starting point. Real-world recommender systems use more sophisticated algorithms and larger datasets. You might use collaborative filtering, content-based filtering, or hybrid methods, and possibly apply machine learning algorithms to make actual recommendations in a full-scale project. This example is intended to demonstrate the simplicity and capabilities of Streamlit in creating interactive web applications with Python.

To learn more, you can visit the Streamlit documentation for tutorials, examples, and advanced usage tips!

Leave a comment