Milind Kamat commited on
Commit
fed9126
Β·
1 Parent(s): 7f69afa

2024 dec 30:fix error

Browse files

Signed-off-by: Milind Kamat <36366961+milindkamat0507@users.noreply.github.com>

Files changed (1) hide show
  1. app.py +37 -28
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
- Manages code input, snippets, and real-time output display.
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 with session state
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
- col1, col2 = st.columns([3, 1])
395
- with col1:
 
 
 
396
  selected_snippet = st.selectbox(
397
  "Choose snippet:",
398
  list(snippets.keys()),
399
- key="snippet_select"
400
  )
401
- with col2:
402
- if st.button("Add", key="add_snippet"):
403
- new_code = snippets[selected_snippet]
404
  if 'code_input' not in st.session_state:
405
- st.session_state.code_input = new_code
406
  else:
407
- st.session_state.code_input = st.session_state.code_input + "\n" + new_code
 
 
 
408
  st.rerun()
409
 
410
- # Control buttons
411
- run_col, reset_col, copy_col = st.columns(3)
412
 
413
- with run_col:
414
- run = st.button("▢️ Run", key="run_button")
415
- with reset_col:
416
- if st.button("πŸ”„ Reset", key="reset_button"):
 
 
 
 
 
 
417
  st.session_state.code_input = ""
418
  st.rerun()
419
- with copy_col:
420
- if st.button("πŸ’Ύ Copy", key="copy_button"):
 
421
  st.code(code_input)
422
 
423
  # Live output section
424
  st.markdown("#### 🎨 Live Output")
425
  with st.container(border=True):
426
- if run and code_input:
427
  try:
428
- # Create a new local namespace for execution
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 & Help
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