Milind Kamat commited on
Commit
32aa98b
Β·
1 Parent(s): 9e400e4

2024 Dec 30: class and method based code

Browse files

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

Files changed (1) hide show
  1. app.py +157 -151
app.py CHANGED
@@ -1,159 +1,165 @@
1
  import streamlit as st
2
 
3
- st.set_page_config(page_title="Learn Streamlit", layout="wide")
4
-
5
- # Custom CSS for better styling
6
- st.markdown("""
7
- <style>
8
- .playground-input { background-color: #f8f9fa; border-radius: 8px; padding: 1rem; }
9
- .output-section { border: 1px solid #ddd; border-radius: 8px; padding: 1rem; margin-top: 1rem; }
10
- .snippet-section { background-color: #f0f2f6; border-radius: 8px; padding: 1rem; margin: 1rem 0; }
11
- .code-example { font-family: monospace; background-color: #f6f6f6; padding: 0.5rem; border-radius: 4px; }
12
- </style>
13
- """, unsafe_allow_html=True)
14
-
15
- # Main navigation sidebar
16
- with st.sidebar:
17
- st.title("Streamlit Tutorial")
18
- selected_topic = st.radio(
19
- "Choose a Topic:",
20
- ["Basic Text Elements", "Input Widgets", "Layouts & Containers",
21
- "Data Display", "Charts & Plots", "Interactive Components"]
22
- )
23
-
24
- if selected_topic == "Basic Text Elements":
25
- # Create three columns
26
- learn_col1, learn_col2, practice_col = st.columns([1, 1, 1])
27
-
28
- # Code Examples Column
29
- with learn_col1:
30
- st.markdown("### πŸ“ Code Examples")
31
- elements = [
32
- ("Title", "st.title('Main Title')"),
33
- ("Header", "st.header('Header')"),
34
- ("Subheader", "st.subheader('Subheader')"),
35
- ("Normal text", "st.write('Normal text')"),
36
- ("Markdown text", "st.markdown('**Bold** and *italic*')"),
37
- ("Colored text", "st.markdown(':blue[Blue text]')")
 
 
 
 
 
 
38
  ]
39
-
40
- for title, code in elements:
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  with st.container(border=True):
42
- st.markdown(f"**{title}**")
43
- st.code(code)
44
-
45
- # Live Output Column
46
- with learn_col2:
47
- st.markdown("### 🎯 Live Output")
48
- with st.container(border=True):
49
- st.title('Main Title')
50
- st.header('Header')
51
- st.subheader('Subheader')
52
- st.write('Normal text')
53
- st.markdown('**Bold** and *italic*')
54
- st.markdown(':blue[Blue text]')
55
-
56
- # Enhanced Playground Column
57
- with practice_col:
58
- st.markdown("### πŸ’» Practice Playground")
59
-
60
- # Template Selection
61
- templates = {
62
- "Basic Text": """st.title('My App')
63
- st.write('Welcome to my app!')""",
64
- "Formatted Text": """st.markdown('# Big Title')
65
- st.markdown('**Bold** and *italic* text')
66
- st.markdown(':blue[Colored] :red[text]')""",
67
- "Multiple Elements": """st.title('My Dashboard')
68
- st.header('Section 1')
69
- st.write('Some content')
70
- st.subheader('Subsection')
71
- st.markdown('- Bullet point 1\\n- Bullet point 2')""",
72
- "Empty": ""
73
  }
