Spaces:
Sleeping
Sleeping
Milind Kamat
commited on
Commit
Β·
ec93d45
1
Parent(s):
6a6a0bc
2024 dec 20 new code
Browse filesSigned-off-by: Milind Kamat <36366961+milindkamat0507@users.noreply.github.com>
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-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
border: 1px solid #ddd;
|
| 14 |
border-radius: 4px;
|
| 15 |
padding: 1rem;
|
| 16 |
-
margin
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
}
|
| 18 |
-
.
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
}
|
| 21 |
</style>
|
| 22 |
""", unsafe_allow_html=True)
|
| 23 |
|
| 24 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
with col:
|
| 26 |
st.markdown("### π Code Examples")
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 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
|
| 68 |
with col:
|
| 69 |
-
st.markdown("###
|
| 70 |
-
|
| 71 |
-
st.
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 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 |
placeholder="Example:\nst.title('My Title')"
|
| 87 |
)
|
| 88 |
|
| 89 |
-
# Quick Snippets
|
| 90 |
with st.container(border=True):
|
| 91 |
st.markdown("#### π Quick Snippets")
|
| 92 |
-
|
|
|
|
|
|
|
| 93 |
with cols[0]:
|
| 94 |
-
|
| 95 |
"Choose snippet:",
|
| 96 |
-
|
|
|
|
| 97 |
)
|
| 98 |
with cols[1]:
|
| 99 |
-
st.
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
# Control buttons in a row
|
| 103 |
-
|
| 104 |
-
with
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
st.markdown("#### π¨ Live Output")
|
| 113 |
with st.container(border=True):
|
| 114 |
-
if code_input
|
| 115 |
try:
|
| 116 |
exec(code_input)
|
| 117 |
except Exception as e:
|
| 118 |
st.error(f"Error: {str(e)}")
|
| 119 |
|
| 120 |
-
# Tips
|
| 121 |
with st.expander("π‘ Tips & Help"):
|
| 122 |
-
st.markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
**Quick Tips:**
|
| 124 |
-
- Try different text elements
|
| 125 |
- Use markdown for formatting
|
| 126 |
-
-
|
| 127 |
-
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
def run(self):
|
| 131 |
with st.sidebar:
|
| 132 |
st.title("Streamlit Tutorial")
|
| 133 |
-
|
| 134 |
"Choose a Topic:",
|
| 135 |
["Basic Text Elements", "Input Widgets", "Layouts & Containers",
|
| 136 |
"Data Display", "Charts & Plots", "Interactive Components"]
|
| 137 |
)
|
| 138 |
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
cols = st.columns([1.2, 1, 1])
|
| 141 |
-
self.
|
| 142 |
-
self.
|
| 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()
|