Spaces:
Sleeping
Sleeping
Milind Kamat
commited on
Commit
Β·
8ba5c6a
1
Parent(s):
2679bb0
2024 Dec 30 Ver.0 created for improvenment
Browse filesSigned-off-by: Milind Kamat <36366961+milindkamat0507@users.noreply.github.com>
app.py
CHANGED
|
@@ -3,14 +3,16 @@ from typing import List, Dict, Tuple, Optional, Any
|
|
| 3 |
|
| 4 |
class StreamlitTutorial:
|
| 5 |
"""
|
| 6 |
-
Main class for Streamlit Tutorial application.
|
| 7 |
-
Handles all
|
|
|
|
| 8 |
"""
|
| 9 |
|
| 10 |
def __init__(self):
|
| 11 |
"""
|
| 12 |
Initializes the Streamlit Tutorial application.
|
| 13 |
-
Sets up page
|
|
|
|
| 14 |
"""
|
| 15 |
st.set_page_config(page_title="Learn Streamlit", layout="wide")
|
| 16 |
self.init_session_state()
|
|
@@ -18,9 +20,12 @@ class StreamlitTutorial:
|
|
| 18 |
|
| 19 |
def init_session_state(self) -> None:
|
| 20 |
"""
|
| 21 |
-
Initializes session state variables for
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
"""
|
| 25 |
if 'code_input' not in st.session_state:
|
| 26 |
st.session_state.code_input = ""
|
|
@@ -28,9 +33,12 @@ class StreamlitTutorial:
|
|
| 28 |
|
| 29 |
def setup_styles(self) -> None:
|
| 30 |
"""
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
"""
|
| 35 |
st.markdown("""
|
| 36 |
<style>
|
|
@@ -55,9 +63,12 @@ class StreamlitTutorial:
|
|
| 55 |
|
| 56 |
def get_text_elements(self) -> List[Tuple[str, str]]:
|
| 57 |
"""
|
| 58 |
-
Provides
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
| 61 |
"""
|
| 62 |
return [
|
| 63 |
("Title", "st.title('Main Title')"),
|
|
@@ -70,9 +81,12 @@ class StreamlitTutorial:
|
|
| 70 |
|
| 71 |
def get_input_elements(self) -> List[Tuple[str, str]]:
|
| 72 |
"""
|
| 73 |
-
Provides
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
| 76 |
"""
|
| 77 |
return [
|
| 78 |
("Text Input", "name = st.text_input('Enter your name')"),
|
|
@@ -85,10 +99,14 @@ class StreamlitTutorial:
|
|
| 85 |
def render_text_elements(self, col: st.delta_generator.DeltaGenerator) -> None:
|
| 86 |
"""
|
| 87 |
Renders text element examples in the specified column.
|
| 88 |
-
|
|
|
|
|
|
|
| 89 |
Arguments:
|
| 90 |
-
col: Streamlit column object for rendering
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
"""
|
| 93 |
with col:
|
| 94 |
st.markdown("### π Code Examples")
|
|
@@ -100,10 +118,192 @@ class StreamlitTutorial:
|
|
| 100 |
with st.container(border=True):
|
| 101 |
exec(code)
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
class StreamlitTutorial:
|
| 5 |
"""
|
| 6 |
+
Main class for the Streamlit Tutorial application.
|
| 7 |
+
Handles rendering of all tutorial content, user interaction, and state management.
|
| 8 |
+
Provides modular components for different tutorial topics and interactive examples.
|
| 9 |
"""
|
| 10 |
|
| 11 |
def __init__(self):
|
| 12 |
"""
|
| 13 |
Initializes the Streamlit Tutorial application.
|
| 14 |
+
Sets up page configuration, session state, and custom styling.
|
| 15 |
+
Prepares the application environment for tutorial content.
|
| 16 |
"""
|
| 17 |
st.set_page_config(page_title="Learn Streamlit", layout="wide")
|
| 18 |
self.init_session_state()
|
|
|
|
| 20 |
|
| 21 |
def init_session_state(self) -> None:
|
| 22 |
"""
|
| 23 |
+
Initializes session state variables for persistent data storage.
|
| 24 |
+
Creates state variables for code input and current topic tracking.
|
| 25 |
+
Ensures consistent state across reruns.
|
| 26 |
+
|
| 27 |
+
Returns:
|
| 28 |
+
None
|
| 29 |
"""
|
| 30 |
if 'code_input' not in st.session_state:
|
| 31 |
st.session_state.code_input = ""
|
|
|
|
| 33 |
|
| 34 |
def setup_styles(self) -> None:
|
| 35 |
"""
|
| 36 |
+
Configures custom CSS styles for the application.
|
| 37 |
+
Sets up consistent styling for code examples, outputs, and layout.
|
| 38 |
+
Enhances visual presentation of tutorial content.
|
| 39 |
+
|
| 40 |
+
Returns:
|
| 41 |
+
None
|
| 42 |
"""
|
| 43 |
st.markdown("""
|
| 44 |
<style>
|
|
|
|
| 63 |
|
| 64 |
def get_text_elements(self) -> List[Tuple[str, str]]:
|
| 65 |
"""
|
| 66 |
+
Provides collection of text element examples with corresponding code.
|
| 67 |
+
Defines basic text manipulation and display examples.
|
| 68 |
+
Used for generating code examples and quick snippets.
|
| 69 |
+
|
| 70 |
+
Returns:
|
| 71 |
+
List[Tuple[str, str]]: List of (element name, code snippet) pairs
|
| 72 |
"""
|
| 73 |
return [
|
| 74 |
("Title", "st.title('Main Title')"),
|
|
|
|
| 81 |
|
| 82 |
def get_input_elements(self) -> List[Tuple[str, str]]:
|
| 83 |
"""
|
| 84 |
+
Provides collection of input widget examples with corresponding code.
|
| 85 |
+
Defines various input types and interactive elements.
|
| 86 |
+
Used for demonstrating user input capabilities.
|
| 87 |
+
|
| 88 |
+
Returns:
|
| 89 |
+
List[Tuple[str, str]]: List of (widget name, code snippet) pairs
|
| 90 |
"""
|
| 91 |
return [
|
| 92 |
("Text Input", "name = st.text_input('Enter your name')"),
|
|
|
|
| 99 |
def render_text_elements(self, col: st.delta_generator.DeltaGenerator) -> None:
|
| 100 |
"""
|
| 101 |
Renders text element examples in the specified column.
|
| 102 |
+
Displays code snippets with live output for each text element.
|
| 103 |
+
Creates interactive learning environment for text elements.
|
| 104 |
+
|
| 105 |
Arguments:
|
| 106 |
+
col: Streamlit column object for content rendering
|
| 107 |
+
|
| 108 |
+
Returns:
|
| 109 |
+
None
|
| 110 |
"""
|
| 111 |
with col:
|
| 112 |
st.markdown("### π Code Examples")
|
|
|
|
| 118 |
with st.container(border=True):
|
| 119 |
exec(code)
|
| 120 |
|
| 121 |
+
def render_input_elements(self, col: st.delta_generator.DeltaGenerator) -> None:
|
| 122 |
+
"""
|
| 123 |
+
Renders input widget examples in the specified column.
|
| 124 |
+
Displays code snippets with live output for each input widget.
|
| 125 |
+
Creates interactive learning environment for input elements.
|
| 126 |
+
|
| 127 |
+
Arguments:
|
| 128 |
+
col: Streamlit column object for content rendering
|
| 129 |
+
|
| 130 |
+
Returns:
|
| 131 |
+
None
|
| 132 |
+
"""
|
| 133 |
+
with col:
|
| 134 |
+
st.markdown("### π Code Examples")
|
| 135 |
+
for title, code in self.get_input_elements():
|
| 136 |
+
with st.container(border=True):
|
| 137 |
+
st.markdown(f"**{title}**")
|
| 138 |
+
st.code(code)
|
| 139 |
+
st.markdown("Live output:")
|
| 140 |
+
with st.container(border=True):
|
| 141 |
+
exec(code)
|
| 142 |
+
|
| 143 |
+
def render_help_section(self, col: st.delta_generator.DeltaGenerator) -> None:
|
| 144 |
+
"""
|
| 145 |
+
Renders comprehensive help content in the specified column.
|
| 146 |
+
Provides educational content, best practices, and examples.
|
| 147 |
+
Creates expandable sections for different learning topics.
|
| 148 |
+
|
| 149 |
+
Arguments:
|
| 150 |
+
col: Streamlit column object for content rendering
|
| 151 |
+
|
| 152 |
+
Returns:
|
| 153 |
+
None
|
| 154 |
+
"""
|
| 155 |
+
with col:
|
| 156 |
+
st.markdown("### π Learning Guide")
|
| 157 |
+
|
| 158 |
+
# Basic Concepts Section
|
| 159 |
+
with st.expander("π― Basic Concepts", expanded=True):
|
| 160 |
+
st.markdown(self._get_basic_concepts_content())
|
| 161 |
+
|
| 162 |
+
# Best Practices Section
|
| 163 |
+
with st.expander("π‘ Best Practices"):
|
| 164 |
+
st.markdown(self._get_best_practices_content())
|
| 165 |
+
|
| 166 |
+
# Examples Section
|
| 167 |
+
with st.expander("π Examples & Use Cases"):
|
| 168 |
+
st.markdown(self._get_examples_content())
|
| 169 |
+
|
| 170 |
+
# Common Mistakes Section
|
| 171 |
+
with st.expander("β οΈ Common Mistakes"):
|
| 172 |
+
st.markdown(self._get_common_mistakes_content())
|
| 173 |
+
|
| 174 |
+
# Advanced Tips Section
|
| 175 |
+
with st.expander("π Advanced Tips"):
|
| 176 |
+
st.markdown(self._get_advanced_tips_content())
|
| 177 |
+
|
| 178 |
+
def render_playground(self, col: st.delta_generator.DeltaGenerator,
|
| 179 |
+
snippets: Dict[str, str]) -> None:
|
| 180 |
+
"""
|
| 181 |
+
Renders interactive coding playground in specified column.
|
| 182 |
+
Provides code editor, snippets, and live output area.
|
| 183 |
+
Creates environment for hands-on practice.
|
| 184 |
+
|
| 185 |
+
Arguments:
|
| 186 |
+
col: Streamlit column object for content rendering
|
| 187 |
+
snippets: Dictionary of code snippets for quick insertion
|
| 188 |
+
|
| 189 |
+
Returns:
|
| 190 |
+
None
|
| 191 |
+
"""
|
| 192 |
+
with col:
|
| 193 |
+
st.markdown("### π» Practice Playground")
|
| 194 |
+
|
| 195 |
+
# Code Input Area
|
| 196 |
+
code_input = st.text_area(
|
| 197 |
+
"Try Streamlit commands:",
|
| 198 |
+
value=st.session_state.code_input,
|
| 199 |
+
height=200,
|
| 200 |
+
placeholder="Example:\nst.title('My Title')"
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
# Quick Snippets Section
|
| 204 |
+
with st.container(border=True):
|
| 205 |
+
st.markdown("#### π Quick Snippets")
|
| 206 |
+
cols = st.columns([4, 1])
|
| 207 |
+
with cols[0]:
|
| 208 |
+
selected_snippet = st.selectbox(
|
| 209 |
+
"Choose snippet:",
|
| 210 |
+
list(snippets.keys()),
|
| 211 |
+
label_visibility="collapsed"
|
| 212 |
+
)
|
| 213 |
+
with cols[1]:
|
| 214 |
+
if st.button("Add", use_container_width=True):
|
| 215 |
+
new_code = snippets[selected_snippet]
|
| 216 |
+
if code_input:
|
| 217 |
+
code_input += f"\n{new_code}"
|
| 218 |
+
else:
|
| 219 |
+
code_input = new_code
|
| 220 |
+
st.session_state.code_input = code_input
|
| 221 |
+
|
| 222 |
+
# Control Buttons
|
| 223 |
+
col1, col2, col3 = st.columns(3)
|
| 224 |
+
with col1:
|
| 225 |
+
if st.button("βΆοΈ Run", use_container_width=True):
|
| 226 |
+
st.session_state.code_input = code_input
|
| 227 |
+
with col2:
|
| 228 |
+
if st.button("π Reset", use_container_width=True):
|
| 229 |
+
st.session_state.code_input = ""
|
| 230 |
+
code_input = ""
|
| 231 |
+
with col3:
|
| 232 |
+
if st.button("πΎ Copy", use_container_width=True):
|
| 233 |
+
st.code(code_input)
|
| 234 |
+
|
| 235 |
+
# Live Output Section
|
| 236 |
+
st.markdown("#### π¨ Live Output")
|
| 237 |
+
with st.container(border=True):
|
| 238 |
+
if code_input:
|
| 239 |
+
try:
|
| 240 |
+
exec(code_input)
|
| 241 |
+
except Exception as e:
|
| 242 |
+
st.error(f"Error: {str(e)}")
|
| 243 |
+
|
| 244 |
+
# Tips Section
|
| 245 |
+
with st.expander("π‘ Tips & Help"):
|
| 246 |
+
st.markdown(self.get_topic_tips(st.session_state.current_topic))
|
| 247 |
+
|
| 248 |
+
def get_topic_tips(self, topic: str) -> str:
|
| 249 |
+
"""
|
| 250 |
+
Provides topic-specific tips and guidance.
|
| 251 |
+
Returns formatted markdown string with helpful information.
|
| 252 |
+
Customizes content based on current tutorial topic.
|
| 253 |
+
|
| 254 |
+
Arguments:
|
| 255 |
+
topic: Current tutorial topic name
|
| 256 |
+
|
| 257 |
+
Returns:
|
| 258 |
+
str: Markdown formatted tips and help content
|
| 259 |
+
"""
|
| 260 |
+
tips = {
|
| 261 |
+
"Basic Text Elements": """
|
| 262 |
+
**Quick Tips:**
|
| 263 |
+
- Use markdown for formatting
|
| 264 |
+
- Try different header levels
|
| 265 |
+
- Combine text elements
|
| 266 |
+
- Use colored text with :color[text]
|
| 267 |
+
""",
|
| 268 |
+
"Input Widgets": """
|
| 269 |
+
**Quick Tips:**
|
| 270 |
+
- Use unique keys for widgets
|
| 271 |
+
- Store widget values in variables
|
| 272 |
+
- Add validation for inputs
|
| 273 |
+
- Combine widgets for complex inputs
|
| 274 |
+
"""
|
| 275 |
+
}
|
| 276 |
+
return tips.get(topic, "Tips coming soon!")
|
| 277 |
+
|
| 278 |
+
def run(self) -> None:
|
| 279 |
+
"""
|
| 280 |
+
Main execution method for the Streamlit Tutorial application.
|
| 281 |
+
Handles topic selection and content rendering.
|
| 282 |
+
Manages overall application flow and state.
|
| 283 |
+
|
| 284 |
+
Returns:
|
| 285 |
+
None
|
| 286 |
+
"""
|
| 287 |
+
with st.sidebar:
|
| 288 |
+
st.title("Streamlit Tutorial")
|
| 289 |
+
st.session_state.current_topic = st.radio(
|
| 290 |
+
"Choose a Topic:",
|
| 291 |
+
["Basic Text Elements", "Input Widgets", "Layouts & Containers",
|
| 292 |
+
"Data Display", "Charts & Plots", "Interactive Components"]
|
| 293 |
+
)
|
| 294 |
+
|
| 295 |
+
if st.session_state.current_topic == "Basic Text Elements":
|
| 296 |
+
cols = st.columns([1.2, 1, 1])
|
| 297 |
+
self.render_text_elements(cols[0])
|
| 298 |
+
self.render_help_section(cols[1])
|
| 299 |
+
self.render_playground(cols[2], dict(self.get_text_elements()))
|
| 300 |
+
|
| 301 |
+
elif st.session_state.current_topic == "Input Widgets":
|
| 302 |
+
cols = st.columns([1.2, 1, 1])
|
| 303 |
+
self.render_input_elements(cols[0])
|
| 304 |
+
self.render_help_section(cols[1])
|
| 305 |
+
self.render_playground(cols[2], dict(self.get_input_elements()))
|
| 306 |
|
| 307 |
+
if __name__ == "__main__":
|
| 308 |
+
app = StreamlitTutorial()
|
| 309 |
+
app.run()
|