organizing-streamlit-code
Organizing Streamlit code for maintainability. Use when structuring apps with separate modules and utilities. Covers separation of concerns, keeping…
它会碰到什么
这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。
技能内容
Streamlit code organization
For most simple apps, keep everything in one file—it's cleaner and more straightforward. The app file should read like a normal Python script for data processing, with a few Streamlit commands sprinkled in.
Name the main file streamlit_app.py (Streamlit's default).
When to split
Keep in one file (most apps):
- Apps under ~1000 lines
- One-off scripts and prototypes
- Apps where logic is straightforward
Consider splitting when:
- Data processing is complex (50+ lines of non-UI code)
- Multiple pages share logic
- You want to test business logic separately
If splitting makes sense, here's how to organize it.
Directory structure
my-app/
├── streamlit_app.py # Main entry point
├── app_pages/ # Page UI modules
│ ├── dashboard.py
│ └── settings.py
└── utils/ # Business logic & helpers
├── data.py
└── api.py
Separating UI from logic
When you do split, keep Streamlit files focused on UI and move complex logic to utility modules:
# streamlit_app.py - UI-focused
import streamlit as st
from utils.data import load_sales_data, compute_metrics
st.title("Sales Dashboard")
start = st.date_input("Start")
end = st.date_input("End")
data = load_sales_data(start, end)
metrics = compute_metrics(data)
st.metric("Revenue", f"${metrics['revenue']:,.0f}")
st.dataframe(data)
Avoid if __name__ == "__main__"
Streamlit apps run the entire file on each interaction. Don't use the main guard in Streamlit files.
# BAD - don't do this in streamlit_app.py or pages
if __name__ == "__main__":
main()
# GOOD - just put the code directly
import streamlit as st
st.title("My App")
The main guard is fine in utility modules for quick testing:
# utils/data.py
def load_data(path):
...
# Optional: test this module directly with `python utils/data.py`
if __name__ == "__main__":
print(load_data("test.csv"))
References
想直接用这个技能?
本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。
它属于哪个仓库
.agents/skills/developing-with-streamlit/skills/organizing-streamlit-code/SKILL.md同一个仓库里的其他技能
- developing-with-streamlit
- building-streamlit-chat-ui
- building-streamlit-custom-components-v2
- building-streamlit-dashboards
- building-streamlit-multipage-apps
- choosing-streamlit-selection-widgets
- connecting-streamlit-to-snowflake
- creating-streamlit-themes
- displaying-streamlit-data
- improving-streamlit-design
- optimizing-streamlit-performance
- setting-up-streamlit-environment