close
close
streamlit cheat sheet

streamlit cheat sheet

3 min read 22-10-2024
streamlit cheat sheet

Streamlit Cheat Sheet: A Quick Guide to Building Interactive Web Apps

Streamlit is a popular Python library that allows you to quickly build and share data-driven web applications. Its simplicity and ease of use make it an excellent choice for data scientists, machine learning engineers, and anyone wanting to showcase their work in a visually appealing way. This cheat sheet provides a concise overview of key Streamlit components and functionalities.

Getting Started

  • Installation:
    pip install streamlit
    
  • Running an App:
    streamlit run your_app.py
    

Basic Components

Component Description Code Example
st.title() Displays a title heading. st.title("My Streamlit App")
st.header() Displays a section header. st.header("Data Exploration")
st.subheader() Displays a sub-section header. st.subheader("Dataset Summary")
st.text() Displays plain text. st.text("This is a text output.")
st.markdown() Displays formatted text using Markdown. st.markdown("**Bold text** and *italic*")
st.write() Displays various data types, including strings, numbers, lists, dictionaries, pandas DataFrames, etc. st.write("Hello world!")
st.code() Displays code snippets. st.code('print("Hello World!")')

Input Widgets

Component Description Code Example
st.text_input() Creates a text input field. name = st.text_input("Enter your name")
st.number_input() Creates a numerical input field. age = st.number_input("Enter your age", min_value=0, max_value=120)
st.slider() Creates a slider widget for selecting a numerical value. value = st.slider("Select a value", min_value=0, max_value=100)
st.checkbox() Creates a checkbox for toggling boolean values. show_data = st.checkbox("Show data")
st.radio() Creates a radio button group for selecting one option from a list. option = st.radio("Choose an option", ("A", "B", "C"))
st.selectbox() Creates a dropdown menu for selecting one item from a list. choice = st.selectbox("Select a value", ("Red", "Green", "Blue"))
st.multiselect() Creates a multi-select box for selecting multiple items from a list. selected_values = st.multiselect("Select multiple values", ("Red", "Green", "Blue"))
st.file_uploader() Creates a file upload widget. uploaded_file = st.file_uploader("Upload a file")

Output Components

Component Description Code Example
st.image() Displays images. st.image("path/to/image.jpg")
st.audio() Plays audio files. st.audio("path/to/audio.mp3")
st.video() Plays video files. st.video("path/to/video.mp4")
st.dataframe() Displays pandas DataFrames. st.dataframe(df)
st.table() Displays tabular data. st.table(df)
st.json() Displays JSON data. st.json(data)
st.map() Displays interactive maps using Leaflet. st.map(data)

Advanced Features

  • Caching: Streamlit supports caching data and results to improve app performance.
  • Sessions: Streamlit allows you to maintain state between app runs.
  • Custom Components: You can create your own custom Streamlit components using React and other web development technologies.
  • Sidebars: Streamlit lets you create sidebars for navigation and additional functionality.
  • Themes: You can customize the appearance of your Streamlit app with themes.

Example: Interactive Data Exploration App

import streamlit as st
import pandas as pd

# Load data
df = pd.read_csv("data.csv")

# Title
st.title("Data Exploration App")

# Show dataframe
st.dataframe(df)

# Data filtering
st.header("Data Filtering")
filter_column = st.selectbox("Filter by column", df.columns)
filter_value = st.text_input("Enter filter value")
filtered_df = df[df[filter_column] == filter_value]
st.dataframe(filtered_df)

# Data visualization
st.header("Data Visualization")
chart_type = st.selectbox("Select chart type", ("line", "bar", "scatter"))
if chart_type == "line":
    st.line_chart(df[["column1", "column2"]])
elif chart_type == "bar":
    st.bar_chart(df["column3"])
elif chart_type == "scatter":
    st.scatter_chart(df[["column1", "column2"]])

This example showcases basic features like input widgets, data display, and filtering. By utilizing Streamlit's capabilities, you can create interactive and user-friendly web applications that showcase your data and analysis effectively.

Key Resources:

Conclusion:

This cheat sheet provides a quick overview of key Streamlit components and functionalities. The library's ease of use and extensive features make it a powerful tool for developing data-driven web applications. Experiment with different components and explore advanced features to build compelling and interactive data visualization experiences.

Related Posts


Latest Posts