01. Getting Started with Streamlit (Setup)
1. What is Streamlit?
Streamlit is an open-source app framework for data analysts. You can create data dashboards with pure Python code without complex web frontend knowledge (HTML, CSS, JS).
Features
- Python Only: You just need to know Python syntax.
- Interactive: Easily add buttons, sliders, input fields, etc.
- Fast: The web page refreshes instantly when you save your code.
2. Installation and Environment Setup
First, you need to install the Streamlit library.
❓ Problem 1: Install Streamlit
Q. Open the Terminal and use pip to install streamlit.
Terminal
pip install streamlitTo verify the installation was successful, run the following command:
streamlit hello3. Hello World!
Let’s create your first app.
❓ Problem 2: Run First App
Q. Create a file called app.py and display a large title saying “Hello, Analyst!” on the screen.
Step 1: Create Python File
Create an app.py file in your working directory.
Step 2: Write Code
Copy and paste the following code into app.py.
import streamlit as st
import pandas as pd
# Write title
st.title("My First Dashboard 📊")
# Write text
st.write("Hello, Analyst! Welcome to the world of data analysis.")Step 3: Run App
Run the following command in the terminal.
streamlit run app.pyA browser will automatically open and show you the results.
4. Displaying DataFrames
Displaying the DataFrame - the output of your analysis - beautifully is key.
❓ Problem 3: DataFrame Visualization
Q. Add code to app.py to read a CSV file or create a sample DataFrame and display it on the screen.
Python (Streamlit)
Hint: Using st.dataframe() creates a table with sorting and filtering capabilities.
# (Continue writing in app.py)
st.header("1. Data Overview")
# Create sample data
data = {
'Product': ['Shirt', 'Pants', 'Hat'],
'Price': [25, 40, 15],
'Stock': [100, 50, 200]
}
df = pd.DataFrame(data)
# Display DataFrame
st.dataframe(df)
# Tip: st.table() might look cleaner for small tables.
st.subheader("Static Table")
st.table(df)💡 Summary
pip install streamlit: Installation only takes once.streamlit run app.py: Command to run the app.st.title(),st.write(),st.dataframe(): The three most commonly used functions.
In the next chapter, we’ll use Sidebar and Columns to create a layout that looks like a real dashboard.