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
- Install Streamlit: Open your terminal or command prompt and run:
- Verify Installation: To ensure Streamlit is installed correctly, run:
pip install streamlit
streamlit --version
Creating a Simple Streamlit App
- Set Up Your Project: Create a new directory for your project and navigate into it:
- Create a Python Script: Create a new Python file named app.py:
- Write Your First App:
Open
app.py
in your favorite text editor and add the following code: - Run Your Streamlit App: Back in your terminal, run:
mkdir streamlit_app
cd streamlit_app
touch app.py
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}!')
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:
- Button:
- Slider:
- Select Box:
- Checkbox:
if st.button('Click Me'):
st.write('Button clicked!')
number = st.slider('Select a number', 0, 100)
st.write(f'You selected: {number}')
option = st.selectbox('Choose an option', ['Option 1', 'Option 2', 'Option 3'])
st.write(f'You selected: {option}')
agree = st.checkbox('I agree')
if agree:
st.write('You agreed!')
Displaying Data
Streamlit makes it easy to display data and visualizations:
- Displaying a DataFrame:
- Displaying Charts:
import pandas as pd
data = {'Column1': [1, 2, 3], 'Column2': [4, 5, 6]}
df = pd.DataFrame(data)
st.write(df)
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])
st.pyplot(fig)
Customizing Your App
- Add Sidebar:
- Add a Markdown:
- Add a Footer:
st.sidebar.title('Sidebar')
st.sidebar.write('This is the sidebar')
st.markdown('### Markdown Title')
st.markdown('*This is italicized text*')
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