Spaces:
Sleeping
Sleeping
Milind Kamat
commited on
Commit
Β·
fed9126
1
Parent(s):
7f69afa
2024 dec 30:fix error
Browse filesSigned-off-by: Milind Kamat <36366961+milindkamat0507@users.noreply.github.com>
app.py
CHANGED
|
@@ -367,12 +367,10 @@ class StreamlitTutorial:
|
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
-
|
| 371 |
def render_playground(self, col: st.delta_generator.DeltaGenerator, snippets: Dict[str, str]) -> None:
|
| 372 |
"""
|
| 373 |
Renders interactive coding playground with live execution.
|
| 374 |
-
|
| 375 |
-
|
| 376 |
Arguments:
|
| 377 |
col: Streamlit column object for rendering
|
| 378 |
snippets: Dictionary of available code snippets
|
|
@@ -380,59 +378,69 @@ class StreamlitTutorial:
|
|
| 380 |
with col:
|
| 381 |
st.markdown("### π» Practice Playground")
|
| 382 |
|
| 383 |
-
# Code input area
|
| 384 |
code_input = st.text_area(
|
| 385 |
"Try Streamlit commands:",
|
| 386 |
key="playground_input",
|
| 387 |
-
height=200,
|
| 388 |
value=st.session_state.get('code_input', ''),
|
|
|
|
| 389 |
placeholder="Example:\nst.title('My Title')"
|
| 390 |
)
|
| 391 |
|
| 392 |
-
# Quick Snippets
|
| 393 |
st.markdown("#### Quick Snippets")
|
| 394 |
-
|
| 395 |
-
with
|
|
|
|
|
|
|
|
|
|
| 396 |
selected_snippet = st.selectbox(
|
| 397 |
"Choose snippet:",
|
| 398 |
list(snippets.keys()),
|
| 399 |
-
|
| 400 |
)
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
if 'code_input' not in st.session_state:
|
| 405 |
-
st.session_state.code_input =
|
| 406 |
else:
|
| 407 |
-
|
|
|
|
|
|
|
|
|
|
| 408 |
st.rerun()
|
| 409 |
|
| 410 |
-
# Control buttons
|
| 411 |
-
|
| 412 |
|
| 413 |
-
with
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
st.session_state.code_input = ""
|
| 418 |
st.rerun()
|
| 419 |
-
|
| 420 |
-
|
|
|
|
| 421 |
st.code(code_input)
|
| 422 |
|
| 423 |
# Live output section
|
| 424 |
st.markdown("#### π¨ Live Output")
|
| 425 |
with st.container(border=True):
|
| 426 |
-
if
|
| 427 |
try:
|
| 428 |
-
|
| 429 |
-
local_dict = {}
|
| 430 |
-
# Execute in the local namespace
|
| 431 |
-
exec(code_input, globals(), local_dict)
|
| 432 |
except Exception as e:
|
| 433 |
st.error(f"Error: {str(e)}")
|
| 434 |
|
| 435 |
-
# Tips
|
| 436 |
with st.expander("π‘ Tips & Help"):
|
| 437 |
st.markdown("""
|
| 438 |
**Quick Tips:**
|
|
@@ -441,6 +449,7 @@ class StreamlitTutorial:
|
|
| 441 |
- Click Run to see results
|
| 442 |
- Reset to clear all code
|
| 443 |
""")
|
|
|
|
| 444 |
|
| 445 |
|
| 446 |
|
|
|
|
| 367 |
|
| 368 |
|
| 369 |
|
|
|
|
| 370 |
def render_playground(self, col: st.delta_generator.DeltaGenerator, snippets: Dict[str, str]) -> None:
|
| 371 |
"""
|
| 372 |
Renders interactive coding playground with live execution.
|
| 373 |
+
|
|
|
|
| 374 |
Arguments:
|
| 375 |
col: Streamlit column object for rendering
|
| 376 |
snippets: Dictionary of available code snippets
|
|
|
|
| 378 |
with col:
|
| 379 |
st.markdown("### π» Practice Playground")
|
| 380 |
|
| 381 |
+
# Code input area
|
| 382 |
code_input = st.text_area(
|
| 383 |
"Try Streamlit commands:",
|
| 384 |
key="playground_input",
|
|
|
|
| 385 |
value=st.session_state.get('code_input', ''),
|
| 386 |
+
height=200,
|
| 387 |
placeholder="Example:\nst.title('My Title')"
|
| 388 |
)
|
| 389 |
|
| 390 |
+
# Quick Snippets section
|
| 391 |
st.markdown("#### Quick Snippets")
|
| 392 |
+
|
| 393 |
+
# Split into two columns with better ratio for button alignment
|
| 394 |
+
snippet_col, button_col = st.columns([4, 1])
|
| 395 |
+
|
| 396 |
+
with snippet_col:
|
| 397 |
selected_snippet = st.selectbox(
|
| 398 |
"Choose snippet:",
|
| 399 |
list(snippets.keys()),
|
| 400 |
+
label_visibility="collapsed"
|
| 401 |
)
|
| 402 |
+
|
| 403 |
+
with button_col:
|
| 404 |
+
if st.button("Add", type="primary", use_container_width=True):
|
| 405 |
if 'code_input' not in st.session_state:
|
| 406 |
+
st.session_state.code_input = snippets[selected_snippet]
|
| 407 |
else:
|
| 408 |
+
# Add new line only if there's existing code
|
| 409 |
+
existing_code = st.session_state.code_input.strip()
|
| 410 |
+
new_code = snippets[selected_snippet]
|
| 411 |
+
st.session_state.code_input = f"{existing_code}\n{new_code}" if existing_code else new_code
|
| 412 |
st.rerun()
|
| 413 |
|
| 414 |
+
# Control buttons in equal columns
|
| 415 |
+
col1, col2, col3 = st.columns(3)
|
| 416 |
|
| 417 |
+
with col1:
|
| 418 |
+
if st.button("βΆοΈ Run", use_container_width=True):
|
| 419 |
+
try:
|
| 420 |
+
if code_input.strip():
|
| 421 |
+
exec(code_input)
|
| 422 |
+
except Exception as e:
|
| 423 |
+
st.error(f"Error: {str(e)}")
|
| 424 |
+
|
| 425 |
+
with col2:
|
| 426 |
+
if st.button("π Reset", use_container_width=True):
|
| 427 |
st.session_state.code_input = ""
|
| 428 |
st.rerun()
|
| 429 |
+
|
| 430 |
+
with col3:
|
| 431 |
+
if st.button("πΎ Copy", use_container_width=True):
|
| 432 |
st.code(code_input)
|
| 433 |
|
| 434 |
# Live output section
|
| 435 |
st.markdown("#### π¨ Live Output")
|
| 436 |
with st.container(border=True):
|
| 437 |
+
if code_input.strip():
|
| 438 |
try:
|
| 439 |
+
exec(code_input)
|
|
|
|
|
|
|
|
|
|
| 440 |
except Exception as e:
|
| 441 |
st.error(f"Error: {str(e)}")
|
| 442 |
|
| 443 |
+
# Tips section
|
| 444 |
with st.expander("π‘ Tips & Help"):
|
| 445 |
st.markdown("""
|
| 446 |
**Quick Tips:**
|
|
|
|
| 449 |
- Click Run to see results
|
| 450 |
- Reset to clear all code
|
| 451 |
""")
|
| 452 |
+
|
| 453 |
|
| 454 |
|
| 455 |
|