Spaces:
Sleeping
Sleeping
Milind Kamat
commited on
Commit
Β·
2679bb0
1
Parent(s):
84512f8
2024 dec 30: Documentaion added
Browse filesSigned-off-by: Milind Kamat <36366961+milindkamat0507@users.noreply.github.com>
app.py
CHANGED
|
@@ -1,29 +1,47 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
|
| 3 |
class StreamlitTutorial:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
def __init__(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
st.set_page_config(page_title="Learn Streamlit", layout="wide")
|
| 6 |
self.init_session_state()
|
| 7 |
self.setup_styles()
|
| 8 |
|
| 9 |
-
def init_session_state(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
if 'code_input' not in st.session_state:
|
| 11 |
st.session_state.code_input = ""
|
| 12 |
st.session_state.current_topic = "Basic Text Elements"
|
| 13 |
|
| 14 |
-
def setup_styles(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
st.markdown("""
|
| 16 |
<style>
|
| 17 |
-
.code-example {
|
| 18 |
-
|
| 19 |
-
}
|
| 20 |
-
.live-output {
|
| 21 |
border: 1px solid #ddd;
|
| 22 |
border-radius: 4px;
|
| 23 |
padding: 1rem;
|
| 24 |
margin: 0.5rem 0 1.5rem 0;
|
| 25 |
}
|
| 26 |
-
.section-title {
|
| 27 |
margin-bottom: 1rem;
|
| 28 |
padding: 0.5rem 0;
|
| 29 |
}
|
|
@@ -35,7 +53,12 @@ class StreamlitTutorial:
|
|
| 35 |
</style>
|
| 36 |
""", unsafe_allow_html=True)
|
| 37 |
|
| 38 |
-
def get_text_elements(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
return [
|
| 40 |
("Title", "st.title('Main Title')"),
|
| 41 |
("Header", "st.header('Header')"),
|
|
@@ -45,7 +68,12 @@ class StreamlitTutorial:
|
|
| 45 |
("Colored text", "st.markdown(':blue[Blue text]')")
|
| 46 |
]
|
| 47 |
|
| 48 |
-
def get_input_elements(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
return [
|
| 50 |
("Text Input", "name = st.text_input('Enter your name')"),
|
| 51 |
("Number Input", "num = st.number_input('Enter a number', min_value=0, max_value=100)"),
|
|
@@ -54,7 +82,14 @@ class StreamlitTutorial:
|
|
| 54 |
("Checkbox", "checked = st.checkbox('Enable feature')")
|
| 55 |
]
|
| 56 |
|
| 57 |
-
def render_text_elements(self, col):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
with col:
|
| 59 |
st.markdown("### π Code Examples")
|
| 60 |
for title, code in self.get_text_elements():
|
|
@@ -65,214 +100,10 @@ class StreamlitTutorial:
|
|
| 65 |
with st.container(border=True):
|
| 66 |
exec(code)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
st.markdown(f"**{title}**")
|
| 74 |
-
st.code(code)
|
| 75 |
-
st.markdown("Live output:")
|
| 76 |
-
with st.container(border=True):
|
| 77 |
-
exec(code)
|
| 78 |
-
|
| 79 |
-
def render_help_section(self, col):
|
| 80 |
-
with col:
|
| 81 |
-
st.markdown("### π Learning Guide")
|
| 82 |
-
|
| 83 |
-
with st.expander("π― Basic Concepts", expanded=True):
|
| 84 |
-
st.markdown("""
|
| 85 |
-
**What are Text Elements?**
|
| 86 |
-
- Basic building blocks for displaying text
|
| 87 |
-
- Range from titles to formatted text
|
| 88 |
-
- Support markdown formatting
|
| 89 |
-
""")
|
| 90 |
-
|
| 91 |
-
with st.expander("π‘ Best Practices"):
|
| 92 |
-
st.markdown("""
|
| 93 |
-
**Writing Tips:**
|
| 94 |
-
1. Use appropriate heading levels
|
| 95 |
-
2. Keep text concise and clear
|
| 96 |
-
3. Use formatting for emphasis
|
| 97 |
-
4. Add visual hierarchy with headers
|
| 98 |
-
|
| 99 |
-
**Code Structure:**
|
| 100 |
-
```python
|
| 101 |
-
st.title('Main Title')
|
| 102 |
-
st.header('Section Header')
|
| 103 |
-
st.subheader('Sub-section')
|
| 104 |
-
st.write('Regular text')
|
| 105 |
-
```
|
| 106 |
-
""")
|
| 107 |
-
|
| 108 |
-
with st.expander("π Examples & Use Cases"):
|
| 109 |
-
st.markdown("""
|
| 110 |
-
**Common Patterns:**
|
| 111 |
-
|
| 112 |
-
1. Page Structure
|
| 113 |
-
```python
|
| 114 |
-
st.title('Dashboard')
|
| 115 |
-
st.header('Sales Data')
|
| 116 |
-
st.subheader('Monthly Trends')
|
| 117 |
-
```
|
| 118 |
-
|
| 119 |
-
2. Formatted Text
|
| 120 |
-
```python
|
| 121 |
-
st.markdown('**Bold** and *italic*')
|
| 122 |
-
st.markdown(':blue[Colored text]')
|
| 123 |
-
```
|
| 124 |
-
|
| 125 |
-
3. Mixed Elements
|
| 126 |
-
```python
|
| 127 |
-
st.title('Report')
|
| 128 |
-
st.write('Regular text')
|
| 129 |
-
st.markdown('- Bullet point')
|
| 130 |
-
```
|
| 131 |
-
""")
|
| 132 |
-
|
| 133 |
-
with st.expander("β οΈ Common Mistakes"):
|
| 134 |
-
st.markdown("""
|
| 135 |
-
1. Skipping header levels
|
| 136 |
-
2. Overusing formatting
|
| 137 |
-
3. Inconsistent styling
|
| 138 |
-
4. Missing hierarchy
|
| 139 |
-
|
| 140 |
-
**How to Avoid:**
|
| 141 |
-
- Plan your page structure
|
| 142 |
-
- Use consistent formatting
|
| 143 |
-
- Test different screen sizes
|
| 144 |
-
- Keep it simple
|
| 145 |
-
""")
|
| 146 |
-
|
| 147 |
-
with st.expander("π Advanced Tips"):
|
| 148 |
-
st.markdown("""
|
| 149 |
-
**Advanced Formatting:**
|
| 150 |
-
```python
|
| 151 |
-
# Custom HTML
|
| 152 |
-
st.markdown('''
|
| 153 |
-
<span style='color:blue'>
|
| 154 |
-
Custom styled text
|
| 155 |
-
</span>
|
| 156 |
-
''', unsafe_allow_html=True)
|
| 157 |
-
|
| 158 |
-
# Complex Markdown
|
| 159 |
-
st.markdown('''
|
| 160 |
-
# Title
|
| 161 |
-
## Subtitle
|
| 162 |
-
* Point 1
|
| 163 |
-
* Subpoint
|
| 164 |
-
> Quote
|
| 165 |
-
''')
|
| 166 |
-
```
|
| 167 |
-
|
| 168 |
-
**Layout Combinations:**
|
| 169 |
-
```python
|
| 170 |
-
col1, col2 = st.columns(2)
|
| 171 |
-
with col1:
|
| 172 |
-
st.header('Column 1')
|
| 173 |
-
with col2:
|
| 174 |
-
st.header('Column 2')
|
| 175 |
-
```
|
| 176 |
-
""")
|
| 177 |
-
|
| 178 |
-
def render_playground(self, col, snippets):
|
| 179 |
-
with col:
|
| 180 |
-
st.markdown("### π» Practice Playground")
|
| 181 |
-
|
| 182 |
-
# Code input area
|
| 183 |
-
code_input = st.text_area(
|
| 184 |
-
"Try Streamlit commands:",
|
| 185 |
-
value=st.session_state.code_input,
|
| 186 |
-
height=200,
|
| 187 |
-
placeholder="Example:\nst.title('My Title')"
|
| 188 |
-
)
|
| 189 |
-
|
| 190 |
-
# Quick Snippets with better alignment
|
| 191 |
-
with st.container(border=True):
|
| 192 |
-
st.markdown("#### π Quick Snippets")
|
| 193 |
-
|
| 194 |
-
cols = st.columns([4, 1])
|
| 195 |
-
with cols[0]:
|
| 196 |
-
selected_snippet = st.selectbox(
|
| 197 |
-
"Choose snippet:",
|
| 198 |
-
list(snippets.keys()),
|
| 199 |
-
label_visibility="collapsed"
|
| 200 |
-
)
|
| 201 |
-
with cols[1]:
|
| 202 |
-
if st.button("Add", use_container_width=True):
|
| 203 |
-
new_code = snippets[selected_snippet]
|
| 204 |
-
if code_input:
|
| 205 |
-
code_input += f"\n{new_code}"
|
| 206 |
-
else:
|
| 207 |
-
code_input = new_code
|
| 208 |
-
st.session_state.code_input = code_input
|
| 209 |
-
|
| 210 |
-
# Control buttons
|
| 211 |
-
col1, col2, col3 = st.columns(3)
|
| 212 |
-
with col1:
|
| 213 |
-
if st.button("βΆοΈ Run", use_container_width=True):
|
| 214 |
-
st.session_state.code_input = code_input
|
| 215 |
-
with col2:
|
| 216 |
-
if st.button("π Reset", use_container_width=True):
|
| 217 |
-
st.session_state.code_input = ""
|
| 218 |
-
code_input = ""
|
| 219 |
-
with col3:
|
| 220 |
-
if st.button("πΎ Copy", use_container_width=True):
|
| 221 |
-
st.code(code_input)
|
| 222 |
-
|
| 223 |
-
# Live output
|
| 224 |
-
st.markdown("#### π¨ Live Output")
|
| 225 |
-
with st.container(border=True):
|
| 226 |
-
if code_input:
|
| 227 |
-
try:
|
| 228 |
-
exec(code_input)
|
| 229 |
-
except Exception as e:
|
| 230 |
-
st.error(f"Error: {str(e)}")
|
| 231 |
-
|
| 232 |
-
# Tips
|
| 233 |
-
with st.expander("π‘ Tips & Help"):
|
| 234 |
-
st.markdown(self.get_topic_tips(st.session_state.current_topic))
|
| 235 |
-
|
| 236 |
-
def get_topic_tips(self, topic):
|
| 237 |
-
tips = {
|
| 238 |
-
"Basic Text Elements": """
|
| 239 |
-
**Quick Tips:**
|
| 240 |
-
- Use markdown for formatting
|
| 241 |
-
- Try different header levels
|
| 242 |
-
- Combine text elements
|
| 243 |
-
- Use colored text with :color[text]
|
| 244 |
-
""",
|
| 245 |
-
"Input Widgets": """
|
| 246 |
-
**Quick Tips:**
|
| 247 |
-
- Use unique keys for widgets
|
| 248 |
-
- Store widget values in variables
|
| 249 |
-
- Add validation for inputs
|
| 250 |
-
- Combine widgets for complex inputs
|
| 251 |
-
"""
|
| 252 |
-
}
|
| 253 |
-
return tips.get(topic, "Tips coming soon!")
|
| 254 |
-
|
| 255 |
-
def run(self):
|
| 256 |
-
with st.sidebar:
|
| 257 |
-
st.title("Streamlit Tutorial")
|
| 258 |
-
st.session_state.current_topic = st.radio(
|
| 259 |
-
"Choose a Topic:",
|
| 260 |
-
["Basic Text Elements", "Input Widgets", "Layouts & Containers",
|
| 261 |
-
"Data Display", "Charts & Plots", "Interactive Components"]
|
| 262 |
-
)
|
| 263 |
-
|
| 264 |
-
if st.session_state.current_topic == "Basic Text Elements":
|
| 265 |
-
cols = st.columns([1.2, 1, 1])
|
| 266 |
-
self.render_text_elements(cols[0])
|
| 267 |
-
self.render_help_section(cols[1])
|
| 268 |
-
self.render_playground(cols[2], dict(self.get_text_elements()))
|
| 269 |
-
|
| 270 |
-
elif st.session_state.current_topic == "Input Widgets":
|
| 271 |
-
cols = st.columns([1.2, 1, 1])
|
| 272 |
-
self.render_input_elements(cols[0])
|
| 273 |
-
self.render_help_section(cols[1])
|
| 274 |
-
self.render_playground(cols[2], dict(self.get_input_elements()))
|
| 275 |
|
| 276 |
-
|
| 277 |
-
app = StreamlitTutorial()
|
| 278 |
-
app.run()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from typing import List, Dict, Tuple, Optional, Any
|
| 3 |
|
| 4 |
class StreamlitTutorial:
|
| 5 |
+
"""
|
| 6 |
+
Main class for Streamlit Tutorial application.
|
| 7 |
+
Handles all UI rendering and tutorial logic.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
def __init__(self):
|
| 11 |
+
"""
|
| 12 |
+
Initializes the Streamlit Tutorial application.
|
| 13 |
+
Sets up page config, session state, and styles.
|
| 14 |
+
"""
|
| 15 |
st.set_page_config(page_title="Learn Streamlit", layout="wide")
|
| 16 |
self.init_session_state()
|
| 17 |
self.setup_styles()
|
| 18 |
|
| 19 |
+
def init_session_state(self) -> None:
|
| 20 |
+
"""
|
| 21 |
+
Initializes session state variables for the application.
|
| 22 |
+
Handles code input persistence and current topic tracking.
|
| 23 |
+
Returns: None
|
| 24 |
+
"""
|
| 25 |
if 'code_input' not in st.session_state:
|
| 26 |
st.session_state.code_input = ""
|
| 27 |
st.session_state.current_topic = "Basic Text Elements"
|
| 28 |
|
| 29 |
+
def setup_styles(self) -> None:
|
| 30 |
+
"""
|
| 31 |
+
Sets up custom CSS styles for the application.
|
| 32 |
+
Handles margins, padding, and layout styling.
|
| 33 |
+
Returns: None
|
| 34 |
+
"""
|
| 35 |
st.markdown("""
|
| 36 |
<style>
|
| 37 |
+
.code-example { margin-bottom: 1.5rem; }
|
| 38 |
+
.live-output {
|
|
|
|
|
|
|
| 39 |
border: 1px solid #ddd;
|
| 40 |
border-radius: 4px;
|
| 41 |
padding: 1rem;
|
| 42 |
margin: 0.5rem 0 1.5rem 0;
|
| 43 |
}
|
| 44 |
+
.section-title {
|
| 45 |
margin-bottom: 1rem;
|
| 46 |
padding: 0.5rem 0;
|
| 47 |
}
|
|
|
|
| 53 |
</style>
|
| 54 |
""", unsafe_allow_html=True)
|
| 55 |
|
| 56 |
+
def get_text_elements(self) -> List[Tuple[str, str]]:
|
| 57 |
+
"""
|
| 58 |
+
Provides list of text element examples with their code.
|
| 59 |
+
Used for generating code examples and snippets.
|
| 60 |
+
Returns: List of tuples containing (title, code snippet)
|
| 61 |
+
"""
|
| 62 |
return [
|
| 63 |
("Title", "st.title('Main Title')"),
|
| 64 |
("Header", "st.header('Header')"),
|
|
|
|
| 68 |
("Colored text", "st.markdown(':blue[Blue text]')")
|
| 69 |
]
|
| 70 |
|
| 71 |
+
def get_input_elements(self) -> List[Tuple[str, str]]:
|
| 72 |
+
"""
|
| 73 |
+
Provides list of input widget examples with their code.
|
| 74 |
+
Used for demonstrating various input widget types.
|
| 75 |
+
Returns: List of tuples containing (widget name, code snippet)
|
| 76 |
+
"""
|
| 77 |
return [
|
| 78 |
("Text Input", "name = st.text_input('Enter your name')"),
|
| 79 |
("Number Input", "num = st.number_input('Enter a number', min_value=0, max_value=100)"),
|
|
|
|
| 82 |
("Checkbox", "checked = st.checkbox('Enable feature')")
|
| 83 |
]
|
| 84 |
|
| 85 |
+
def render_text_elements(self, col: st.delta_generator.DeltaGenerator) -> None:
|
| 86 |
+
"""
|
| 87 |
+
Renders text element examples in the specified column.
|
| 88 |
+
Shows code and live output for each text element.
|
| 89 |
+
Arguments:
|
| 90 |
+
col: Streamlit column object for rendering
|
| 91 |
+
Returns: None
|
| 92 |
+
"""
|
| 93 |
with col:
|
| 94 |
st.markdown("### π Code Examples")
|
| 95 |
for title, code in self.get_text_elements():
|
|
|
|
| 100 |
with st.container(border=True):
|
| 101 |
exec(code)
|
| 102 |
|
| 103 |
+
Would you like me to continue with the documentation for the remaining methods? This pattern shows:
|
| 104 |
+
1. Method purpose and description
|
| 105 |
+
2. Arguments and their types
|
| 106 |
+
3. Return value hints
|
| 107 |
+
4. Clear explanation of functionality
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
class StreamlitTutorial:
|
| 4 |
+
def __init__(self):
|
| 5 |
+
st.set_page_config(page_title="Learn Streamlit", layout="wide")
|
| 6 |
+
self.init_session_state()
|
| 7 |
+
self.setup_styles()
|
| 8 |
+
|
| 9 |
+
def init_session_state(self):
|
| 10 |
+
if 'code_input' not in st.session_state:
|
| 11 |
+
st.session_state.code_input = ""
|
| 12 |
+
st.session_state.current_topic = "Basic Text Elements"
|
| 13 |
+
|
| 14 |
+
def setup_styles(self):
|
| 15 |
+
st.markdown("""
|
| 16 |
+
<style>
|
| 17 |
+
.code-example {
|
| 18 |
+
margin-bottom: 1.5rem;
|
| 19 |
+
}
|
| 20 |
+
.live-output {
|
| 21 |
+
border: 1px solid #ddd;
|
| 22 |
+
border-radius: 4px;
|
| 23 |
+
padding: 1rem;
|
| 24 |
+
margin: 0.5rem 0 1.5rem 0;
|
| 25 |
+
}
|
| 26 |
+
.section-title {
|
| 27 |
+
margin-bottom: 1rem;
|
| 28 |
+
padding: 0.5rem 0;
|
| 29 |
+
}
|
| 30 |
+
.button-container {
|
| 31 |
+
display: flex;
|
| 32 |
+
justify-content: space-between;
|
| 33 |
+
gap: 1rem;
|
| 34 |
+
}
|
| 35 |
+
</style>
|
| 36 |
+
""", unsafe_allow_html=True)
|
| 37 |
+
|
| 38 |
+
def get_text_elements(self):
|
| 39 |
+
return [
|
| 40 |
+
("Title", "st.title('Main Title')"),
|
| 41 |
+
("Header", "st.header('Header')"),
|
| 42 |
+
("Subheader", "st.subheader('Subheader')"),
|
| 43 |
+
("Normal text", "st.write('Normal text')"),
|
| 44 |
+
("Markdown text", "st.markdown('**Bold** and *italic*')"),
|
| 45 |
+
("Colored text", "st.markdown(':blue[Blue text]')")
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
def get_input_elements(self):
|
| 49 |
+
return [
|
| 50 |
+
("Text Input", "name = st.text_input('Enter your name')"),
|
| 51 |
+
("Number Input", "num = st.number_input('Enter a number', min_value=0, max_value=100)"),
|
| 52 |
+
("Slider", "value = st.slider('Select value', 0, 100, 50)"),
|
| 53 |
+
("Selectbox", "option = st.selectbox('Choose option', ['A', 'B', 'C'])"),
|
| 54 |
+
("Checkbox", "checked = st.checkbox('Enable feature')")
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
+
def render_text_elements(self, col):
|
| 58 |
+
with col:
|
| 59 |
+
st.markdown("### π Code Examples")
|
| 60 |
+
for title, code in self.get_text_elements():
|
| 61 |
+
with st.container(border=True):
|
| 62 |
+
st.markdown(f"**{title}**")
|
| 63 |
+
st.code(code)
|
| 64 |
+
st.markdown("Live output:")
|
| 65 |
+
with st.container(border=True):
|
| 66 |
+
exec(code)
|
| 67 |
+
|
| 68 |
+
def render_input_elements(self, col):
|
| 69 |
+
with col:
|
| 70 |
+
st.markdown("### π Code Examples")
|
| 71 |
+
for title, code in self.get_input_elements():
|
| 72 |
+
with st.container(border=True):
|
| 73 |
+
st.markdown(f"**{title}**")
|
| 74 |
+
st.code(code)
|
| 75 |
+
st.markdown("Live output:")
|
| 76 |
+
with st.container(border=True):
|
| 77 |
+
exec(code)
|
| 78 |
+
|
| 79 |
+
def render_help_section(self, col):
|
| 80 |
+
with col:
|
| 81 |
+
st.markdown("### π Learning Guide")
|
| 82 |
+
|
| 83 |
+
with st.expander("π― Basic Concepts", expanded=True):
|
| 84 |
+
st.markdown("""
|
| 85 |
+
**What are Text Elements?**
|
| 86 |
+
- Basic building blocks for displaying text
|
| 87 |
+
- Range from titles to formatted text
|
| 88 |
+
- Support markdown formatting
|
| 89 |
+
""")
|
| 90 |
+
|
| 91 |
+
with st.expander("π‘ Best Practices"):
|
| 92 |
+
st.markdown("""
|
| 93 |
+
**Writing Tips:**
|
| 94 |
+
1. Use appropriate heading levels
|
| 95 |
+
2. Keep text concise and clear
|
| 96 |
+
3. Use formatting for emphasis
|
| 97 |
+
4. Add visual hierarchy with headers
|
| 98 |
+
|
| 99 |
+
**Code Structure:**
|
| 100 |
+
```python
|
| 101 |
+
st.title('Main Title')
|
| 102 |
+
st.header('Section Header')
|
| 103 |
+
st.subheader('Sub-section')
|
| 104 |
+
st.write('Regular text')
|
| 105 |
+
```
|
| 106 |
+
""")
|
| 107 |
+
|
| 108 |
+
with st.expander("π Examples & Use Cases"):
|
| 109 |
+
st.markdown("""
|
| 110 |
+
**Common Patterns:**
|
| 111 |
+
|
| 112 |
+
1. Page Structure
|
| 113 |
+
```python
|
| 114 |
+
st.title('Dashboard')
|
| 115 |
+
st.header('Sales Data')
|
| 116 |
+
st.subheader('Monthly Trends')
|
| 117 |
+
```
|
| 118 |
+
|
| 119 |
+
2. Formatted Text
|
| 120 |
+
```python
|
| 121 |
+
st.markdown('**Bold** and *italic*')
|
| 122 |
+
st.markdown(':blue[Colored text]')
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
+
3. Mixed Elements
|
| 126 |
+
```python
|
| 127 |
+
st.title('Report')
|
| 128 |
+
st.write('Regular text')
|
| 129 |
+
st.markdown('- Bullet point')
|
| 130 |
+
```
|
| 131 |
+
""")
|
| 132 |
+
|
| 133 |
+
with st.expander("β οΈ Common Mistakes"):
|
| 134 |
+
st.markdown("""
|
| 135 |
+
1. Skipping header levels
|
| 136 |
+
2. Overusing formatting
|
| 137 |
+
3. Inconsistent styling
|
| 138 |
+
4. Missing hierarchy
|
| 139 |
+
|
| 140 |
+
**How to Avoid:**
|
| 141 |
+
- Plan your page structure
|
| 142 |
+
- Use consistent formatting
|
| 143 |
+
- Test different screen sizes
|
| 144 |
+
- Keep it simple
|
| 145 |
+
""")
|
| 146 |
+
|
| 147 |
+
with st.expander("π Advanced Tips"):
|
| 148 |
+
st.markdown("""
|
| 149 |
+
**Advanced Formatting:**
|
| 150 |
+
```python
|
| 151 |
+
# Custom HTML
|
| 152 |
+
st.markdown('''
|
| 153 |
+
<span style='color:blue'>
|
| 154 |
+
Custom styled text
|
| 155 |
+
</span>
|
| 156 |
+
''', unsafe_allow_html=True)
|
| 157 |
+
|
| 158 |
+
# Complex Markdown
|
| 159 |
+
st.markdown('''
|
| 160 |
+
# Title
|
| 161 |
+
## Subtitle
|
| 162 |
+
* Point 1
|
| 163 |
+
* Subpoint
|
| 164 |
+
> Quote
|
| 165 |
+
''')
|
| 166 |
+
```
|
| 167 |
+
|
| 168 |
+
**Layout Combinations:**
|
| 169 |
+
```python
|
| 170 |
+
col1, col2 = st.columns(2)
|
| 171 |
+
with col1:
|
| 172 |
+
st.header('Column 1')
|
| 173 |
+
with col2:
|
| 174 |
+
st.header('Column 2')
|
| 175 |
+
```
|
| 176 |
+
""")
|
| 177 |
+
|
| 178 |
+
def render_playground(self, col, snippets):
|
| 179 |
+
with col:
|
| 180 |
+
st.markdown("### π» Practice Playground")
|
| 181 |
+
|
| 182 |
+
# Code input area
|
| 183 |
+
code_input = st.text_area(
|
| 184 |
+
"Try Streamlit commands:",
|
| 185 |
+
value=st.session_state.code_input,
|
| 186 |
+
height=200,
|
| 187 |
+
placeholder="Example:\nst.title('My Title')"
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
# Quick Snippets with better alignment
|
| 191 |
+
with st.container(border=True):
|
| 192 |
+
st.markdown("#### π Quick Snippets")
|
| 193 |
+
|
| 194 |
+
cols = st.columns([4, 1])
|
| 195 |
+
with cols[0]:
|
| 196 |
+
selected_snippet = st.selectbox(
|
| 197 |
+
"Choose snippet:",
|
| 198 |
+
list(snippets.keys()),
|
| 199 |
+
label_visibility="collapsed"
|
| 200 |
+
)
|
| 201 |
+
with cols[1]:
|
| 202 |
+
if st.button("Add", use_container_width=True):
|
| 203 |
+
new_code = snippets[selected_snippet]
|
| 204 |
+
if code_input:
|
| 205 |
+
code_input += f"\n{new_code}"
|
| 206 |
+
else:
|
| 207 |
+
code_input = new_code
|
| 208 |
+
st.session_state.code_input = code_input
|
| 209 |
+
|
| 210 |
+
# Control buttons
|
| 211 |
+
col1, col2, col3 = st.columns(3)
|
| 212 |
+
with col1:
|
| 213 |
+
if st.button("βΆοΈ Run", use_container_width=True):
|
| 214 |
+
st.session_state.code_input = code_input
|
| 215 |
+
with col2:
|
| 216 |
+
if st.button("π Reset", use_container_width=True):
|
| 217 |
+
st.session_state.code_input = ""
|
| 218 |
+
code_input = ""
|
| 219 |
+
with col3:
|
| 220 |
+
if st.button("πΎ Copy", use_container_width=True):
|
| 221 |
+
st.code(code_input)
|
| 222 |
+
|
| 223 |
+
# Live output
|
| 224 |
+
st.markdown("#### π¨ Live Output")
|
| 225 |
+
with st.container(border=True):
|
| 226 |
+
if code_input:
|
| 227 |
+
try:
|
| 228 |
+
exec(code_input)
|
| 229 |
+
except Exception as e:
|
| 230 |
+
st.error(f"Error: {str(e)}")
|
| 231 |
+
|
| 232 |
+
# Tips
|
| 233 |
+
with st.expander("π‘ Tips & Help"):
|
| 234 |
+
st.markdown(self.get_topic_tips(st.session_state.current_topic))
|
| 235 |
+
|
| 236 |
+
def get_topic_tips(self, topic):
|
| 237 |
+
tips = {
|
| 238 |
+
"Basic Text Elements": """
|
| 239 |
+
**Quick Tips:**
|
| 240 |
+
- Use markdown for formatting
|
| 241 |
+
- Try different header levels
|
| 242 |
+
- Combine text elements
|
| 243 |
+
- Use colored text with :color[text]
|
| 244 |
+
""",
|
| 245 |
+
"Input Widgets": """
|
| 246 |
+
**Quick Tips:**
|
| 247 |
+
- Use unique keys for widgets
|
| 248 |
+
- Store widget values in variables
|
| 249 |
+
- Add validation for inputs
|
| 250 |
+
- Combine widgets for complex inputs
|
| 251 |
+
"""
|
| 252 |
+
}
|
| 253 |
+
return tips.get(topic, "Tips coming soon!")
|
| 254 |
+
|
| 255 |
+
def run(self):
|
| 256 |
+
with st.sidebar:
|
| 257 |
+
st.title("Streamlit Tutorial")
|
| 258 |
+
st.session_state.current_topic = st.radio(
|
| 259 |
+
"Choose a Topic:",
|
| 260 |
+
["Basic Text Elements", "Input Widgets", "Layouts & Containers",
|
| 261 |
+
"Data Display", "Charts & Plots", "Interactive Components"]
|
| 262 |
+
)
|
| 263 |
+
|
| 264 |
+
if st.session_state.current_topic == "Basic Text Elements":
|
| 265 |
+
cols = st.columns([1.2, 1, 1])
|
| 266 |
+
self.render_text_elements(cols[0])
|
| 267 |
+
self.render_help_section(cols[1])
|
| 268 |
+
self.render_playground(cols[2], dict(self.get_text_elements()))
|
| 269 |
+
|
| 270 |
+
elif st.session_state.current_topic == "Input Widgets":
|
| 271 |
+
cols = st.columns([1.2, 1, 1])
|
| 272 |
+
self.render_input_elements(cols[0])
|
| 273 |
+
self.render_help_section(cols[1])
|
| 274 |
+
self.render_playground(cols[2], dict(self.get_input_elements()))
|
| 275 |
+
|
| 276 |
+
if __name__ == "__main__":
|
| 277 |
+
app = StreamlitTutorial()
|
| 278 |
+
app.run()
|