Milind Kamat commited on
Commit
d625b64
Β·
1 Parent(s): ec93d45

2024 Dec 30: New start

Browse files

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

Files changed (1) hide show
  1. app.py +98 -161
app.py CHANGED
@@ -1,156 +1,102 @@
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
-
13
- def setup_styles(self):
14
- st.markdown("""
15
- <style>
16
- .code-example {
17
- margin-bottom: 1.5rem;
18
- }
19
- .live-output {
20
- border: 1px solid #ddd;
21
- border-radius: 4px;
22
- padding: 1rem;
23
- margin: 0.5rem 0 1.5rem 0;
24
- }
25
- .section-title {
26
- margin-bottom: 1rem;
27
- padding: 0.5rem 0;
28
- }
29
- .button-container {
30
- display: flex;
31
- justify-content: space-between;
32
- gap: 1rem;
33
- }
34
- </style>
35
- """, unsafe_allow_html=True)
36
-
37
- def get_text_elements(self):
38
- return [
39
- ("Title", "st.title('Main Title')"),
40
- ("Header", "st.header('Header')"),
41
- ("Subheader", "st.subheader('Subheader')"),
42
- ("Normal text", "st.write('Normal text')"),
43
- ("Markdown text", "st.markdown('**Bold** and *italic*')"),
44
- ("Colored text", "st.markdown(':blue[Blue text]')")
45
- ]
46
-
47
- def get_input_elements(self):
48
- return [
49
- ("Text Input", "name = st.text_input('Enter your name')"),
50
- ("Number Input", "num = st.number_input('Enter a number', min_value=0, max_value=100)"),
51
- ("Slider", "value = st.slider('Select value', 0, 100, 50)"),
52
- ("Selectbox", "option = st.selectbox('Choose option', ['A', 'B', 'C'])"),
53
- ("Checkbox", "checked = st.checkbox('Enable feature')")
54
- ]
55
-
56
- def render_text_elements(self, col):
57
- with col:
58
- st.markdown("### πŸ“ Code Examples")
59
- for title, code in self.get_text_elements():
60
- with st.container(border=True):
61
- st.markdown(f"**{title}**")
62
- st.code(code)
63
- st.markdown("Live output:")
64
- exec(code)
65
-
66
- def render_input_elements(self, col):
67
  with col:
68
- st.markdown("### πŸ“ Code Examples")
69
- for title, code in self.get_input_elements():
70
- with st.container(border=True):
71
- st.markdown(f"**{title}**")
72
- st.code(code)
73
- st.markdown("Live output:")
74
- with st.container(border=True):
75
- exec(code)
76
-
77
- def render_playground(self, col, snippets):
78
- with col:
79
- st.markdown("### πŸ’» Practice Playground")
80
 
81
- # Code input area
82
- code_input = st.text_area(
83
- "Try Streamlit commands:",
84
- value=st.session_state.code_input,
85
- height=200,
86
- placeholder="Example:\nst.title('My Title')"
87
- )
88
-
89
- # Quick Snippets with better alignment
90
- with st.container(border=True):
91
- st.markdown("#### πŸ“š Quick Snippets")
92
 
93
- # Align selectbox and button horizontally
94
- cols = st.columns([4, 1])
95
- with cols[0]:
96
- selected_snippet = st.selectbox(
97
- "Choose snippet:",
98
- list(snippets.keys()),
99
- label_visibility="collapsed"
100
- )
101
- with cols[1]:
102
- if st.button("Add", use_container_width=True):
103
- new_code = snippets[selected_snippet]
104
- if code_input:
105
- code_input += f"\n{new_code}"
106
- else:
107
- code_input = new_code
108
- st.session_state.code_input = code_input
109
-
110
- # Control buttons in a row
111
- col1, col2, col3 = st.columns(3)
112
- with col1:
113
- if st.button("▢️ Run", use_container_width=True):
114
- st.session_state.code_input = code_input
115
- with col2:
116
- if st.button("πŸ”„ Reset", use_container_width=True):
117
- st.session_state.code_input = ""
118
- code_input = ""
119
- with col3:
120
- if st.button("πŸ’Ύ Copy", use_container_width=True):
121
- st.code(code_input)
122
-
123
- # Live output
124
- st.markdown("#### 🎨 Live Output")
125
- with st.container(border=True):
126
- if code_input:
127
- try:
128
- exec(code_input)
129
- except Exception as e:
130
- st.error(f"Error: {str(e)}")
131
-
132
- # Tips
133
- with st.expander("πŸ’‘ Tips & Help"):
134
- st.markdown(self.get_topic_tips(st.session_state.current_topic))
135
-
136
- def get_topic_tips(self, topic):
137
- tips = {
138
- "Basic Text Elements": """
139
- **Quick Tips:**
140
- - Use markdown for formatting
141
- - Try different header levels
142
- - Combine text elements
143
- - Use colored text with :color[text]
144
- """,
145
- "Input Widgets": """
146
- **Quick Tips:**
147
- - Use unique keys for widgets
148
- - Store widget values in variables
149
- - Add validation for inputs
150
- - Combine widgets for complex inputs
151
- """
152
- }
153
- return tips.get(topic, "Tips coming soon!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
 
155
  def run(self):
156
  with st.sidebar:
@@ -161,17 +107,8 @@ class StreamlitTutorial:
161
  "Data Display", "Charts & Plots", "Interactive Components"]
