Spaces:
Sleeping
Sleeping
Milind Kamat
commited on
Commit
Β·
84512f8
1
Parent(s):
d625b64
2024 Dec 30: new code
Browse filesSigned-off-by: Milind Kamat <36366961+milindkamat0507@users.noreply.github.com>
app.py
CHANGED
|
@@ -1,8 +1,85 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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?**
|
|
@@ -98,6 +175,83 @@ def render_help_section(self, col):
|
|
| 98 |
```
|
| 99 |
""")
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
def run(self):
|
| 102 |
with st.sidebar:
|
| 103 |
st.title("Streamlit Tutorial")
|
|
@@ -110,5 +264,15 @@ def render_help_section(self, col):
|
|
| 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])
|
| 114 |
-
self.render_playground(cols[2], dict(self.get_text_elements()))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
st.session_state.current_topic = "Basic Text Elements"
|
| 13 |
+
|
| 14 |
+
def setup_styles(self):
|
| 15 |
+
st.markdown("""
|
| 16 |
+
<style>
|
| 17 |
+
.code-example {
|
| 18 |
+
margin-bottom: 1.5rem;
|
| 19 |
+
}
|
| 20 |
+
.live-output {
|
| 21 |
+
border: 1px solid #ddd;
|
| 22 |
+
border-radius: 4px;
|
| 23 |
+
padding: 1rem;
|
| 24 |
+
margin: 0.5rem 0 1.5rem 0;
|
| 25 |
+
}
|
| 26 |
+
.section-title {
|
| 27 |
+
margin-bottom: 1rem;
|
| 28 |
+
padding: 0.5rem 0;
|
| 29 |
+
}
|
| 30 |
+
.button-container {
|
| 31 |
+
display: flex;
|
| 32 |
+
justify-content: space-between;
|
| 33 |
+
gap: 1rem;
|
| 34 |
+
}
|
| 35 |
+
</style>
|
| 36 |
+
""", unsafe_allow_html=True)
|
| 37 |
+
|
| 38 |
+
def get_text_elements(self):
|
| 39 |
+
return [
|
| 40 |
+
("Title", "st.title('Main Title')"),
|
| 41 |
+
("Header", "st.header('Header')"),
|
| 42 |
+
("Subheader", "st.subheader('Subheader')"),
|
| 43 |
+
("Normal text", "st.write('Normal text')"),
|
| 44 |
+
("Markdown text", "st.markdown('**Bold** and *italic*')"),
|
| 45 |
+
("Colored text", "st.markdown(':blue[Blue text]')")
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
def get_input_elements(self):
|
| 49 |
+
return [
|
| 50 |
+
("Text Input", "name = st.text_input('Enter your name')"),
|
| 51 |
+
("Number Input", "num = st.number_input('Enter a number', min_value=0, max_value=100)"),
|
| 52 |
+
("Slider", "value = st.slider('Select value', 0, 100, 50)"),
|
| 53 |
+
("Selectbox", "option = st.selectbox('Choose option', ['A', 'B', 'C'])"),
|
| 54 |
+
("Checkbox", "checked = st.checkbox('Enable feature')")
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
+
def render_text_elements(self, col):
|
| 58 |
+
with col:
|
| 59 |
+
st.markdown("### π Code Examples")
|
| 60 |
+
for title, code in self.get_text_elements():
|
| 61 |
+
with st.container(border=True):
|
| 62 |
+
st.markdown(f"**{title}**")
|
| 63 |
+
st.code(code)
|
| 64 |
+
st.markdown("Live output:")
|
| 65 |
+
with st.container(border=True):
|
| 66 |
+
exec(code)
|
| 67 |
+
|
| 68 |
+
def render_input_elements(self, col):
|
| 69 |
+
with col:
|
| 70 |
+
st.markdown("### π Code Examples")
|
| 71 |
+
for title, code in self.get_input_elements():
|
| 72 |
+
with st.container(border=True):
|
| 73 |
+
st.markdown(f"**{title}**")
|
| 74 |
+
st.code(code)
|
| 75 |
+
st.markdown("Live output:")
|
| 76 |
+
with st.container(border=True):
|
| 77 |
+
exec(code)
|
| 78 |
+
|
| 79 |
+
def render_help_section(self, col):
|
| 80 |
with col:
|
| 81 |
st.markdown("### π Learning Guide")
|
| 82 |
|
|
|
|
| 83 |
with st.expander("π― Basic Concepts", expanded=True):
|
| 84 |
st.markdown("""
|
| 85 |
**What are Text Elements?**
|
|
|
|
| 175 |
```
|
| 176 |
""")
|
| 177 |
|
| 178 |
+
def render_playground(self, col, snippets):
|
| 179 |
+
with col:
|
| 180 |
+
st.markdown("### π» Practice Playground")
|
| 181 |
+
|
| 182 |
+
# Code input area
|
| 183 |
+
code_input = st.text_area(
|
| 184 |
+
"Try Streamlit commands:",
|
| 185 |
+
value=st.session_state.code_input,
|
| 186 |
+
height=200,
|
| 187 |
+
placeholder="Example:\nst.title('My Title')"
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
# Quick Snippets with better alignment
|
| 191 |
+
with st.container(border=True):
|
| 192 |
+
st.markdown("#### π Quick Snippets")
|
| 193 |
+
|
| 194 |
+
cols = st.columns([4, 1])
|
| 195 |
+
with cols[0]:
|
| 196 |
+
selected_snippet = st.selectbox(
|
| 197 |
+
"Choose snippet:",
|
| 198 |
+
list(snippets.keys()),
|
| 199 |
+
label_visibility="collapsed"
|
| 200 |
+
)
|
| 201 |
+
with cols[1]:
|
| 202 |
+
if st.button("Add", use_container_width=True):
|
| 203 |
+
new_code = snippets[selected_snippet]
|
| 204 |
+
if code_input:
|
| 205 |
+
code_input += f"\n{new_code}"
|
| 206 |
+
else:
|
| 207 |
+
code_input = new_code
|
| 208 |
+
st.session_state.code_input = code_input
|
| 209 |
+
|
| 210 |
+
# Control buttons
|
| 211 |
+
col1, col2, col3 = st.columns(3)
|
| 212 |
+
with col1:
|
| 213 |
+
if st.button("βΆοΈ Run", use_container_width=True):
|
| 214 |
+
st.session_state.code_input = code_input
|
| 215 |
+
with col2:
|
| 216 |
+
if st.button("π Reset", use_container_width=True):
|
| 217 |
+
st.session_state.code_input = ""
|
| 218 |
+
code_input = ""
|
| 219 |
+
with col3:
|
| 220 |
+
if st.button("πΎ Copy", use_container_width=True):
|
| 221 |
+
st.code(code_input)
|
| 222 |
+
|
| 223 |
+
# Live output
|
| 224 |
+
st.markdown("#### π¨ Live Output")
|
| 225 |
+
with st.container(border=True):
|
| 226 |
+
if code_input:
|
| 227 |
+
try:
|
| 228 |
+
exec(code_input)
|
| 229 |
+
except Exception as e:
|
| 230 |
+
st.error(f"Error: {str(e)}")
|
| 231 |
+
|
| 232 |
+
# Tips
|
| 233 |
+
with st.expander("π‘ Tips & Help"):
|
| 234 |
+
st.markdown(self.get_topic_tips(st.session_state.current_topic))
|
| 235 |
+
|
| 236 |
+
def get_topic_tips(self, topic):
|
| 237 |
+
tips = {
|
| 238 |
+
"Basic Text Elements": """
|
| 239 |
+
**Quick Tips:**
|
| 240 |
+
- Use markdown for formatting
|
| 241 |
+
- Try different header levels
|
| 242 |
+
- Combine text elements
|
| 243 |
+
- Use colored text with :color[text]
|
| 244 |
+
""",
|
| 245 |
+
"Input Widgets": """
|
| 246 |
+
**Quick Tips:**
|
| 247 |
+
- Use unique keys for widgets
|
| 248 |
+
- Store widget values in variables
|
| 249 |
+
- Add validation for inputs
|
| 250 |
+
- Combine widgets for complex inputs
|
| 251 |
+
"""
|
| 252 |
+
}
|
| 253 |
+
return tips.get(topic, "Tips coming soon!")
|
| 254 |
+
|
| 255 |
def run(self):
|
| 256 |
with st.sidebar:
|
| 257 |
st.title("Streamlit Tutorial")
|
|
|
|
| 264 |
if st.session_state.current_topic == "Basic Text Elements":
|
| 265 |
cols = st.columns([1.2, 1, 1])
|
| 266 |
self.render_text_elements(cols[0])
|
| 267 |
+
self.render_help_section(cols[1])
|
| 268 |
+
self.render_playground(cols[2], dict(self.get_text_elements()))
|
| 269 |
+
|
| 270 |
+
elif st.session_state.current_topic == "Input Widgets":
|
| 271 |
+
cols = st.columns([1.2, 1, 1])
|
| 272 |
+
self.render_input_elements(cols[0])
|
| 273 |
+
self.render_help_section(cols[1])
|
| 274 |
+
self.render_playground(cols[2], dict(self.get_input_elements()))
|
| 275 |
+
|
| 276 |
+
if __name__ == "__main__":
|
| 277 |
+
app = StreamlitTutorial()
|
| 278 |
+
app.run()
|