vsj0702 commited on
Commit
ade0c56
·
verified ·
1 Parent(s): fc252a3

Adding input button.

Browse files
Files changed (1) hide show
  1. app.py +45 -52
app.py CHANGED
@@ -1,8 +1,7 @@
1
  import streamlit as st
2
  import streamlit_ace as st_ace
3
- from utils import execute_code, export_session
4
  from chatbot import render_chatbot
5
- import json
6
 
7
  # 1️⃣ Page configuration (must be first)
8
  st.set_page_config(
@@ -12,11 +11,7 @@ st.set_page_config(
12
  )
13
 
14
  # 2️⃣ Dark mode toggle
15
- if 'dark_mode' not in st.session_state:
16
- st.session_state.dark_mode = True
17
-
18
- dm = st.checkbox("🌙 Dark mode", value=st.session_state.dark_mode)
19
- st.session_state.dark_mode = dm
20
 
21
  # 3️⃣ Theme variables
22
  BG = "#0f1620" if dm else "#f5f5f5"
@@ -43,13 +38,7 @@ st.markdown(f"""
43
  border: 1px solid {BORDER} !important;
44
  border-radius: 4px !important;
45
  }}
46
- button, .stDownloadButton > button {{
47
- background-color: {ACCENT} !important;
48
- color: #fff !important;
49
- border-radius: 6px !important;
50
- transition: transform 0.1s;
51
- }}
52
- button:hover {{ transform: scale(1.02) !important; }}
53
  .chat-container {{ background: {PANEL_BG} !important; border: 1px solid {BORDER} !important; border-radius: 8px !important; padding: 1rem; max-height: 480px; overflow-y: auto; }}
54
  .chat-message {{ margin-bottom: 1rem; padding: 0.75rem 1rem; border-radius: 12px; }}
55
  .user-message {{ background: rgba(100,149,237,0.2); align-self: flex-end; }}
@@ -60,64 +49,68 @@ st.markdown(f"""
60
 
61
  # 5️⃣ App header
62
  st.title("Pro Code Playground")
63
- st.markdown("Write, execute & export Python snippets, with built‑in AI assistance.")
64
 
65
- # 6️⃣ Main layout
66
- gen, bot = st.columns((2,1), gap="large")
67
 
68
- with gen:
69
  st.subheader("Editor")
70
  code = st_ace.st_ace(
71
- placeholder="Start typing your Python code…",
72
  language="python",
73
  theme=ACE_THEME,
74
  keybinding="vscode",
75
  font_size=14,
76
- min_lines=20,
77
  show_gutter=True,
78
  wrap=True,
79
  auto_update=True
80
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  if st.button("▶️ Run"):
82
- out, err, exc = execute_code(code)
83
- st.session_state.code_output = out
84
- st.session_state.error_output = err or exc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  if st.session_state.get("code_output"):
86
  st.text_area("Output", st.session_state.code_output, height=120)
87
  if st.session_state.get("error_output"):
88
  st.error(st.session_state.error_output)
89
 
90
- with bot:
91
  st.subheader("Code Assistant")
92
  render_chatbot(
93
  code,
94
  st.session_state.get("code_output", ""),
95
  st.session_state.get("error_output", "")
96
- )
97
-
98
- # 7️⃣ Export section
99
- st.markdown("---")
100
- st.subheader("Export")
101
- export_data = export_session(
102
- code,
103
- st.session_state.get("code_output", ""),
104
- st.session_state.get("error_output", "")
105
- )
106
- col1, col2 = st.columns(2)
107
- with col1:
108
- st.download_button(
109
- "📦 JSON",
110
- data=json.dumps(export_data, indent=2),
111
- file_name="session.json",
112
- mime="application/json"
113
- )
114
- with col2:
115
- txt = f"# Code\n{code}\n\n# Output\n{st.session_state.get('code_output','')}\n\n# Errors\n{st.session_state.get('error_output','')}"
116
- st.download_button("📝 Text", txt, "session.txt", "text/plain")
117
-
118
- # 8️⃣ Footer
119
- st.markdown(f"""
120
- <div style='text-align:center; margin-top:1rem; color:{TEXT}66;'>
121
- Built with ❤️ & Streamlit
122
- </div>
123
- """, unsafe_allow_html=True)
 
1
  import streamlit as st
2
  import streamlit_ace as st_ace
3
+ from utils import execute_code
4
  from chatbot import render_chatbot
 
5
 
6
  # 1️⃣ Page configuration (must be first)
7
  st.set_page_config(
 
11
  )
12
 
13
  # 2️⃣ Dark mode toggle
14
+ dm = st.sidebar.checkbox("🌙 Dark mode", value=False)
 
 
 
 
15
 
16
  # 3️⃣ Theme variables
17
  BG = "#0f1620" if dm else "#f5f5f5"
 
38
  border: 1px solid {BORDER} !important;
39
  border-radius: 4px !important;
40
  }}
41
+ button {{ background-color: {ACCENT} !important; color: #fff !important; border-radius: 6px !important; }}
 
 
 
 
 
 
42
  .chat-container {{ background: {PANEL_BG} !important; border: 1px solid {BORDER} !important; border-radius: 8px !important; padding: 1rem; max-height: 480px; overflow-y: auto; }}
43
  .chat-message {{ margin-bottom: 1rem; padding: 0.75rem 1rem; border-radius: 12px; }}
44
  .user-message {{ background: rgba(100,149,237,0.2); align-self: flex-end; }}
 
49
 
50
  # 5️⃣ App header
51
  st.title("Pro Code Playground")
52
+ st.markdown("Write, execute Python snippets, with built‑in AI assistance.")
53
 
54
+ # 6️⃣ Layout: Editor + Assistant
55
+ col1, col2 = st.columns([2, 1], gap="large")
56
 
57
+ with col1:
58
  st.subheader("Editor")
59
  code = st_ace.st_ace(
60
+ placeholder="Type your Python code…",
61
  language="python",
62
  theme=ACE_THEME,
63
  keybinding="vscode",
64
  font_size=14,
65
+ min_lines=15,
66
  show_gutter=True,
67
  wrap=True,
68
  auto_update=True
69
  )
70
+
71
+ # 7️⃣ Detect input() calls and collect values
72
+ input_count = code.count("input(")
73
+ inputs = []
74
+ if input_count > 0:
75
+ st.info(f"Your code expects {input_count} input() call(s). Please provide values:")
76
+ for i in range(input_count):
77
+ val = st.text_input(f"Input value #{i+1}", key=f"in_{i}", help="Enter value for input() call")
78
+ inputs.append(val)
79
+ if not all(val.strip() for val in inputs):
80
+ st.warning("Please fill all input fields before running the code.")
81
+
82
+ # 8️⃣ Run code
83
  if st.button("▶️ Run"):
84
+ if input_count > 0 and not all(val.strip() for val in inputs):
85
+ st.error("Cannot run: missing input values.")
86
+ else:
87
+ exec_code = code
88
+ if input_count > 0:
89
+ # Override input() to return provided values in order
90
+ stub = (
91
+ "import builtins
92
+ "
93
+ f"vals = iter([{', '.join(repr(v) for v in inputs)}])
94
+ "
95
+ "builtins.input = lambda prompt='': next(vals)
96
+ "
97
+ )
98
+ exec_code = stub + exec_code
99
+
100
+ out, err, exc = execute_code(exec_code)
101
+ st.session_state.code_output = out
102
+ st.session_state.error_output = err or exc
103
+
104
+ # 9️⃣ Display output and errors
105
  if st.session_state.get("code_output"):
106
  st.text_area("Output", st.session_state.code_output, height=120)
107
  if st.session_state.get("error_output"):
108
  st.error(st.session_state.error_output)
109
 
110
+ with col2:
111
  st.subheader("Code Assistant")
112
  render_chatbot(
113
  code,
114
  st.session_state.get("code_output", ""),
115
  st.session_state.get("error_output", "")
116
+ )