Code_editor / app.py
vsj0702's picture
Add comment in default code
db5acbd verified
raw
history blame
4.56 kB
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)