Spaces:
Sleeping
Sleeping
Milind Kamat
commited on
Commit
Β·
8baabec
1
Parent(s):
fed9126
2024 Dec 30:new methods
Browse filesSigned-off-by: Milind Kamat <36366961+milindkamat0507@users.noreply.github.com>
app.py
CHANGED
|
@@ -182,8 +182,71 @@ class StreamlitTutorial:
|
|
| 182 |
"""
|
| 183 |
|
| 184 |
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
|
| 189 |
def _get_advanced_tips_content(self) -> str:
|
|
@@ -253,24 +316,7 @@ class StreamlitTutorial:
|
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
-
|
| 257 |
-
"""
|
| 258 |
-
Provides collection of input widget examples with corresponding code.
|
| 259 |
-
Defines various input types and interactive elements.
|
| 260 |
-
Used for demonstrating user input capabilities.
|
| 261 |
-
|
| 262 |
-
Returns:
|
| 263 |
-
List[Tuple[str, str]]: List of (widget name, code snippet) pairs
|
| 264 |
-
"""
|
| 265 |
-
return [
|
| 266 |
-
("Text Input", "name = st.text_input('Enter your name')"),
|
| 267 |
-
("Number Input", "num = st.number_input('Enter a number', min_value=0, max_value=100)"),
|
| 268 |
-
("Slider", "value = st.slider('Select value', 0, 100, 50)"),
|
| 269 |
-
("Selectbox", "option = st.selectbox('Choose option', ['A', 'B', 'C'])"),
|
| 270 |
-
("Checkbox", "checked = st.checkbox('Enable feature')")
|
| 271 |
-
]
|
| 272 |
-
|
| 273 |
-
|
| 274 |
|
| 275 |
|
| 276 |
|
|
@@ -302,6 +348,70 @@ class StreamlitTutorial:
|
|
| 302 |
|
| 303 |
|
| 304 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
def render_input_elements(self, col: st.delta_generator.DeltaGenerator) -> None:
|
| 306 |
"""
|
| 307 |
Renders input widget examples in the specified column.
|
|
@@ -451,9 +561,47 @@ class StreamlitTutorial:
|
|
| 451 |
""")
|
| 452 |
|
| 453 |
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
|
| 458 |
|
| 459 |
def get_topic_tips(self, topic: str) -> str:
|
|
|
|
| 182 |
"""
|
| 183 |
|
| 184 |
|
| 185 |
+
def _get_widgets_help_content(self) -> str:
|
| 186 |
+
"""
|
| 187 |
+
Enhanced help content for widgets section
|
| 188 |
+
"""
|
| 189 |
+
return """
|
| 190 |
+
**Widget Best Practices:**
|
| 191 |
+
1. Input Validation
|
| 192 |
+
```python
|
| 193 |
+
# Add validation to inputs
|
| 194 |
+
age = st.number_input('Age', min_value=0, max_value=150, key='age_1')
|
| 195 |
+
if age < 18:
|
| 196 |
+
st.warning('Must be 18 or older')
|
| 197 |
+
```
|
| 198 |
+
|
| 199 |
+
2. Default Values
|
| 200 |
+
```python
|
| 201 |
+
# Provide sensible defaults
|
| 202 |
+
st.text_input('Name', value='Guest User', key='name_2')
|
| 203 |
+
st.slider('Rating', 1, 5, value=3, key='rating_1')
|
| 204 |
+
```
|
| 205 |
+
|
| 206 |
+
3. Responsive Widgets
|
| 207 |
+
```python
|
| 208 |
+
# React to widget changes
|
| 209 |
+
if st.checkbox('Show advanced options', key='advanced_1'):
|
| 210 |
+
st.number_input('Threshold', key='threshold_1')
|
| 211 |
+
st.slider('Sensitivity', key='sensitivity_1')
|
| 212 |
+
```
|
| 213 |
+
|
| 214 |
+
4. Form Submission
|
| 215 |
+
```python
|
| 216 |
+
with st.form('my_form'):
|
| 217 |
+
st.text_input('Username', key='form_user')
|
| 218 |
+
st.text_input('Password', type='password', key='form_pass')
|
| 219 |
+
submitted = st.form_submit_button('Login')
|
| 220 |
+
```
|
| 221 |
+
|
| 222 |
+
5. File Handling
|
| 223 |
+
```python
|
| 224 |
+
file = st.file_uploader('Upload CSV', key='file_2')
|
| 225 |
+
if file is not None:
|
| 226 |
+
# Handle file upload
|
| 227 |
+
st.success('File uploaded!')
|
| 228 |
+
```
|
| 229 |
+
|
| 230 |
+
6. Interactive Filters
|
| 231 |
+
```python
|
| 232 |
+
col1, col2 = st.columns(2)
|
| 233 |
+
with col1:
|
| 234 |
+
category = st.selectbox('Category', ['A', 'B', 'C'], key='cat_1')
|
| 235 |
+
with col2:
|
| 236 |
+
subcategory = st.multiselect('Subcategory', ['X', 'Y', 'Z'], key='subcat_1')
|
| 237 |
+
```
|
| 238 |
+
|
| 239 |
+
**Tips for Widget Usage:**
|
| 240 |
+
- Use clear, concise labels
|
| 241 |
+
- Group related widgets together
|
| 242 |
+
- Provide help text when needed
|
| 243 |
+
- Use appropriate widget types
|
| 244 |
+
- Handle all possible states
|
| 245 |
+
- Validate inputs properly
|
| 246 |
+
- Always use unique keys
|
| 247 |
+
- Consider mobile responsiveness
|
| 248 |
+
"""
|
| 249 |
+
|
| 250 |
|
| 251 |
|
| 252 |
def _get_advanced_tips_content(self) -> str:
|
|
|
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
|
| 322 |
|
|
|
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
+
def get_input_elements(self) -> List[Tuple[str, str]]:
|
| 352 |
+
"""
|
| 353 |
+
Enhanced collection of input widget examples with unique keys for each widget
|
| 354 |
+
"""
|
| 355 |
+
return [
|
| 356 |
+
("Text Input", """name = st.text_input('Enter your name',
|
| 357 |
+
placeholder='John Doe',
|
| 358 |
+
key='name_input_1')"""),
|
| 359 |
+
|
| 360 |
+
("Text Area", """text = st.text_area('Enter long text',
|
| 361 |
+
height=100,
|
| 362 |
+
placeholder='Write something...',
|
| 363 |
+
key='text_area_1')"""),
|
| 364 |
+
|
| 365 |
+
("Number Input", """num = st.number_input('Enter a number',
|
| 366 |
+
min_value=0,
|
| 367 |
+
max_value=100,
|
| 368 |
+
value=50,
|
| 369 |
+
step=5,
|
| 370 |
+
key='num_input_1')"""),
|
| 371 |
+
|
| 372 |
+
("Slider", """value = st.slider('Select range',
|
| 373 |
+
min_value=0,
|
| 374 |
+
max_value=100,
|
| 375 |
+
value=(25, 75),
|
| 376 |
+
step=5,
|
| 377 |
+
key='slider_1')"""),
|
| 378 |
+
|
| 379 |
+
("Select Box", """option = st.selectbox('Choose an option',
|
| 380 |
+
options=['Option 1', 'Option 2', 'Option 3'],
|
| 381 |
+
index=0,
|
| 382 |
+
key='select_1')"""),
|
| 383 |
+
|
| 384 |
+
("Multi Select", """options = st.multiselect('Select multiple',
|
| 385 |
+
options=['A', 'B', 'C', 'D'],
|
| 386 |
+
default=['A', 'B'],
|
| 387 |
+
key='multiselect_1')"""),
|
| 388 |
+
|
| 389 |
+
("Checkbox", """if st.checkbox('Enable feature',
|
| 390 |
+
value=False,
|
| 391 |
+
key='check_1'):
|
| 392 |
+
st.write('Feature enabled!')"""),
|
| 393 |
+
|
| 394 |
+
("Radio Button", """choice = st.radio('Pick one',
|
| 395 |
+
options=['First', 'Second', 'Third'],
|
| 396 |
+
horizontal=True,
|
| 397 |
+
key='radio_1')"""),
|
| 398 |
+
|
| 399 |
+
("Date Input", """date = st.date_input('Select date',
|
| 400 |
+
key='date_1')"""),
|
| 401 |
+
|
| 402 |
+
("Time Input", """time = st.time_input('Select time',
|
| 403 |
+
key='time_1')"""),
|
| 404 |
+
|
| 405 |
+
("Color Picker", """color = st.color_picker('Pick a color',
|
| 406 |
+
'#00EEFF',
|
| 407 |
+
key='color_1')"""),
|
| 408 |
+
|
| 409 |
+
("File Uploader", """uploaded_file = st.file_uploader('Choose a file',
|
| 410 |
+
type=['csv', 'txt', 'pdf'],
|
| 411 |
+
key='file_1')""")
|
| 412 |
+
]
|
| 413 |
+
|
| 414 |
+
|
| 415 |
def render_input_elements(self, col: st.delta_generator.DeltaGenerator) -> None:
|
| 416 |
"""
|
| 417 |
Renders input widget examples in the specified column.
|
|
|
|
| 561 |
""")
|
| 562 |
|
| 563 |
|
| 564 |
+
def render_widget_help(self, col: st.delta_generator.DeltaGenerator) -> None:
|
| 565 |
+
"""
|
| 566 |
+
Renders comprehensive widget help content
|
| 567 |
+
"""
|
| 568 |
+
with col:
|
| 569 |
+
st.markdown("### π Widget Guide")
|
| 570 |
+
|
| 571 |
+
with st.expander("π― Basic Concepts", expanded=True):
|
| 572 |
+
st.markdown("""
|
| 573 |
+
**Widget Types:**
|
| 574 |
+
- Input widgets (text, numbers, dates)
|
| 575 |
+
- Selection widgets (dropdown, checkbox, radio)
|
| 576 |
+
- File widgets (uploaders, downloads)
|
| 577 |
+
- Display widgets (progress, status)
|
| 578 |
+
|
| 579 |
+
**Key Features:**
|
| 580 |
+
- Real-time updates
|
| 581 |
+
- State management
|
| 582 |
+
- Input validation
|
| 583 |
+
- Responsive layout
|
| 584 |
+
- Form handling
|
| 585 |
+
- Unique widget keys
|
| 586 |
+
""")
|
| 587 |
+
|
| 588 |
+
with st.expander("π‘ Best Practices"):
|
| 589 |
+
st.markdown(self._get_widgets_help_content())
|
| 590 |
+
|
| 591 |
+
with st.expander("π Examples"):
|
| 592 |
+
st.code("""
|
| 593 |
+
# Basic form example
|
| 594 |
+
with st.form('contact'):
|
| 595 |
+
name = st.text_input('Name', key='contact_name')
|
| 596 |
+
email = st.text_input('Email', key='contact_email')
|
| 597 |
+
message = st.text_area('Message', key='contact_message')
|
| 598 |
+
submit = st.form_submit_button('Send')
|
| 599 |
+
|
| 600 |
+
if submit:
|
| 601 |
+
st.success('Message sent!')
|
| 602 |
+
""")
|
| 603 |
+
|
| 604 |
+
|
| 605 |
|
| 606 |
|
| 607 |
def get_topic_tips(self, topic: str) -> str:
|