Spaces:
Sleeping
Sleeping
Milind Kamat
commited on
Commit
Β·
6a6a0bc
1
Parent(s):
32aa98b
2024 Dec 30 : new updates
Browse filesSigned-off-by: Milind Kamat <36366961+milindkamat0507@users.noreply.github.com>
app.py
CHANGED
|
@@ -2,78 +2,78 @@ 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-
|
| 20 |
-
.output
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
def render_live_output(self, col):
|
| 58 |
with col:
|
| 59 |
st.markdown("### π― Live Output")
|
| 60 |
with st.container(border=True):
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 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:
|
|
@@ -83,36 +83,32 @@ class StreamlitTutorial:
|
|
| 83 |
code_input = st.text_area(
|
| 84 |
"Try Streamlit commands:",
|
| 85 |
height=200,
|
| 86 |
-
|
| 87 |
-
value=st.session_state.code_input
|
| 88 |
)
|
| 89 |
|
| 90 |
-
# Quick Snippets
|
| 91 |
-
st.
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 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
|
| 111 |
reset = st.button("π Reset")
|
| 112 |
-
with
|
| 113 |
copy = st.button("πΎ Copy")
|
| 114 |
|
| 115 |
-
#
|
| 116 |
st.markdown("#### π¨ Live Output")
|
| 117 |
with st.container(border=True):
|
| 118 |
if code_input and run:
|
|
@@ -121,45 +117,31 @@ class StreamlitTutorial:
|
|
| 121 |
except Exception as e:
|
| 122 |
st.error(f"Error: {str(e)}")
|
| 123 |
|
| 124 |
-
|
| 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 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
if selected_topic == "Basic Text Elements":
|
| 155 |
-
|
| 156 |
-
self.render_code_examples(
|
| 157 |
-
self.render_live_output(
|
| 158 |
-
self.render_playground(
|
| 159 |
-
|
| 160 |
-
self.render_footer()
|
| 161 |
|
| 162 |
-
# Run the app
|
| 163 |
if __name__ == "__main__":
|
| 164 |
app = StreamlitTutorial()
|
| 165 |
app.run()
|
|
|
|
| 2 |
|
| 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:
|
|
|
|
| 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:
|
|
|
|
| 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()
|
| 147 |
app.run()
|