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

2024 dec 20 new code

Browse files

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

Files changed (1) hide show
  1. app.py +114 -84
app.py CHANGED
@@ -3,144 +3,174 @@ import streamlit as st
3
  class StreamlitTutorial:
4
  def __init__(self):
5
  st.set_page_config(page_title="Learn Streamlit", layout="wide")
 
6
  self.setup_styles()
7
 
 
 
 
 
8
  def setup_styles(self):
9
  st.markdown("""
10
  <style>
11
- .code-block { margin-bottom: 1rem; }
12
- .live-output {
 
 
13
  border: 1px solid #ddd;
14
  border-radius: 4px;
15
  padding: 1rem;
16
- margin-bottom: 2rem;
 
 
 
 
17
  }
18
- .stButton > button {
19
- width: 100%;
 
 
20
  }
21
  </style>
22
  """, unsafe_allow_html=True)
23
 
24
- def render_code_examples(self, col):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  with col:
26
  st.markdown("### πŸ“ Code Examples")
27
-
28
- # Title example
29
- st.markdown("**Title**")
30
- st.code("st.title('Main Title')")
31
- st.markdown("Live output:")
32
- with st.container(border=True):
33
- st.title("Main Title")
34
- st.markdown("---")
35
-
36
- # Header example
37
- st.markdown("**Header**")
38
- st.code("st.header('Header')")
39
- st.markdown("Live output:")
40
- with st.container(border=True):
41
- st.header("Header")
42
- st.markdown("---")
43
-
44
- # Subheader example
45
- st.markdown("**Subheader**")
46
- st.code("st.subheader('Subheader')")
47
- st.markdown("Live output:")
48
- with st.container(border=True):
49
- st.subheader("Subheader")
50
- st.markdown("---")
51
-
52
- # Normal text example
53
- st.markdown("**Normal text**")
54
- st.code("st.write('Normal text')")
55
- st.markdown("Live output:")
56
- with st.container(border=True):
57
- st.write("Normal text")
58
- st.markdown("---")
59
-
60
- # Markdown text example
61
- st.markdown("**Markdown text**")
62
- st.code("st.markdown('**Bold** and *italic*')")
63
- st.markdown("Live output:")
64
- with st.container(border=True):
65
- st.markdown("**Bold** and *italic*")
66
 
67
- def render_live_output(self, col):
68
  with col:
69
- st.markdown("### 🎯 Live Output")
70
- with st.container(border=True):
71
- st.title("Main Title")
72
- st.header("Header")
73
- st.subheader("Subheader")
74
- st.write("Normal text")
75
- st.markdown("**Bold** and *italic*")
76
- st.markdown(":blue[Blue text]")
77
-
78
- def render_playground(self, col):
79
  with col:
80
  st.markdown("### πŸ’» Practice Playground")
81
 
82
- # Code input
83
  code_input = st.text_area(
84
  "Try Streamlit commands:",
 
85
  height=200,
86
  placeholder="Example:\nst.title('My Title')"
87
  )
88
 
89
- # Quick Snippets in a container
90
  with st.container(border=True):
91
  st.markdown("#### πŸ“š Quick Snippets")
92
- cols = st.columns([3, 1])
 
 
93
  with cols[0]:
94
- snippet = st.selectbox(
95
  "Choose snippet:",
96
- ["Title", "Header", "Subheader", "Text", "Markdown"]
 
97
  )
98
  with cols[1]:
99
- st.markdown("#") # Spacing
100
- add_button = st.button("Add")
 
 
 
 
 
101
 
102
  # Control buttons in a row
103
- button_cols = st.columns(3)
104
- with button_cols[0]:
105
- run = st.button("▢️ Run")
106
- with button_cols[1]:
107
- reset = st.button("πŸ”„ Reset")
108
- with button_cols[2]:
109
- copy = st.button("πŸ’Ύ Copy")
110
-
111
- # Live output with border
 
 
 
 
112
  st.markdown("#### 🎨 Live Output")
113
  with st.container(border=True):
114
- if code_input and run:
115
  try:
116
  exec(code_input)
117
  except Exception as e:
118
  st.error(f"Error: {str(e)}")
119
 
120
- # Tips in expandable section
121
  with st.expander("πŸ’‘ Tips & Help"):
122
- st.markdown("""
 
 
 
 
123
  **Quick Tips:**
124
- - Try different text elements
125
  - Use markdown for formatting
126
- - Mix and match elements
127
- - Watch live output as you type
128
- """)
 
 
 
 
 
 
 
 
 
 
129
 
130
  def run(self):
131
  with st.sidebar:
132
  st.title("Streamlit Tutorial")
133
- selected_topic = st.radio(
134
  "Choose a Topic:",
135
  ["Basic Text Elements", "Input Widgets", "Layouts & Containers",
136
  "Data Display", "Charts & Plots", "Interactive Components"]
137
  )
138
 
139
- if selected_topic == "Basic Text Elements":
 
 
 
 
 
 
140
  cols = st.columns([1.2, 1, 1])
141
- self.render_code_examples(cols[0])
142
- self.render_live_output(cols[1])
143
- self.render_playground(cols[2])
144
 
145
  if __name__ == "__main__":
146
  app = StreamlitTutorial()
 
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:
157
  st.title("Streamlit Tutorial")
158
+ st.session_state.current_topic = st.radio(
159
  "Choose a Topic:",
160
  ["Basic Text Elements", "Input Widgets", "Layouts & Containers",
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()