Spaces:
Sleeping
Sleeping
Milind Kamat
commited on
Commit
·
3d60be0
1
Parent(s):
8baabec
2024 Dec 30 : New updates
Browse filesSigned-off-by: Milind Kamat <36366961+milindkamat0507@users.noreply.github.com>
app.py
CHANGED
|
@@ -346,96 +346,61 @@ class StreamlitTutorial:
|
|
| 346 |
|
| 347 |
|
| 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 |
-
|
| 357 |
-
|
| 358 |
-
|
| 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.
|
| 418 |
-
Displays code snippets with live output for each input widget.
|
| 419 |
-
Creates interactive learning environment for input elements.
|
| 420 |
-
|
| 421 |
-
Arguments:
|
| 422 |
-
col: Streamlit column object for content rendering
|
| 423 |
-
|
| 424 |
-
Returns:
|
| 425 |
-
None
|
| 426 |
-
"""
|
| 427 |
with col:
|
| 428 |
st.markdown("### 📝 Code Examples")
|
| 429 |
-
for title, code in self.get_input_elements():
|
| 430 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 431 |
st.markdown(f"**{title}**")
|
| 432 |
st.code(code)
|
| 433 |
st.markdown("Live output:")
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
|
|
|
|
|
|
| 439 |
|
| 440 |
|
| 441 |
def render_help_section(self, col: st.delta_generator.DeltaGenerator) -> None:
|
|
|
|
| 346 |
|
| 347 |
|
| 348 |
|
|
|
|
|
|
|
| 349 |
def get_input_elements(self) -> List[Tuple[str, str]]:
|
| 350 |
+
"""Cross-validated collection of input widgets with unique keys"""
|
|
|
|
|
|
|
| 351 |
return [
|
| 352 |
+
("Text Input", """st.text_input('Enter your name',
|
| 353 |
+
key='input_example_1_a',
|
| 354 |
+
placeholder='John Doe')"""),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
|
| 356 |
+
("Text Area", """st.text_area('Enter long text',
|
| 357 |
+
key='area_example_1_b',
|
| 358 |
+
height=100,
|
| 359 |
+
placeholder='Write something...')"""),
|
| 360 |
+
|
| 361 |
+
("Number Input", """st.number_input('Enter a number',
|
| 362 |
+
key='number_example_1_c',
|
| 363 |
+
min_value=0,
|
| 364 |
+
max_value=100,
|
| 365 |
+
value=50,
|
| 366 |
+
step=5)"""),
|
| 367 |
+
|
| 368 |
+
("Slider", """st.slider('Select value',
|
| 369 |
+
key='slider_example_1_d',
|
| 370 |
+
min_value=0,
|
| 371 |
+
max_value=100,
|
| 372 |
+
value=50,
|
| 373 |
+
step=5)"""),
|
| 374 |
+
|
| 375 |
+
("Select Box", """st.selectbox('Choose an option',
|
| 376 |
+
key='select_example_1_e',
|
| 377 |
+
options=['Option 1', 'Option 2', 'Option 3'],
|
| 378 |
+
index=0)""")
|
| 379 |
+
]
|
| 380 |
+
|
| 381 |
|
| 382 |
+
|
| 383 |
+
|
| 384 |
def render_input_elements(self, col: st.delta_generator.DeltaGenerator) -> None:
|
| 385 |
+
"""Cross-validated rendering method with proper key handling"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
with col:
|
| 387 |
st.markdown("### 📝 Code Examples")
|
| 388 |
+
for idx, (title, code) in enumerate(self.get_input_elements()):
|
| 389 |
+
# Create unique container key
|
| 390 |
+
example_container_key = f"example_container_{idx}"
|
| 391 |
+
output_container_key = f"output_container_{idx}"
|
| 392 |
+
|
| 393 |
+
with st.container(border=True, key=example_container_key):
|
| 394 |
st.markdown(f"**{title}**")
|
| 395 |
st.code(code)
|
| 396 |
st.markdown("Live output:")
|
| 397 |
+
|
| 398 |
+
with st.container(border=True, key=output_container_key):
|
| 399 |
+
try:
|
| 400 |
+
# Execute the code with unique keys
|
| 401 |
+
exec(code)
|
| 402 |
+
except Exception as e:
|
| 403 |
+
st.error(f"Error: {str(e)}")
|
| 404 |
|
| 405 |
|
| 406 |
def render_help_section(self, col: st.delta_generator.DeltaGenerator) -> None:
|