File size: 3,531 Bytes
aba525f
089452f
aba525f
 
 
 
9706206
089452f
 
 
 
 
 
 
 
 
 
28986a4
089452f
9706206
089452f
9706206
 
 
089452f
 
 
9706206
 
089452f
 
 
 
9706206
aba525f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9706206
aba525f
9706206
aba525f
 
9706206
aba525f
 
9706206
aba525f
 
 
9706206
aba525f
 
 
 
 
 
 
 
 
9706206
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# code_editor.py

import streamlit as st
import streamlit_ace as st_ace
from utils import execute_code

# Default templates for each language
DEFAULT_SNIPPETS = {
    "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}'''
}


def render_code_editor(selected_lang, ace_theme, editor_key):

    # Load the default code for the current language
    default_code = DEFAULT_SNIPPETS[selected_lang]
    previous_lang = st.session_state.get("prev_language")

    # Reset code when switching language or if empty
    if (
        st.session_state.get("code") is None
        or st.session_state.code.strip() == ""
        or selected_lang != previous_lang
        or st.session_state.code.strip() == DEFAULT_SNIPPETS.get(previous_lang, "")
    ):
        st.session_state.code = default_code
        st.session_state.prev_language = selected_lang

    # Code editor block
    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

    # Input area for stdin
    user_input = st.text_area(
        "📥 Input (stdin)",
        value=st.session_state.stdin,
        height=100,
        placeholder="Enter input() values here, one per line",
        key="stdin_input"
    )

    if user_input != st.session_state.stdin:
        st.session_state.stdin = user_input

    # Run button
    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

    # Output section
    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)