Spaces:
Sleeping
Sleeping
File size: 2,912 Bytes
aba525f 28986a4 aba525f 28986a4 aba525f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# code_editor.py
import streamlit as st
import streamlit_ace as st_ace
from utils import execute_code
def render_code_editor(selected_lang, ace_theme, editor_key):
st.subheader("Editor")
default_snippet = {
"Python": '''# default code\na = int(input())\nb = int(input())\nprint("Sum:", a + b)''',
"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}''',
"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}''',
"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}''',
"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});''',
"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}'''
}
code = st_ace.st_ace(
value=st.session_state.code,
placeholder=f"Start typing your {selected_lang} code…",
language=selected_lang.lower() if selected_lang != "C++" else "c_cpp",
theme=ace_theme,
keybinding="vscode",
font_size=14,
min_lines=20,
show_gutter=True,
wrap=True,
auto_update=True,
key=editor_key
)
if code != st.session_state.code:
st.session_state.code = code
user_input = st.text_area(
label="📥 Input (stdin)",
value=st.session_state.stdin,
placeholder="Enter input() values here, one per line",
height=100,
key="stdin_input"
)
if user_input != st.session_state.stdin:
st.session_state.stdin = user_input
if st.button("▶️ Run"):
out, err, exc = execute_code(
code=st.session_state.code,
stdin=st.session_state.stdin,
language=selected_lang
)
st.session_state.code_output = out
st.session_state.error_output = err or exc
if st.session_state.get("code_output"):
st.text_area("Output", st.session_state.code_output, height=120)
if st.session_state.get("error_output"):
st.error(st.session_state.error_output)
|