Spaces:
Sleeping
Sleeping
Updating
Browse files- code_editor.py +14 -9
code_editor.py
CHANGED
|
@@ -4,6 +4,7 @@ import streamlit as st
|
|
| 4 |
import streamlit_ace as st_ace
|
| 5 |
from utils import execute_code
|
| 6 |
|
|
|
|
| 7 |
DEFAULT_SNIPPETS = {
|
| 8 |
"Python": '''# default code\na = int(input())\nb = int(input())\nprint("Sum:", a + b)''',
|
| 9 |
"C": '''// default code\n#include <stdio.h>\nint main() {\n int a, b;\n scanf("%d %d", &a, &b);\n printf("Sum: %d\\n", a + b);\n return 0;\n}''',
|
|
@@ -17,18 +18,21 @@ DEFAULT_SNIPPETS = {
|
|
| 17 |
def render_code_editor(selected_lang, ace_theme, editor_key):
|
| 18 |
st.subheader("Editor")
|
| 19 |
|
| 20 |
-
#
|
| 21 |
default_code = DEFAULT_SNIPPETS[selected_lang]
|
|
|
|
|
|
|
|
|
|
| 22 |
if (
|
| 23 |
st.session_state.get("code") is None
|
| 24 |
or st.session_state.code.strip() == ""
|
| 25 |
-
or
|
| 26 |
-
or st.session_state.code.strip() == DEFAULT_SNIPPETS.get(
|
| 27 |
):
|
| 28 |
st.session_state.code = default_code
|
| 29 |
st.session_state.prev_language = selected_lang
|
| 30 |
|
| 31 |
-
# Code
|
| 32 |
code = st_ace.st_ace(
|
| 33 |
value=st.session_state.code,
|
| 34 |
placeholder=f"Start typing your {selected_lang} code…",
|
|
@@ -46,18 +50,19 @@ def render_code_editor(selected_lang, ace_theme, editor_key):
|
|
| 46 |
if code != st.session_state.code:
|
| 47 |
st.session_state.code = code
|
| 48 |
|
| 49 |
-
# Input
|
| 50 |
user_input = st.text_area(
|
| 51 |
-
|
| 52 |
value=st.session_state.stdin,
|
| 53 |
-
placeholder="Enter input() values here, one per line",
|
| 54 |
height=100,
|
|
|
|
| 55 |
key="stdin_input"
|
| 56 |
)
|
|
|
|
| 57 |
if user_input != st.session_state.stdin:
|
| 58 |
st.session_state.stdin = user_input
|
| 59 |
|
| 60 |
-
# Run
|
| 61 |
if st.button("▶️ Run"):
|
| 62 |
out, err, exc = execute_code(
|
| 63 |
code=st.session_state.code,
|
|
@@ -67,7 +72,7 @@ def render_code_editor(selected_lang, ace_theme, editor_key):
|
|
| 67 |
st.session_state.code_output = out
|
| 68 |
st.session_state.error_output = err or exc
|
| 69 |
|
| 70 |
-
# Output
|
| 71 |
if st.session_state.get("code_output"):
|
| 72 |
st.text_area("Output", st.session_state.code_output, height=120)
|
| 73 |
|
|
|
|
| 4 |
import streamlit_ace as st_ace
|
| 5 |
from utils import execute_code
|
| 6 |
|
| 7 |
+
# Default templates for each language
|
| 8 |
DEFAULT_SNIPPETS = {
|
| 9 |
"Python": '''# default code\na = int(input())\nb = int(input())\nprint("Sum:", a + b)''',
|
| 10 |
"C": '''// default code\n#include <stdio.h>\nint main() {\n int a, b;\n scanf("%d %d", &a, &b);\n printf("Sum: %d\\n", a + b);\n return 0;\n}''',
|
|
|
|
| 18 |
def render_code_editor(selected_lang, ace_theme, editor_key):
|
| 19 |
st.subheader("Editor")
|
| 20 |
|
| 21 |
+
# Load the default code for the current language
|
| 22 |
default_code = DEFAULT_SNIPPETS[selected_lang]
|
| 23 |
+
previous_lang = st.session_state.get("prev_language")
|
| 24 |
+
|
| 25 |
+
# Reset code when switching language or if empty
|
| 26 |
if (
|
| 27 |
st.session_state.get("code") is None
|
| 28 |
or st.session_state.code.strip() == ""
|
| 29 |
+
or selected_lang != previous_lang
|
| 30 |
+
or st.session_state.code.strip() == DEFAULT_SNIPPETS.get(previous_lang, "")
|
| 31 |
):
|
| 32 |
st.session_state.code = default_code
|
| 33 |
st.session_state.prev_language = selected_lang
|
| 34 |
|
| 35 |
+
# Code editor block
|
| 36 |
code = st_ace.st_ace(
|
| 37 |
value=st.session_state.code,
|
| 38 |
placeholder=f"Start typing your {selected_lang} code…",
|
|
|
|
| 50 |
if code != st.session_state.code:
|
| 51 |
st.session_state.code = code
|
| 52 |
|
| 53 |
+
# Input area for stdin
|
| 54 |
user_input = st.text_area(
|
| 55 |
+
"📥 Input (stdin)",
|
| 56 |
value=st.session_state.stdin,
|
|
|
|
| 57 |
height=100,
|
| 58 |
+
placeholder="Enter input() values here, one per line",
|
| 59 |
key="stdin_input"
|
| 60 |
)
|
| 61 |
+
|
| 62 |
if user_input != st.session_state.stdin:
|
| 63 |
st.session_state.stdin = user_input
|
| 64 |
|
| 65 |
+
# Run button
|
| 66 |
if st.button("▶️ Run"):
|
| 67 |
out, err, exc = execute_code(
|
| 68 |
code=st.session_state.code,
|
|
|
|
| 72 |
st.session_state.code_output = out
|
| 73 |
st.session_state.error_output = err or exc
|
| 74 |
|
| 75 |
+
# Output section
|
| 76 |
if st.session_state.get("code_output"):
|
| 77 |
st.text_area("Output", st.session_state.code_output, height=120)
|
| 78 |
|