Spaces:
Sleeping
Sleeping
| # 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) | |