74
-
75
- template_choice = st.selectbox("Start with a template:", list(templates.keys()))
76
-
77
- # Code input area with template
78
- if 'code_input' not in st.session_state or template_choice == "Empty":
79
- st.session_state.code_input = templates[template_choice]
80
-
81
- code_input = st.text_area(
82
- "Try Streamlit commands:",
83
- value=st.session_state.code_input,
84
- height=200,
85
- key="playground_code"
86
- )
87
-
88
- # Quick Snippets Section
89
- st.markdown("#### πŸ“š Quick Snippets")
90
-
91
- # Two columns for snippets
92
- snip_col1, snip_col2 = st.columns([3, 1])
93
-
94
- with snip_col1:
95
- snippets = {
96
- "Title": "st.title('My Title')",
97
- "Header": "st.header('My Header')",
98
- "Subheader": "st.subheader('My Subheader')",
99
- "Text": "st.write('Normal text')",
100
- "Bold Markdown": "st.markdown('**Bold text**')",
101
- "Italic Markdown": "st.markdown('*Italic text*')",
102
- "Colored Text": "st.markdown(':blue[Blue text]')",
103
- "Bullet Points": "st.markdown('- Point 1\\n- Point 2')",
104
- "Numbered List": "st.markdown('1. First\\n2. Second')",
105
- "Divider": "st.markdown('---')"
106
- }
107
- snippet = st.selectbox("Choose snippet:", list(snippets.keys()))
108
-
109
- with snip_col2:
110
- if st.button("Add", key="add_snippet"):
111
- if code_input:
112
- code_input += f"\n{snippets[snippet]}"
113
- else:
114
- code_input = snippets[snippet]
115
- st.session_state.code_input = code_input
116
-
117
- # Control buttons
118
- col1, col2, col3 = st.columns([1, 1, 1])
119
- with col1:
120
- if st.button("▢️ Run"):
121
- st.session_state.code_input = code_input
122
- with col2:
123
- if st.button("πŸ”„ Reset"):
124
  st.session_state.code_input = ""
125
- code_input = ""
126
- with col3:
127
- if st.button("πŸ’Ύ Copy"):
128
  st.code(code_input)
129
 
130
- # Output section
131
- st.markdown("#### 🎨 Live Output")
132
- with st.container(border=True):
133
- if code_input:
134
- try:
135
- exec(code_input)
136
- except Exception as e:
137
- st.error(f"Error: {str(e)}")
138
-
139
- # Interactive Help
140
- with st.expander("πŸ’‘ Tips & Help"):
141
- st.markdown("""
142
- #### Quick Tips:
143
- 1. Start with a template or add snippets
144
- 2. Mix different text elements
145
- 3. Use markdown for formatting:
146
- - `**bold**` for **bold**
147
- - `*italic*` for *italic*
148
- - `:blue[text]` for colored text
149
- 4. Press 'Run' to see changes
150
- 5. 'Reset' clears the playground
151
- """)
152
-
153
- # Code Preview
154
- with st.expander("πŸ‘€ Preview Your Code"):
155
- st.code(code_input)
156
-
157
- # Footer
158
- st.markdown("---")
159
- st.caption("Learn more at streamlit.io/docs")
 
 
 
 
 
 
1
  import streamlit as st
2
 
3
+ class StreamlitTutorial:
4
+ def __init__(self):
5
+ self.setup_page_config()
6
+ self.init_session_state()
7
+ self.setup_styles()
8
+
9
+ def setup_page_config(self):
10
+ st.set_page_config(page_title="Learn Streamlit", layout="wide")
11
+
12
+ def init_session_state(self):
13
+ if 'code_input' not in st.session_state:
14
+ st.session_state.code_input = ""
15
+
16
+ def setup_styles(self):
17
+ st.markdown("""
18
+ <style>
19
+ .code-box { background-color: #f6f6f6; border-radius: 5px; padding: 1rem; }
20
+ .output-box { border: 1px solid #ddd; border-radius: 5px; padding: 1rem; }
21
+ .snippet-section { background-color: #f0f2f6; padding: 1rem; }
22
+ </style>
23
+ """, unsafe_allow_html=True)
24
+
25
+ def render_sidebar(self):
26
+ with st.sidebar:
27
+ st.title("Streamlit Tutorial")
28
+ return st.radio(
29
+ "Choose a Topic:",
30
+ ["Basic Text Elements", "Input Widgets", "Layouts & Containers",
31
+ "Data Display", "Charts & Plots", "Interactive Components"]
32
+ )
33
+
34
+ def get_text_elements(self):
35
+ return [
36
+ ("Title", "st.title('Main Title')", lambda: st.title('Main Title')),
37
+ ("Header", "st.header('Header')", lambda: st.header('Header')),
38
+ ("Subheader", "st.subheader('Subheader')", lambda: st.subheader('Subheader')),
39
+ ("Normal text", "st.write('Normal text')", lambda: st.write('Normal text')),
40
+ ("Markdown text", "st.markdown('**Bold** and *italic*')",
41
+ lambda: st.markdown('**Bold** and *italic*')),
42
+ ("Colored text", "st.markdown(':blue[Blue text]')",
43
+ lambda: st.markdown(':blue[Blue text]'))
44
  ]
