vsj0702 commited on
Commit
c217967
·
verified ·
1 Parent(s): aba525f

Segregating code_editor.py

Browse files
Files changed (1) hide show
  1. app.py +4 -70
app.py CHANGED
@@ -1,8 +1,7 @@
1
  import streamlit as st
2
- import streamlit_ace as st_ace
3
- from utils import execute_code
4
- from chatbot import render_chatbot
5
  from layout import init_session_state, apply_theme
 
 
6
 
7
  # Page Setup
8
  st.set_page_config(page_title="Pro Code Playground", page_icon="💻", layout="wide")
@@ -12,20 +11,10 @@ init_session_state()
12
  st.title("Pro Code Playground")
13
  st.markdown("Write, execute & export multi-language snippets, with built‑in AI assistance.")
14
 
15
- # Define default code snippets
16
- DEFAULT_SNIPPETS = {
17
- "Python": '''# default code\na = int(input())\nb = int(input())\nprint("Sum:", a + b)''',
18
- "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}''',
19
- "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}''',
20
- "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}''',
21
- "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});''',
22
- "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}'''
23
- }
24
-
25
  # Language + Theme selection
26
  lang_col, spacer, theme_col = st.columns([3, 6, 1])
27
  with lang_col:
28
- selected_lang = st.selectbox("Language", list(DEFAULT_SNIPPETS.keys()), index=0)
29
 
30
  with theme_col:
31
  theme_choice = st.radio("Theme", options=["☀️", "🌙"], horizontal=True, label_visibility="collapsed")
@@ -34,66 +23,11 @@ with theme_col:
34
  # Apply theme
35
  colors, ace_theme = apply_theme()
36
 
37
- # Initialize code for language
38
- prev_lang = st.session_state.get("prev_language", None)
39
- if (
40
- selected_lang != prev_lang
41
- or st.session_state.get("code") is None
42
- or st.session_state.code.strip() == ""
43
- or st.session_state.code.strip() == DEFAULT_SNIPPETS.get(prev_lang, "")
44
- ):
45
- st.session_state.code = DEFAULT_SNIPPETS[selected_lang]
46
- st.session_state.prev_language = selected_lang
47
-
48
- # Generate dynamic editor key so it refreshes
49
- editor_key = f"editor_{selected_lang}"
50
-
51
  # Layout: Editor + Chatbot
52
  gen, bot = st.columns((2, 1), gap="large")
53
  with gen:
54
  st.subheader("Editor")
55
-
56
- code = st_ace.st_ace(
57
- value=st.session_state.code,
58
- placeholder=f"Start typing your {selected_lang} code…",
59
- language=selected_lang.lower() if selected_lang != "C++" else "c_cpp",
60
- theme=ace_theme,
61
- keybinding="vscode",
62
- font_size=14,
63
- min_lines=20,
64
- show_gutter=True,
65
- wrap=True,
66
- auto_update=True,
67
- key=editor_key # key now varies with language
68
- )
69
-
70
- if code != st.session_state.code:
71
- st.session_state.code = code
72
-
73
- user_input = st.text_area(
74
- label="📥 Input (stdin)",
75
- value=st.session_state.stdin,
76
- placeholder="Enter input() values here, one per line",
77
- height=100,
78
- key="stdin_input"
79
- )
80
- if user_input != st.session_state.stdin:
81
- st.session_state.stdin = user_input
82
-
83
- if st.button("▶️ Run"):
84
- out, err, exc = execute_code(
85
- code=st.session_state.code,
86
- stdin=st.session_state.stdin,
87
- language=selected_lang
88
- )
89
- st.session_state.code_output = out
90
- st.session_state.error_output = err or exc
91
-
92
- if st.session_state.get("code_output"):
93
- st.text_area("Output", st.session_state.code_output, height=120)
94
-
95
- if st.session_state.get("error_output"):
96
- st.error(st.session_state.error_output)
97
 
98
  with bot:
99
  st.subheader("Code Assistant")
 
1
  import streamlit as st
 
 
 
2
  from layout import init_session_state, apply_theme
3
+ from code_editor import render_code_editor
4
+ from chatbot import render_chatbot
5
 
6
  # Page Setup
7
  st.set_page_config(page_title="Pro Code Playground", page_icon="💻", layout="wide")
 
11
  st.title("Pro Code Playground")
12
  st.markdown("Write, execute & export multi-language snippets, with built‑in AI assistance.")
13
 
 
 
 
 
 
 
 
 
 
 
14
  # Language + Theme selection
15
  lang_col, spacer, theme_col = st.columns([3, 6, 1])
16
  with lang_col:
17
+ selected_lang = st.selectbox("Language", ["Python", "C", "C++", "Java", "JavaScript", "C#"], index=0)
18
 
19
  with theme_col:
20
  theme_choice = st.radio("Theme", options=["☀️", "🌙"], horizontal=True, label_visibility="collapsed")
 
23
  # Apply theme
24
  colors, ace_theme = apply_theme()
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  # Layout: Editor + Chatbot
27
  gen, bot = st.columns((2, 1), gap="large")
28
  with gen:
29
  st.subheader("Editor")
30
+ render_code_editor(selected_lang, ace_theme)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  with bot:
33
  st.subheader("Code Assistant")