File size: 4,561 Bytes
28e7fd6
b8523b7
76b1b72
b8523b7
1a353ec
28e7fd6
73d1f25
76b1b72
 
ee225de
73d1f25
b8523b7
73d1f25
 
05ed529
aee8936
db5acbd
 
 
 
 
 
aee8936
 
a8a25ed
 
05ed529
a8a25ed
 
 
 
 
 
 
 
0ba5b95
8c7f7b0
 
05ed529
 
 
 
 
 
 
a8a25ed
aee8936
8c7f7b0
 
 
 
76b1b72
ee225de
b8523b7
73d1f25
b8523b7
8c7f7b0
 
 
d22106a
b8523b7
 
ee225de
b8523b7
 
100b6c8
8c7f7b0
b8523b7
8c7f7b0
100b6c8
 
d48e8d9
 
73d1f25
100b6c8
d48e8d9
100b6c8
 
d48e8d9
100b6c8
 
d48e8d9
b8523b7
73d1f25
 
 
8c7f7b0
73d1f25
ee225de
 
d48e8d9
b8523b7
 
d48e8d9
b8523b7
 
28e7fd6
ee225de
b8523b7
 
100b6c8
b8523b7
 
ee225de
 
73d1f25
ab2413a
1a353ec
c61b525
ee225de
73d1f25
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import streamlit as st
import streamlit_ace as st_ace
from utils import execute_code
from chatbot import render_chatbot
from layout import init_session_state, apply_theme

# Page Setup
st.set_page_config(page_title="Pro Code Playground", page_icon="💻", layout="wide")
init_session_state()

# Header
st.title("Pro Code Playground")
st.markdown("Write, execute & export multi-language snippets, with built‑in AI assistance.")

# Define default code snippets
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}'''
}

# Language + Theme selection
lang_col, spacer, theme_col = st.columns([3, 6, 1])
with lang_col:
    selected_lang = st.selectbox("Language", list(DEFAULT_SNIPPETS.keys()), index=0)

with theme_col:
    theme_choice = st.radio("Theme", options=["☀️", "🌙"], horizontal=True, label_visibility="collapsed")
    st.session_state.dark_mode = (theme_choice == "🌙")

# Apply theme
colors, ace_theme = apply_theme()

# Initialize code for language
prev_lang = st.session_state.get("prev_language", None)
if (
    selected_lang != prev_lang
    or st.session_state.get("code") is None
    or st.session_state.code.strip() == ""
    or st.session_state.code.strip() == DEFAULT_SNIPPETS.get(prev_lang, "")
):
    st.session_state.code = DEFAULT_SNIPPETS[selected_lang]
st.session_state.prev_language = selected_lang

# Generate dynamic editor key so it refreshes
editor_key = f"editor_{selected_lang}"

# Layout: Editor + Chatbot
gen, bot = st.columns((2, 1), gap="large")
with gen:
    st.subheader("Editor")

    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  # key now varies with language
    )

    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)

with bot:
    st.subheader("Code Assistant")
    render_chatbot(
        st.session_state.code,
        st.session_state.get("code_output", ""),
        st.session_state.get("error_output", "")
    )

# Footer
st.markdown("""
<div style='text-align:center; margin-top:1rem; opacity:0.6;'>
  Built with ❤️ & Streamlit by Vaibhav
</div>
""", unsafe_allow_html=True)