Milind Kamat commited on
Commit
8ba5c6a
Β·
1 Parent(s): 2679bb0

2024 Dec 30 Ver.0 created for improvenment

Browse files

Signed-off-by: Milind Kamat <36366961+milindkamat0507@users.noreply.github.com>

Files changed (1) hide show
  1. app.py +224 -24
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 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()
@@ -18,9 +20,12 @@ class StreamlitTutorial:
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 = ""
@@ -28,9 +33,12 @@ class StreamlitTutorial:
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>
@@ -55,9 +63,12 @@ class StreamlitTutorial:
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')"),
@@ -70,9 +81,12 @@ class StreamlitTutorial:
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')"),
@@ -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
- 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")
@@ -100,10 +118,192 @@ class StreamlitTutorial:
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
- I can document the remaining methods in the same comprehensive way.
 
 
 
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()