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

2024 Dec 30: version improved alignment

Browse files

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

Files changed (1) hide show
  1. app.py +133 -85
app.py CHANGED
@@ -2,110 +2,158 @@ import streamlit as st
2
 
3
  st.set_page_config(page_title="Learn Streamlit", layout="wide")
4
 
 
 
 
 
 
 
 
 
 
 
5
  # Main navigation sidebar
6
  with st.sidebar:
7
  st.title("Streamlit Tutorial")
8
  selected_topic = st.radio(
9
  "Choose a Topic:",
10
- [
11
- "Basic Text Elements",
12
- "Input Widgets",
13
- "Layouts & Containers",
14
- "Data Display",
15
- "Charts & Plots",
16
- "Interactive Components"
17
- ]
18
  )
19
 
20
  if selected_topic == "Basic Text Elements":
21
- st.title("Basic Text Elements in Streamlit")
22
-
23
- # Create two main sections: Learning and Practice
24
- learning_col, practice_col = st.columns([1.2, 0.8])
25
-
26
- # Learning Section (Code Examples + Live Output)
27
- with learning_col:
28
- st.markdown("### πŸ“š Learning Section")
29
-
30
- # Create two sub-columns for code and output
31
- code_col, output_col = st.columns(2)
 
 
 
32
 
33
- with code_col:
34
- st.markdown("#### πŸ“ Code Examples")
35
-
36
- st.markdown("**Title**")
37
- st.code("st.title('Main Title')")
38
-
39
- st.markdown("**Header**")
40
- st.code("st.header('Header')")
41
-
42
- st.markdown("**Subheader**")
43
- st.code("st.subheader('Subheader')")
44
-
45
- st.markdown("**Normal text**")
46
- st.code("st.write('Normal text')")
47
-
48
- st.markdown("**Markdown text**")
49
- st.code("st.markdown('**Bold** and *italic*')")
50
-
51
- st.markdown("**Colored text**")
52
- st.code("st.markdown(':blue[Blue text]')")
53
-
54
- with output_col:
55
- st.markdown("#### 🎯 Live Output")
56
  with st.container(border=True):
57
- st.title('Main Title')
58
- st.header('Header')
59
- st.subheader('Subheader')
60
- st.write('Normal text')
61
- st.markdown('**Bold** and *italic*')
62
- st.markdown(':blue[Blue text]')
 
 
 
 
 
 
 
63
 
64
- # Practice Section (Playground)
65
  with practice_col:
66
  st.markdown("### πŸ’» Practice Playground")
67
 
68
- with st.container(border=True):
69
- code = st.text_area(
70
- "Try Streamlit commands:",
71
- height=200,
72
- placeholder="Example:\nst.title('My Title')"
73
- )
74
-
75
- st.markdown("#### Quick Snippets")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  snippets = {
77
  "Title": "st.title('My Title')",
78
  "Header": "st.header('My Header')",
 
 
 
 
79
  "Colored Text": "st.markdown(':blue[Blue text]')",
80
- "Bold & Italic": "st.markdown('**Bold** and *italic*')",
 
 
81
  }
82
-
83
- col1, col2 = st.columns([2, 1])
84
- with col1:
85
- selected_snippet = st.selectbox("Insert:", list(snippets.keys()))
86
- with col2:
87
- if st.button("Add Snippet"):
88
- if code:
89
- code += "\n" + snippets[selected_snippet]
90
- else:
91
- code = snippets[selected_snippet]
92
-
93
- st.markdown("#### Output")
94
- with st.container(border=True):
95
- if code:
96
- try:
97
- exec(code)
98
- except Exception as e:
99
- st.error(f"Error: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
- # Tips Section
102
- with st.expander("πŸ’‘ Tips & Tricks"):
103
- st.markdown("""
104
- - Use `st.write()` for simple text output
105
- - Use markdown for formatted text
106
- - Try different header levels (title, header, subheader)
107
- - Experiment with colored text using `:blue[text]`, `:red[text]`, etc.
108
- """)
109
 
 
110
  st.markdown("---")
111
- st.caption("πŸ“š Learn more at streamlit.io/docs")
 
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")