Spaces:
Sleeping
Sleeping
adding default code
Browse files- code_editor.py +27 -8
code_editor.py
CHANGED
|
@@ -1,18 +1,34 @@
|
|
| 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):
|
| 7 |
st.subheader("Editor")
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
code = st_ace.st_ace(
|
| 17 |
value=st.session_state.code,
|
| 18 |
placeholder=f"Start typing your {selected_lang} code…",
|
|
@@ -30,6 +46,7 @@ def render_code_editor(selected_lang, ace_theme, editor_key):
|
|
| 30 |
if code != st.session_state.code:
|
| 31 |
st.session_state.code = code
|
| 32 |
|
|
|
|
| 33 |
user_input = st.text_area(
|
| 34 |
label="📥 Input (stdin)",
|
| 35 |
value=st.session_state.stdin,
|
|
@@ -40,6 +57,7 @@ def render_code_editor(selected_lang, ace_theme, editor_key):
|
|
| 40 |
if user_input != st.session_state.stdin:
|
| 41 |
st.session_state.stdin = user_input
|
| 42 |
|
|
|
|
| 43 |
if st.button("▶️ Run"):
|
| 44 |
out, err, exc = execute_code(
|
| 45 |
code=st.session_state.code,
|
|
@@ -49,6 +67,7 @@ def render_code_editor(selected_lang, ace_theme, editor_key):
|
|
| 49 |
st.session_state.code_output = out
|
| 50 |
st.session_state.error_output = err or exc
|
| 51 |
|
|
|
|
| 52 |
if st.session_state.get("code_output"):
|
| 53 |
st.text_area("Output", st.session_state.code_output, height=120)
|
| 54 |
|
|
|
|
| 1 |
# code_editor.py
|
| 2 |
+
|
| 3 |
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}''',
|
| 10 |
+
"C++": '''// default code\n#include <iostream>\nusing namespace std;\nint main() {\n int a, b;\n cin >> a >> b;\n cout << "Sum: " << a + b << endl;\n return 0;\n}''',
|
| 11 |
+
"Java": '''// default code\nimport java.util.Scanner;\npublic class Program {\n public static void main(String[] args) {\n Scanner sc = new Scanner(System.in);\n int a = sc.nextInt();\n int b = sc.nextInt();\n System.out.println("Sum: " + (a + b));\n }\n}''',
|
| 12 |
+
"JavaScript": '''// default code\nconst readline = require('readline');\nconst rl = readline.createInterface({ input: process.stdin, output: process.stdout });\nlet inputs = [];\nrl.on('line', (line) => {\n inputs.push(parseInt(line));\n if (inputs.length === 2) {\n console.log("Sum:", inputs[0] + inputs[1]);\n rl.close();\n }\n});''',
|
| 13 |
+
"C#": '''// default code\nusing System;\npublic class Program {\n public static void Main(string[] args) {\n int a = Convert.ToInt32(Console.ReadLine());\n int b = Convert.ToInt32(Console.ReadLine());\n Console.WriteLine("Sum: " + (a + b));\n }\n}'''
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
|
| 17 |
def render_code_editor(selected_lang, ace_theme, editor_key):
|
| 18 |
st.subheader("Editor")
|
| 19 |
+
|
| 20 |
+
# Initialize default code if needed
|
| 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 st.session_state.get("prev_language") != selected_lang
|
| 26 |
+
or st.session_state.code.strip() == DEFAULT_SNIPPETS.get(st.session_state.get("prev_language", ""), "")
|
| 27 |
+
):
|
| 28 |
+
st.session_state.code = default_code
|
| 29 |
+
st.session_state.prev_language = selected_lang
|
| 30 |
+
|
| 31 |
+
# Code Editor
|
| 32 |
code = st_ace.st_ace(
|
| 33 |
value=st.session_state.code,
|
| 34 |
placeholder=f"Start typing your {selected_lang} code…",
|
|
|
|
| 46 |
if code != st.session_state.code:
|
| 47 |
st.session_state.code = code
|
| 48 |
|
| 49 |
+
# Input box
|
| 50 |
user_input = st.text_area(
|
| 51 |
label="📥 Input (stdin)",
|
| 52 |
value=st.session_state.stdin,
|
|
|
|
| 57 |
if user_input != st.session_state.stdin:
|
| 58 |
st.session_state.stdin = user_input
|
| 59 |
|
| 60 |
+
# Run Button
|
| 61 |
if st.button("▶️ Run"):
|
| 62 |
out, err, exc = execute_code(
|
| 63 |
code=st.session_state.code,
|
|
|
|
| 67 |
st.session_state.code_output = out
|
| 68 |
st.session_state.error_output = err or exc
|
| 69 |
|
| 70 |
+
# Output Display
|
| 71 |
if st.session_state.get("code_output"):
|
| 72 |
st.text_area("Output", st.session_state.code_output, height=120)
|
| 73 |
|