Streamlit Basics: App Structure and Layout
You'll learn how to structure a Streamlit app and organize its layout using columns, expanders, and containers.
What Is a Streamlit App?
A Streamlit app is simply a Python script that runs from top to bottom, re-executing every time a user interacts with a widget. Think of it like a conversation that restarts fresh each time you respond—Streamlit rebuilds the entire page based on your input.
Running Your First App
Create a file called app.py and run it from your terminal:
streamlit run app.py
Inside app.py, every st command adds an element to your page in order:
import streamlit as st
st.title("My Dashboard")
st.write("This text appears below the title")
Organizing Layout with Columns
Columns let you place content side-by-side, like newspaper columns:
col1, col2 = st.columns(2)
with col1:
st.header("Left Side")
st.write("Content here")
with col2:
st.header("Right Side")
st.write("More content")
Expanders for Collapsible Sections
Expanders hide content until clicked, perfect for optional details:
with st.expander("Click to see more"):
st.write("Hidden content that users can reveal")
Containers for Grouped Elements
Containers let you group related elements and even write to them out of order:
container = st.container()
st.write("This appears first")
with container:
st.write("But this was written to the container earlier in code")
Key Takeaway: Streamlit apps run top-to-bottom as Python scripts; use st.columns() for side-by-side layouts, st.expander() for collapsible sections, and st.container() to group and reorder elements strategically.