Getting started with Streamlit : Creating a simple web app

Follow on  Blogger  |  LinkedIn

Introduction

Streamlit is a powerful and user-friendly framework for creating interactive web applications with Python. It’s especially useful for data scientists and machine learning practitioners who want to visualize their data or share their models with minimal coding effort.

This tutorial helps you to understand the basics of Streamlit or get started with Streamlit. I have included only app.py file, where you can find the code.

Github Repository: Getting started with Streamlit 

Prerequisites

  • Basic knowledge of Python programming.
  • Familiarity with data manipulation and visualization libraries (e.g., Pandas, Matplotlib).
  • Python installed on your system (Python 3.6+ recommended).

Description

In this tutorial we will understand the basics of streamlit where we will try to explore different components of streamlit with practical implementation.

Dependencies

  • Create Virtual Environment
  • Install all the dependencies from requirements.txt

Installation

  1. Install Streamlit: Open your terminal or command prompt and run:
  2. pip install streamlit

  3. Verify Installation: To ensure Streamlit is installed correctly, run:
  4. streamlit --version

Creating a Simple Streamlit App

  1. Set Up Your Project: Create a new directory for your project and navigate into it:
  2. mkdir streamlit_app
    cd streamlit_app

  3. Create a Python Script: Create a new Python file named app.py:
  4. touch app.py

  5. Write Your First App: Open app.py in your favorite text editor and add the following code:
  6. import streamlit as st
    
    # Title of the app
    st.title('My First Streamlit App')
    
    # Add a text input box
    name = st.text_input('Enter your name:')
    
    # Display the input text
    if name:
        st.write(f'Hello, {name}!')

  7. Run Your Streamlit App: Back in your terminal, run:
  8. streamlit run app.py

    This command will start a local server and open the app in your default web browser.

Building Interactive Widgets

Streamlit provides a variety of widgets to make your app interactive. Here’s how to use some of them:

  1. Button:
  2. if st.button('Click Me'):
        st.write('Button clicked!')

  3. Slider:
  4. number = st.slider('Select a number', 0, 100)
    st.write(f'You selected: {number}')

  5. Select Box:
  6. option = st.selectbox('Choose an option', ['Option 1', 'Option 2', 'Option 3'])
    st.write(f'You selected: {option}')

  7. Checkbox:
  8. agree = st.checkbox('I agree')
    if agree:
        st.write('You agreed!')

Displaying Data

Streamlit makes it easy to display data and visualizations:

  1. Displaying a DataFrame:
  2. import pandas as pd
    
    data = {'Column1': [1, 2, 3], 'Column2': [4, 5, 6]}
    df = pd.DataFrame(data)
    st.write(df)

  3. Displaying Charts:
  4. import matplotlib.pyplot as plt
    
    fig, ax = plt.subplots()
    ax.plot([1, 2, 3], [4, 5, 6])
    st.pyplot(fig)

Customizing Your App

  1. Add Sidebar:
  2. st.sidebar.title('Sidebar')
    st.sidebar.write('This is the sidebar')

  3. Add a Markdown:
  4. st.markdown('### Markdown Title')
    st.markdown('*This is italicized text*')

  5. Add a Footer:
  6. st.write('---')  # Creates a horizontal line
    st.write('Footer content here')

Conclusion

Congratulations! You’ve just created your first Streamlit app. With Streamlit, you can easily build interactive data applications and share your work with others.

Follow on  Blogger  |  LinkedIn

Post a Comment

Previous Post Next Post