Spaces:
Running
Running
Create code_editor.py
Browse files- code_editor.py +49 -0
code_editor.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# code_editor.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import streamlit_ace as st_ace
|
| 4 |
+
from utils import execute_code
|
| 5 |
+
|
| 6 |
+
def render_code_editor(selected_lang, ace_theme, editor_key, default_snippet):
|
| 7 |
+
st.subheader("Editor")
|
| 8 |
+
|
| 9 |
+
code = st_ace.st_ace(
|
| 10 |
+
value=st.session_state.code,
|
| 11 |
+
placeholder=f"Start typing your {selected_lang} code…",
|
| 12 |
+
language=selected_lang.lower() if selected_lang != "C++" else "c_cpp",
|
| 13 |
+
theme=ace_theme,
|
| 14 |
+
keybinding="vscode",
|
| 15 |
+
font_size=14,
|
| 16 |
+
min_lines=20,
|
| 17 |
+
show_gutter=True,
|
| 18 |
+
wrap=True,
|
| 19 |
+
auto_update=True,
|
| 20 |
+
key=editor_key
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
if code != st.session_state.code:
|
| 24 |
+
st.session_state.code = code
|
| 25 |
+
|
| 26 |
+
user_input = st.text_area(
|
| 27 |
+
label="📥 Input (stdin)",
|
| 28 |
+
value=st.session_state.stdin,
|
| 29 |
+
placeholder="Enter input() values here, one per line",
|
| 30 |
+
height=100,
|
| 31 |
+
key="stdin_input"
|
| 32 |
+
)
|
| 33 |
+
if user_input != st.session_state.stdin:
|
| 34 |
+
st.session_state.stdin = user_input
|
| 35 |
+
|
| 36 |
+
if st.button("▶️ Run"):
|
| 37 |
+
out, err, exc = execute_code(
|
| 38 |
+
code=st.session_state.code,
|
| 39 |
+
stdin=st.session_state.stdin,
|
| 40 |
+
language=selected_lang
|
| 41 |
+
)
|
| 42 |
+
st.session_state.code_output = out
|
| 43 |
+
st.session_state.error_output = err or exc
|
| 44 |
+
|
| 45 |
+
if st.session_state.get("code_output"):
|
| 46 |
+
st.text_area("Output", st.session_state.code_output, height=120)
|
| 47 |
+
|
| 48 |
+
if st.session_state.get("error_output"):
|
| 49 |
+
st.error(st.session_state.error_output)
|