Spaces:
Sleeping
Sleeping
Milind Kamat
commited on
Commit
Β·
4d2f949
1
Parent(s):
a97cd9f
2024 Dec 30 : New code added for playground
Browse filesSigned-off-by: Milind Kamat <36366961+milindkamat0507@users.noreply.github.com>
app.py
CHANGED
|
@@ -1,6 +1,4 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
|
| 5 |
st.set_page_config(page_title="Learn Streamlit", layout="wide")
|
| 6 |
|
|
@@ -10,205 +8,123 @@ with st.sidebar:
|
|
| 10 |
selected_topic = st.radio(
|
| 11 |
"Choose a Topic:",
|
| 12 |
[
|
| 13 |
-
"
|
| 14 |
-
"
|
| 15 |
-
"
|
| 16 |
-
"
|
| 17 |
-
"
|
| 18 |
-
"
|
| 19 |
-
"
|
| 20 |
]
|
| 21 |
)
|
| 22 |
|
| 23 |
-
if selected_topic == "
|
| 24 |
st.title("Basic Text Elements in Streamlit")
|
| 25 |
|
| 26 |
-
#
|
| 27 |
col1, col2 = st.columns(2)
|
| 28 |
|
| 29 |
with col1:
|
| 30 |
-
st.
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
st.title('Main Title')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
# Header
|
| 36 |
-
st.header('Header')
|
| 37 |
-
|
| 38 |
-
# Subheader
|
| 39 |
-
st.subheader('Subheader')
|
| 40 |
-
|
| 41 |
-
# Normal text
|
| 42 |
-
st.write('Normal text')
|
| 43 |
-
|
| 44 |
-
# Markdown text
|
| 45 |
-
st.markdown('**Bold** and *italic*')
|
| 46 |
-
|
| 47 |
-
# Colored text
|
| 48 |
-
st.markdown(':blue[Blue text]')
|
| 49 |
-
""")
|
| 50 |
-
|
| 51 |
with col2:
|
| 52 |
-
st.
|
| 53 |
st.title('Main Title')
|
|
|
|
|
|
|
| 54 |
st.header('Header')
|
|
|
|
|
|
|
| 55 |
st.subheader('Subheader')
|
|
|
|
|
|
|
| 56 |
st.write('Normal text')
|
|
|
|
|
|
|
| 57 |
st.markdown('**Bold** and *italic*')
|
|
|
|
|
|
|
| 58 |
st.markdown(':blue[Blue text]')
|
| 59 |
|
| 60 |
-
elif selected_topic == "
|
| 61 |
-
st.title("
|
| 62 |
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
with col1:
|
| 66 |
-
st.
|
| 67 |
-
st.
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
#
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
st.
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
age = st.number_input('Enter age', min_value=0, max_value=120)
|
| 95 |
-
value = st.slider('Select value', 0, 100)
|
| 96 |
-
agree = st.checkbox('I agree')
|
| 97 |
-
option = st.selectbox('Choose option', ['A', 'B', 'C'])
|
| 98 |
-
|
| 99 |
-
elif selected_topic == "3. Layouts & Containers":
|
| 100 |
-
st.title("Layouts & Containers")
|
| 101 |
|
| 102 |
-
st.header("1. Columns")
|
| 103 |
-
st.code("col1, col2 = st.columns(2)")
|
| 104 |
-
col1, col2 = st.columns(2)
|
| 105 |
-
with col1:
|
| 106 |
-
st.write("This is column 1")
|
| 107 |
with col2:
|
| 108 |
-
st.
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
st.header("3. Expander")
|
| 119 |
-
with st.expander("Click to expand"):
|
| 120 |
-
st.write("Hidden content revealed!")
|
| 121 |
-
|
| 122 |
-
elif selected_topic == "4. Data Display":
|
| 123 |
-
st.title("Working with Data")
|
| 124 |
-
|
| 125 |
-
# Create sample dataframe
|
| 126 |
-
df = pd.DataFrame({
|
| 127 |
-
'Name': ['John', 'Anna', 'Peter'],
|
| 128 |
-
'Age': [25, 30, 35],
|
| 129 |
-
'City': ['New York', 'Paris', 'London']
|
| 130 |
-
})
|
| 131 |
-
|
| 132 |
-
st.header("Display DataFrame")
|
| 133 |
-
st.code("""
|
| 134 |
-
# Create DataFrame
|
| 135 |
-
df = pd.DataFrame({
|
| 136 |
-
'Name': ['John', 'Anna', 'Peter'],
|
| 137 |
-
'Age': [25, 30, 35],
|
| 138 |
-
'City': ['New York', 'Paris', 'London']
|
| 139 |
-
})
|
| 140 |
-
|
| 141 |
-
# Display as table
|
| 142 |
-
st.dataframe(df)
|
| 143 |
-
|
| 144 |
-
# Display as static table
|
| 145 |
-
st.table(df)
|
| 146 |
-
""")
|
| 147 |
-
|
| 148 |
-
st.dataframe(df)
|
| 149 |
-
st.table(df)
|
| 150 |
-
|
| 151 |
-
elif selected_topic == "5. Charts & Plots":
|
| 152 |
-
st.title("Creating Charts")
|
| 153 |
-
|
| 154 |
-
# Generate sample data
|
| 155 |
-
chart_data = pd.DataFrame(
|
| 156 |
-
np.random.randn(20, 3),
|
| 157 |
-
columns=['A', 'B', 'C']
|
| 158 |
-
)
|
| 159 |
-
|
| 160 |
-
st.header("Line Chart")
|
| 161 |
-
st.code("st.line_chart(chart_data)")
|
| 162 |
-
st.line_chart(chart_data)
|
| 163 |
-
|
| 164 |
-
st.header("Bar Chart")
|
| 165 |
-
st.code("st.bar_chart(chart_data)")
|
| 166 |
-
st.bar_chart(chart_data)
|
| 167 |
-
|
| 168 |
-
st.header("Area Chart")
|
| 169 |
-
st.code("st.area_chart(chart_data)")
|
| 170 |
-
st.area_chart(chart_data)
|
| 171 |
-
|
| 172 |
-
elif selected_topic == "6. Interactive Components":
|
| 173 |
-
st.title("Interactive Components")
|
| 174 |
-
|
| 175 |
-
st.header("Form Example")
|
| 176 |
-
with st.form("my_form"):
|
| 177 |
-
st.write("Inside the form")
|
| 178 |
-
name = st.text_input("Name")
|
| 179 |
-
age = st.slider("Age", 0, 100, 25)
|
| 180 |
-
submitted = st.form_submit_button("Submit")
|
| 181 |
-
if submitted:
|
| 182 |
-
st.write(f"Name: {name}, Age: {age}")
|
| 183 |
-
|
| 184 |
-
st.header("File Uploader")
|
| 185 |
-
uploaded_file = st.file_uploader("Choose a file")
|
| 186 |
-
if uploaded_file:
|
| 187 |
-
st.write("File uploaded!")
|
| 188 |
-
|
| 189 |
-
elif selected_topic == "Try It Yourself":
|
| 190 |
-
st.title("Practice Zone")
|
| 191 |
-
|
| 192 |
-
st.write("Try writing some Streamlit code below!")
|
| 193 |
-
|
| 194 |
-
code = st.text_area("Your code:", height=200,
|
| 195 |
-
placeholder="Example:\nst.write('Hello World!')")
|
| 196 |
-
|
| 197 |
-
if st.button("Run Code"):
|
| 198 |
-
try:
|
| 199 |
-
exec(code)
|
| 200 |
-
except Exception as e:
|
| 201 |
-
st.error(f"Error: {str(e)}")
|
| 202 |
-
|
| 203 |
-
# Footer with helpful tips
|
| 204 |
with st.expander("π‘ Tips & Tricks"):
|
| 205 |
st.markdown("""
|
| 206 |
-
|
| 207 |
-
-
|
| 208 |
-
-
|
| 209 |
-
-
|
| 210 |
-
-
|
|
|
|
| 211 |
""")
|
| 212 |
|
| 213 |
st.markdown("---")
|
| 214 |
-
st.caption("Learn more at streamlit.io/docs")
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
|
| 3 |
st.set_page_config(page_title="Learn Streamlit", layout="wide")
|
| 4 |
|
|
|
|
| 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 |
+
"Playground"
|
| 18 |
]
|
| 19 |
)
|
| 20 |
|
| 21 |
+
if selected_topic == "Basic Text Elements":
|
| 22 |
st.title("Basic Text Elements in Streamlit")
|
| 23 |
|
| 24 |
+
# Create two equal columns
|
| 25 |
col1, col2 = st.columns(2)
|
| 26 |
|
| 27 |
with col1:
|
| 28 |
+
st.markdown("### π Code Examples")
|
| 29 |
+
# Code section with comments
|
| 30 |
+
st.markdown("#### Title")
|
| 31 |
+
st.code("st.title('Main Title')")
|
| 32 |
+
st.markdown("β³") # Arrow pointing to output
|
| 33 |
+
|
| 34 |
+
st.markdown("#### Header")
|
| 35 |
+
st.code("st.header('Header')")
|
| 36 |
+
st.markdown("β³")
|
| 37 |
+
|
| 38 |
+
st.markdown("#### Subheader")
|
| 39 |
+
st.code("st.subheader('Subheader')")
|
| 40 |
+
st.markdown("β³")
|
| 41 |
+
|
| 42 |
+
st.markdown("#### Normal text")
|
| 43 |
+
st.code("st.write('Normal text')")
|
| 44 |
+
st.markdown("β³")
|
| 45 |
+
|
| 46 |
+
st.markdown("#### Markdown text")
|
| 47 |
+
st.code("st.markdown('**Bold** and *italic*')")
|
| 48 |
+
st.markdown("β³")
|
| 49 |
+
|
| 50 |
+
st.markdown("#### Colored text")
|
| 51 |
+
st.code("st.markdown(':blue[Blue text]')")
|
| 52 |
+
st.markdown("β³")
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
with col2:
|
| 55 |
+
st.markdown("### π― Live Output")
|
| 56 |
st.title('Main Title')
|
| 57 |
+
st.markdown("---") # Separator
|
| 58 |
+
|
| 59 |
st.header('Header')
|
| 60 |
+
st.markdown("---")
|
| 61 |
+
|
| 62 |
st.subheader('Subheader')
|
| 63 |
+
st.markdown("---")
|
| 64 |
+
|
| 65 |
st.write('Normal text')
|
| 66 |
+
st.markdown("---")
|
| 67 |
+
|
| 68 |
st.markdown('**Bold** and *italic*')
|
| 69 |
+
st.markdown("---")
|
| 70 |
+
|
| 71 |
st.markdown(':blue[Blue text]')
|
| 72 |
|
| 73 |
+
elif selected_topic == "Playground":
|
| 74 |
+
st.title("π» Streamlit Playground")
|
| 75 |
|
| 76 |
+
# Split screen for code and output
|
| 77 |
+
col1, col2 = st.columns([1, 1])
|
| 78 |
|
| 79 |
with col1:
|
| 80 |
+
st.markdown("### βοΈ Write Your Code")
|
| 81 |
+
code = st.text_area(
|
| 82 |
+
"Try Streamlit commands here:",
|
| 83 |
+
height=400,
|
| 84 |
+
placeholder="""Examples to try:
|
| 85 |
+
|
| 86 |
+
st.title('My Title')
|
| 87 |
+
st.write('Hello World!')
|
| 88 |
+
st.markdown('**Bold** and *italic*')
|
| 89 |
+
st.markdown(':blue[Colored text]')
|
| 90 |
+
"""
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
# Add helpful snippets
|
| 94 |
+
st.markdown("#### π Quick Snippets")
|
| 95 |
+
snippets = {
|
| 96 |
+
"Title": "st.title('My Title')",
|
| 97 |
+
"Header": "st.header('My Header')",
|
| 98 |
+
"Colored Text": "st.markdown(':blue[Blue text]')",
|
| 99 |
+
"Bold & Italic": "st.markdown('**Bold** and *italic*')",
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
selected_snippet = st.selectbox("Insert a snippet:", list(snippets.keys()))
|
| 103 |
+
if st.button("Insert Snippet"):
|
| 104 |
+
if code:
|
| 105 |
+
code += "\n" + snippets[selected_snippet]
|
| 106 |
+
else:
|
| 107 |
+
code = snippets[selected_snippet]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
with col2:
|
| 110 |
+
st.markdown("### π― Live Output")
|
| 111 |
+
with st.container(border=True):
|
| 112 |
+
if code:
|
| 113 |
+
try:
|
| 114 |
+
exec(code)
|
| 115 |
+
except Exception as e:
|
| 116 |
+
st.error(f"Error: {str(e)}")
|
| 117 |
+
|
| 118 |
+
# Tips & Documentation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
with st.expander("π‘ Tips & Tricks"):
|
| 120 |
st.markdown("""
|
| 121 |
+
### Helpful Tips:
|
| 122 |
+
- Start with basic text elements (`st.write`, `st.markdown`)
|
| 123 |
+
- Use markdown for formatting (`**bold**`, `*italic*`)
|
| 124 |
+
- Try colored text with `:blue[text]`, `:red[text]`, etc.
|
| 125 |
+
- Experiment with different header levels
|
| 126 |
+
- Watch the live output as you type
|
| 127 |
""")
|
| 128 |
|
| 129 |
st.markdown("---")
|
| 130 |
+
st.caption("π Learn more at streamlit.io/docs")
|