Spaces:
Sleeping
Sleeping
Milind Kamat
commited on
Commit
Β·
d625b64
1
Parent(s):
ec93d45
2024 Dec 30: New start
Browse filesSigned-off-by: Milind Kamat <36366961+milindkamat0507@users.noreply.github.com>
app.py
CHANGED
|
@@ -1,156 +1,102 @@
|
|
| 1 |
-
|
| 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 |
-
|
| 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("###
|
| 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 |
-
#
|
| 82 |
-
|
| 83 |
-
"
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
# Quick Snippets with better alignment
|
| 90 |
-
with st.container(border=True):
|
| 91 |
-
st.markdown("#### π Quick Snippets")
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
def run(self):
|
| 156 |
with st.sidebar:
|
|
@@ -161,17 +107,8 @@ class StreamlitTutorial:
|
|
| 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.
|
| 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()
|
| 177 |
-
app.run()
|
|
|
|
| 1 |
+
def render_help_section(self, col):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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?**
|
| 9 |
+
- Basic building blocks for displaying text
|
| 10 |
+
- Range from titles to formatted text
|
| 11 |
+
- Support markdown formatting
|
| 12 |
+
""")
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
with st.expander("π‘ Best Practices"):
|
| 15 |
+
st.markdown("""
|
| 16 |
+
**Writing Tips:**
|
| 17 |
+
1. Use appropriate heading levels
|
| 18 |
+
2. Keep text concise and clear
|
| 19 |
+
3. Use formatting for emphasis
|
| 20 |
+
4. Add visual hierarchy with headers
|
| 21 |
+
|
| 22 |
+
**Code Structure:**
|
| 23 |
+
```python
|
| 24 |
+
st.title('Main Title')
|
| 25 |
+
st.header('Section Header')
|
| 26 |
+
st.subheader('Sub-section')
|
| 27 |
+
st.write('Regular text')
|
| 28 |
+
```
|
| 29 |
+
""")
|
| 30 |
+
|
| 31 |
+
with st.expander("π Examples & Use Cases"):
|
| 32 |
+
st.markdown("""
|
| 33 |
+
**Common Patterns:**
|
| 34 |
+
|
| 35 |
+
1. Page Structure
|
| 36 |
+
```python
|
| 37 |
+
st.title('Dashboard')
|
| 38 |
+
st.header('Sales Data')
|
| 39 |
+
st.subheader('Monthly Trends')
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
2. Formatted Text
|
| 43 |
+
```python
|
| 44 |
+
st.markdown('**Bold** and *italic*')
|
| 45 |
+
st.markdown(':blue[Colored text]')
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
3. Mixed Elements
|
| 49 |
+
```python
|
| 50 |
+
st.title('Report')
|
| 51 |
+
st.write('Regular text')
|
| 52 |
+
st.markdown('- Bullet point')
|
| 53 |
+
```
|
| 54 |
+
""")
|
| 55 |
+
|
| 56 |
+
with st.expander("β οΈ Common Mistakes"):
|
| 57 |
+
st.markdown("""
|
| 58 |
+
1. Skipping header levels
|
| 59 |
+
2. Overusing formatting
|
| 60 |
+
3. Inconsistent styling
|
| 61 |
+
4. Missing hierarchy
|
| 62 |
+
|
| 63 |
+
**How to Avoid:**
|
| 64 |
+
- Plan your page structure
|
| 65 |
+
- Use consistent formatting
|
| 66 |
+
- Test different screen sizes
|
| 67 |
+
- Keep it simple
|
| 68 |
+
""")
|
| 69 |
+
|
| 70 |
+
with st.expander("π Advanced Tips"):
|
| 71 |
+
st.markdown("""
|
| 72 |
+
**Advanced Formatting:**
|
| 73 |
+
```python
|
| 74 |
+
# Custom HTML
|
| 75 |
+
st.markdown('''
|
| 76 |
+
<span style='color:blue'>
|
| 77 |
+
Custom styled text
|
| 78 |
+
</span>
|
| 79 |
+
''', unsafe_allow_html=True)
|
| 80 |
+
|
| 81 |
+
# Complex Markdown
|
| 82 |
+
st.markdown('''
|
| 83 |
+
# Title
|
| 84 |
+
## Subtitle
|
| 85 |
+
* Point 1
|
| 86 |
+
* Subpoint
|
| 87 |
+
> Quote
|
| 88 |
+
''')
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
**Layout Combinations:**
|
| 92 |
+
```python
|
| 93 |
+
col1, col2 = st.columns(2)
|
| 94 |
+
with col1:
|
| 95 |
+
st.header('Column 1')
|
| 96 |
+
with col2:
|
| 97 |
+
st.header('Column 2')
|
| 98 |
+
```
|
| 99 |
+
""")
|
| 100 |
|
| 101 |
def run(self):
|
| 102 |
with st.sidebar:
|
|
|
|
| 107 |
"Data Display", "Charts & Plots", "Interactive Components"]
|
| 108 |
)
|
| 109 |
|
|
|
|
| 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]) # New help section in middle
|
| 114 |
+
self.render_playground(cols[2], dict(self.get_text_elements()))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|