45
+
46
+ def render_code_examples(self, col):
47
+ with col:
48
+ st.markdown("### πŸ“ Code Examples")
49
+ for title, code, _ in self.get_text_elements():
50
+ with st.container(border=True):
51
+ st.markdown(f"**{title}**")
52
+ st.code(code)
53
+ with st.container(border=True):
54
+ st.markdown("Live output:")
55
+ eval(code)
56
+
57
+ def render_live_output(self, col):
58
+ with col:
59
+ st.markdown("### 🎯 Live Output")
60
  with st.container(border=True):
61
+ for _, _, func in self.get_text_elements():
62
+ func()
63
+
64
+ def get_snippets(self):
65
+ return {
66
+ "Title": "st.title('My Title')",
67
+ "Header": "st.header('My Header')",
68
+ "Subheader": "st.subheader('My Subheader')",
69
+ "Text": "st.write('Normal text')",
70
+ "Bold Markdown": "st.markdown('**Bold text**')",
71
+ "Italic Markdown": "st.markdown('*Italic text*')",
72
+ "Colored Text": "st.markdown(':blue[Blue text]')",
73
+ "Bullet Points": "st.markdown('- Point 1\\n- Point 2')",
74
+ "Numbered List": "st.markdown('1. First\\n2. Second')",
75
+ "Divider": "st.markdown('---')"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  }
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
+ key="playground_input",
87
+ value=st.session_state.code_input
88
+ )
89
+
90
+ # Quick Snippets
91
+ st.markdown("#### πŸ“š Quick Snippets")
92
+ snip_col1, snip_col2 = st.columns([3, 1])
93
+
94
+ with snip_col1:
95
+ snippet = st.selectbox("Choose snippet:",
96
+ list(self.get_snippets().keys()))
97
+
98
+ with snip_col2:
99
+ if st.button("Add"):
100
+ if code_input:
101
+ code_input += f"\n{self.get_snippets()[snippet]}"
102
+ else:
103
+ code_input = self.get_snippets()[snippet]
104
+ st.session_state.code_input = code_input
105
+
106
+ # Control buttons
107
+ btn_col1, btn_col2, btn_col3 = st.columns(3)
108
+ with btn_col1:
109
+ run = st.button("▢️ Run")
110
+ with btn_col2:
111
+ reset = st.button("πŸ”„ Reset")
112
+ with btn_col3:
113
+ copy = st.button("πŸ’Ύ Copy")
114
+
115
+ # Output
116
+ st.markdown("#### 🎨 Live Output")
117
+ with st.container(border=True):
118
+ if code_input and run:
119
+ try:
120
+ exec(code_input)
121
+ except Exception as e:
122
+ st.error(f"Error: {str(e)}")
123
+
124
+ if reset:
 
 
125
  st.session_state.code_input = ""
126
+ st.experimental_rerun()
127
+
128
+ if copy:
129
  st.code(code_input)
130
 
131
+ # Help section
132
+ with st.expander("πŸ’‘ Tips & Help"):
133
+ self.render_tips()
134
+
135
+ def render_tips(self):
136
+ st.markdown("""
137
+ #### Quick Tips:
138
+ 1. Try different text elements
139
+ 2. Use markdown formatting:
140
+ - `**bold**` for **bold**
141
+ - `*italic*` for *italic*
142
+ - `:blue[text]` for colored text
143
+ 3. Combine multiple elements
144
+ 4. Watch live output as you type
145
+ """)
146
+
147
+ def render_footer(self):
148
+ st.markdown("---")
149
+ st.caption("Learn more at streamlit.io/docs")
150
+
151
+ def run(self):
152
+ selected_topic = self.render_sidebar()
153
+
154
+ if selected_topic == "Basic Text Elements":
155
+ col1, col2, col3 = st.columns([1, 1, 1])
156
+ self.render_code_examples(col1)
157
+ self.render_live_output(col2)
158
+ self.render_playground(col3)
159
+
160
+ self.render_footer()
161
+
162
+ # Run the app
163
+ if __name__ == "__main__":
164
+ app = StreamlitTutorial()
165
+ app.run()