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📚",
|
7 |
+
layout="wide",
|
8 |
+
initial_sidebar_state="expanded"
|
9 |
+
)
|
10 |
+
|
11 |
+
st.title("📚 Streamlit Learning App")
|
12 |
+
st.markdown("Learn Streamlit by exploring this interactive app!")
|
13 |
+
|
14 |
+
# Sidebar navigation
|
15 |
+
st.sidebar.title("Navigation")
|
16 |
+
page = st.sidebar.radio(
|
17 |
+
"Go to",
|
18 |
+
["Home", "Basic Elements", "Layouts", "Widgets", "Advanced Features"]
|
19 |
+
)
|
20 |
+
|
21 |
+
if page == "Home":
|
22 |
+
show_home()
|
23 |
+
elif page == "Basic Elements":
|
24 |
+
show_basic_elements()
|
25 |
+
elif page == "Layouts":
|
26 |
+
show_layouts()
|
27 |
+
elif page == "Widgets":
|
28 |
+
show_widgets()
|
29 |
+
elif page == "Advanced Features":
|
30 |
+
show_advanced_features()
|
31 |
+
|
32 |
+
def show_home():
|
33 |
+
st.header("Welcome to Streamlit Learning!")
|
34 |
+
st.markdown("""
|
35 |
+
This app demonstrates various Streamlit features while teaching you how to use them.
|
36 |
+
|
37 |
+
**How to use this app:**
|
38 |
+
1. Select a topic from the sidebar
|
39 |
+
2. See live examples of Streamlit code
|
40 |
+
3. View the code that creates each example
|
41 |
+
4. Try modifying the code in the expanders
|
42 |
+
""")
|
43 |
+
|
44 |
+
st.image("https://streamlit.io/images/brand/streamlit-logo-secondary-colormark-darktext.png", width=300)
|
45 |
+
|
46 |
+
with st.expander("Quick Streamlit Facts"):
|
47 |
+
st.markdown("""
|
48 |
+
- Streamlit turns data scripts into shareable web apps in minutes
|
49 |
+
- No front-end experience required
|
50 |
+
- Pure Python
|
51 |
+
- Open source (Apache License 2.0)
|
52 |
+
- First released in October 2019
|
53 |
+
""")
|
54 |
+
|
55 |
+
def show_basic_elements():
|
56 |
+
st.header("Basic Streamlit Elements")
|
57 |
+
|
58 |
+
st.subheader("Text Elements")
|
59 |
+
st.code("""
|
60 |
+
st.title("This is a title") # Displays text in title formatting
|
61 |
+
st.header("This is a header") # Section header
|
62 |
+
st.subheader("This is a subheader") # Subsection header
|
63 |
+
st.text("This is plain text") # Fixed-width and monospace text
|
64 |
+
st.markdown("This is **markdown**") # Markdown formatting
|
65 |
+
st.write("This is multi-purpose write") # Can handle multiple arguments
|
66 |
+
""")
|
67 |
+
|
68 |
+
st.subheader("Display Elements")
|
69 |
+
st.code("""
|
70 |
+
st.dataframe(pandas_dataframe) # Interactive dataframe
|
71 |
+
st.table(static_data) # Static table
|
72 |
+
st.json({"key": "value"}) # Interactive JSON
|
73 |
+
st.metric("Metric", 42, delta=1.2) # Metric display
|
74 |
+
""")
|
75 |
+
|
76 |
+
col1, col2 = st.columns(2)
|
77 |
+
with col1:
|
78 |
+
st.info("This is an info message")
|
79 |
+
st.success("This is a success message!")
|
80 |
+
with col2:
|
81 |
+
st.warning("This is a warning message")
|
82 |
+
st.error("This is an error message")
|
83 |
+
|
84 |
+
def show_layouts():
|
85 |
+
st.header("Streamlit Layouts")
|
86 |
+
|
87 |
+
st.subheader("Columns")
|
88 |
+
st.code("""
|
89 |
+
col1, col2 = st.columns(2) # Create two columns
|
90 |
+
with col1:
|
91 |
+
st.write("Content in column 1")
|
92 |
+
with col2:
|
93 |
+
st.write("Content in column 2")
|
94 |
+
""")
|
95 |
+
|
96 |
+
# Example of columns
|
97 |
+
col1, col2, col3 = st.columns([1, 2, 1])
|
98 |
+
with col1:
|
99 |
+
st.write("Narrow column")
|
100 |
+
with col2:
|
101 |
+
st.write("Wide column")
|
102 |
+
with col3:
|
103 |
+
st.write("Another narrow column")
|
104 |
+
|
105 |
+
st.subheader("Expander")
|
106 |
+
st.code("""
|
107 |
+
with st.expander("Click to expand"):
|
108 |
+
st.write("Hidden content that appears when expanded")
|
109 |
+
""")
|
110 |
+
|
111 |
+
with st.expander("Example Expander"):
|
112 |
+
st.write("This content was hidden but now it's visible!")
|
113 |
+
st.image("https://via.placeholder.com/150", width=100)
|
114 |
+
|
115 |
+
st.subheader("Tabs")
|
116 |
+
st.code("""
|
117 |
+
tab1, tab2 = st.tabs(["Tab 1", "Tab 2"])
|
118 |
+
with tab1:
|
119 |
+
st.write("Content for tab 1")
|
120 |
+
with tab2:
|
121 |
+
st.write("Content for tab 2")
|
122 |
+
""")
|
123 |
+
|
124 |
+
tab1, tab2 = st.tabs(["First Tab", "Second Tab"])
|
125 |
+
with tab1:
|
126 |
+
st.write("This is the first tab")
|
127 |
+
with tab2:
|
128 |
+
st.write("This is the second tab")
|
129 |
+
|
130 |
+
def show_widgets():
|
131 |
+
st.header("Streamlit Widgets")
|
132 |
+
|
133 |
+
st.subheader("Input Widgets")
|
134 |
+
st.code("""
|
135 |
+
st.button("Click me") # Button
|
136 |
+
st.checkbox("I agree") # Checkbox
|
137 |
+
st.radio("Choose one", ["A", "B", "C"]) # Radio buttons
|
138 |
+
st.selectbox("Select", ["Option 1", "Option 2"]) # Dropdown select
|
139 |
+
st.multiselect("Choose many", ["A", "B", "C"]) # Multi-select
|
140 |
+
st.slider("Slide", 0, 100) # Slider
|
141 |
+
st.select_slider("Slide to select", ["Bad", "Good", "Excellent"]) # Select slider
|
142 |
+
st.text_input("Enter text") # Text input
|
143 |
+
st.number_input("Enter number") # Number input
|
144 |
+
st.text_area("Enter long text") # Text area
|
145 |
+
st.date_input("Pick a date") # Date input
|
146 |
+
st.time_input("Pick a time") # Time input
|
147 |
+
st.file_uploader("Upload a file") # File uploader
|
148 |
+
st.color_picker("Pick a color") # Color picker
|
149 |
+
""")
|
150 |
+
|
151 |
+
# Interactive widget demo
|
152 |
+
col1, col2 = st.columns(2)
|
153 |
+
with col1:
|
154 |
+
name = st.text_input("What's your name?")
|
155 |
+
age = st.slider("How old are you?", 0, 100, 25)
|
156 |
+
color = st.color_picker("Pick your favorite color")
|
157 |
+
with col2:
|
158 |
+
if name:
|
159 |
+
st.write(f"Hello, {name}!")
|
160 |
+
st.write(f"You're {age} years old")
|
161 |
+
st.write("Your favorite color is:")
|
162 |
+
st.markdown(f'<div style="width:100%; height:50px; background:{color};"></div>', unsafe_allow_html=True)
|
163 |
+
|
164 |
+
def show_advanced_features():
|
165 |
+
st.header("Advanced Streamlit Features")
|
166 |
+
|
167 |
+
st.subheader("Session State")
|
168 |
+
st.code("""
|
169 |
+
# Initialize session state
|
170 |
+
if 'counter' not in st.session_state:
|
171 |
+
st.session_state.counter = 0
|
172 |
+
|
173 |
+
# Increment counter
|
174 |
+
if st.button('Increment'):
|
175 |
+
st.session_state.counter += 1
|
176 |
+
|
177 |
+
st.write('Count:', st.session_state.counter)
|
178 |
+
""")
|
179 |
+
|
180 |
+
# Session state example
|
181 |
+
if 'counter' not in st.session_state:
|
182 |
+
st.session_state.counter = 0
|
183 |
+
|
184 |
+
col1, col2, col3 = st.columns(3)
|
185 |
+
with col1:
|
186 |
+
if st.button('Increment'):
|
187 |
+
st.session_state.counter += 1
|
188 |
+
with col2:
|
189 |
+
if st.button('Decrement'):
|
190 |
+
st.session_state.counter -= 1
|
191 |
+
with col3:
|
192 |
+
if st.button('Reset'):
|
193 |
+
st.session_state.counter = 0
|
194 |
+
|
195 |
+
st.write('Current count:', st.session_state.counter)
|
196 |
+
|
197 |
+
st.subheader("Caching")
|
198 |
+
st.code("""
|
199 |
+
@st.cache_data # Cache data processing functions
|
200 |
+
def expensive_computation(a, b):
|
201 |
+
time.sleep(2) # Simulate slow computation
|
202 |
+
return a * b
|
203 |
+
|
204 |
+
result = expensive_computation(5, 10)
|
205 |
+
st.write(result)
|
206 |
+
""")
|
207 |
+
|
208 |
+
import time
|
209 |
+
@st.cache_data
|
210 |
+
def expensive_computation(a, b):
|
211 |
+
time.sleep(2) # Simulate slow computation
|
212 |
+
return a * b
|
213 |
+
|
214 |
+
if st.button("Run expensive computation (cached)"):
|
215 |
+
result = expensive_computation(5, 10)
|
216 |
+
st.write("Result:", result)
|
217 |
+
|
218 |
+
st.info("Notice how the cached version runs instantly after the first run!")
|
219 |
+
|
220 |
+
if __name__ == "__main__":
|
221 |
+
main()
|