162
  )
163
 
164
- # Render appropriate content based on selected topic
165
  if st.session_state.current_topic == "Basic Text Elements":
166
  cols = st.columns([1.2, 1, 1])
167
  self.render_text_elements(cols[0])
168
- self.render_playground(cols[2], dict(self.get_text_elements()))
169
-
170
- elif st.session_state.current_topic == "Input Widgets":
171
- cols = st.columns([1.2, 1, 1])
172
- self.render_input_elements(cols[0])
173
- self.render_playground(cols[2], dict(self.get_input_elements()))
174
-
175
- if __name__ == "__main__":
176
- app = StreamlitTutorial()
177
- app.run()
 
1
+ def render_help_section(self, col):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  with col:
3
+ st.markdown("### πŸ“š Learning Guide")
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ # Different help topics in expandable sections
6
+ with st.expander("🎯 Basic Concepts", expanded=True):
7
+ st.markdown("""
8
+ **What are Text Elements?**
9
+ - Basic building blocks for displaying text
10
+ - Range from titles to formatted text
11
+ - Support markdown formatting
12
+ """)
 
 
 
13
 
14
+ with st.expander("πŸ’‘ Best Practices"):
15
+ st.markdown("""
16
+ **Writing Tips:**
17
+ 1. Use appropriate heading levels
18
+ 2. Keep text concise and clear
19
+ 3. Use formatting for emphasis
20
+ 4. Add visual hierarchy with headers
21
+
22
+ **Code Structure:**
23
+ ```python
24
+ st.title('Main Title')
25
+ st.header('Section Header')
26
+ st.subheader('Sub-section')
27
+ st.write('Regular text')
28
+ ```
29
+ """)
30
+
31
+ with st.expander("πŸ” Examples & Use Cases"):
32
+ st.markdown("""
33
+ **Common Patterns:**
34
+
35
+ 1. Page Structure
36
+ ```python
37
+ st.title('Dashboard')
38
+ st.header('Sales Data')
39
+ st.subheader('Monthly Trends')
40
+ ```
41
+
42
+ 2. Formatted Text
43
+ ```python
44
+ st.markdown('**Bold** and *italic*')
45
+ st.markdown(':blue[Colored text]')
46
+ ```
47
+
48
+ 3. Mixed Elements
49
+ ```python
50
+ st.title('Report')
51
+ st.write('Regular text')
52
+ st.markdown('- Bullet point')
53
+ ```
54
+ """)
55
+
56
+ with st.expander("⚠️ Common Mistakes"):
57
+ st.markdown("""
58
+ 1. Skipping header levels
59
+ 2. Overusing formatting
60
+ 3. Inconsistent styling
61
+ 4. Missing hierarchy
62
+
63
+ **How to Avoid:**
64
+ - Plan your page structure
65
+ - Use consistent formatting
66
+ - Test different screen sizes
67
+ - Keep it simple
68
+ """)
69
+
70
+ with st.expander("πŸš€ Advanced Tips"):
71
+ st.markdown("""
72
+ **Advanced Formatting:**
73
+ ```python
74
+ # Custom HTML
75
+ st.markdown('''
76
+ <span style='color:blue'>
77
+ Custom styled text
78
+ </span>
79
+ ''', unsafe_allow_html=True)
80
+
81
+ # Complex Markdown
82
+ st.markdown('''
83
+ # Title
84
+ ## Subtitle
85
+ * Point 1
86
+ * Subpoint
87
+ > Quote
88
+ ''')
89
+ ```
90
+
91
+ **Layout Combinations:**
92
+ ```python
93
+ col1, col2 = st.columns(2)
94
+ with col1:
95
+ st.header('Column 1')
96
+ with col2:
97
+ st.header('Column 2')
98
+ ```
99
+ """)
100
 
101
  def run(self):
102
  with st.sidebar:
 
107
  "Data Display", "Charts & Plots", "Interactive Components"]
108
  )
109
 
 
110
  if st.session_state.current_topic == "Basic Text Elements":
111
  cols = st.columns([1.2, 1, 1])
112
  self.render_text_elements(cols[0])
113
+ self.render_help_section(cols[1]) # New help section in middle
114
+ self.render_playground(cols[2], dict(self.get_text_elements()))