vsj0702 commited on
Commit
0e0ba0f
Β·
verified Β·
1 Parent(s): 76a0fe4

Adding lots of things

Browse files
Files changed (1) hide show
  1. code_editor.py +34 -20
code_editor.py CHANGED
@@ -1,11 +1,11 @@
1
- # code_editor.py
2
-
3
  import streamlit as st
4
  import streamlit_ace as st_ace
5
  import os
 
6
  from utils import execute_code
 
7
 
8
- # Default templates for each language
9
  DEFAULT_SNIPPETS = {
10
  "Python": '''# default code\na = int(input())\nb = int(input())\nprint("Sum:", a + b)''',
11
  "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}''',
@@ -25,18 +25,31 @@ EXT_LANG_MAP = {
25
  }
26
 
27
  def render_code_editor(selected_lang, ace_theme, editor_key):
28
- uploaded_file = st.file_uploader("πŸ“€ Upload code file", type=list([e[1:] for e in EXT_LANG_MAP.keys()]))
 
29
 
30
  if uploaded_file:
31
- code_str = uploaded_file.read().decode("utf-8")
32
- _, ext = os.path.splitext(uploaded_file.name)
33
- detected_lang = EXT_LANG_MAP.get(ext.lower())
34
- if detected_lang:
35
- st.session_state.language = detected_lang
36
- selected_lang = detected_lang
37
- st.toast(f"βœ… Language auto-detected: {detected_lang}", icon="πŸ”„")
38
- st.session_state.code = code_str
39
-
 
 
 
 
 
 
 
 
 
 
 
 
40
  default_code = DEFAULT_SNIPPETS[selected_lang]
41
  previous_lang = st.session_state.get("prev_language")
42
 
@@ -49,6 +62,7 @@ def render_code_editor(selected_lang, ace_theme, editor_key):
49
  st.session_state.code = default_code
50
  st.session_state.prev_language = selected_lang
51
 
 
52
  code = st_ace.st_ace(
53
  value=st.session_state.code,
54
  placeholder=f"Start typing your {selected_lang} code…",
@@ -66,19 +80,19 @@ def render_code_editor(selected_lang, ace_theme, editor_key):
66
  if code != st.session_state.code:
67
  st.session_state.code = code
68
 
 
69
  user_input = st.text_area(
70
  "πŸ“₯ Input (stdin)",
71
  value=st.session_state.stdin,
72
  height=100,
73
- placeholder="Enter input() values here, one per line",
74
  key="stdin_input"
75
  )
76
-
77
  if user_input != st.session_state.stdin:
78
  st.session_state.stdin = user_input
79
 
 
80
  if st.button("▢️ Run"):
81
- import time, psutil
82
  start_time = time.perf_counter()
83
  process = psutil.Process()
84
  mem_before = process.memory_info().rss
@@ -91,19 +105,20 @@ def render_code_editor(selected_lang, ace_theme, editor_key):
91
 
92
  exec_time = time.perf_counter() - start_time
93
  mem_after = process.memory_info().rss
94
- mem_used = (mem_after - mem_before) / 1024
95
 
96
  st.session_state.code_output = out
97
  st.session_state.error_output = err or exc
98
 
99
- st.text_area("Output", out or "(no output)", height=120)
100
  if err or exc:
101
  st.error(err or exc)
 
102
  st.markdown(f"⏱️ **Execution Time:** `{exec_time:.4f}s`")
103
  st.markdown(f"πŸ’Ύ **Memory Used:** `{mem_used:.2f} KB`")
104
 
 
105
  if st.session_state.code:
106
- # Map Streamlit language to proper file extensions
107
  lang_extension = {
108
  "Python": "py",
109
  "C": "c",
@@ -120,4 +135,3 @@ def render_code_editor(selected_lang, ace_theme, editor_key):
120
  file_name=f"code.{ext}",
121
  mime="text/plain"
122
  )
123
-
 
 
 
1
  import streamlit as st
2
  import streamlit_ace as st_ace
3
  import os
4
+ from pathlib import Path
5
  from utils import execute_code
6
+ import time, psutil
7
 
8
+ # Default code snippets
9
  DEFAULT_SNIPPETS = {
10
  "Python": '''# default code\na = int(input())\nb = int(input())\nprint("Sum:", a + b)''',
11
  "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}''',
 
25
  }
26
 
27
  def render_code_editor(selected_lang, ace_theme, editor_key):
28
+ # ── Handle File Upload ──────────────────────────────
29
+ uploaded_file = st.file_uploader("πŸ“€ Upload file", type=["py", "cpp", "c", "java", "js", "cs", "txt"])
30
 
31
  if uploaded_file:
32
+ if uploaded_file.size > 10 * 1024 * 1024:
33
+ st.error("🚫 File too large. Max allowed is 10MB.")
34
+ else:
35
+ content = uploaded_file.read().decode("utf-8", errors="ignore")
36
+ ext = Path(uploaded_file.name).suffix.lower()
37
+
38
+ detected_lang = EXT_LANG_MAP.get(ext)
39
+ if detected_lang:
40
+ st.session_state.language = detected_lang
41
+ st.session_state.prev_language = detected_lang
42
+ selected_lang = detected_lang
43
+ st.toast(f"βœ… Auto-switched to {detected_lang}", icon="πŸ”„")
44
+ elif ext == ".txt":
45
+ st.toast("πŸ“„ Loaded text file", icon="πŸ“„")
46
+ else:
47
+ st.error("❌ Unsupported file format.")
48
+ return
49
+
50
+ st.session_state.code = content
51
+
52
+ # ── Load/Reset Code ──────────────────────────────
53
  default_code = DEFAULT_SNIPPETS[selected_lang]
54
  previous_lang = st.session_state.get("prev_language")
55
 
 
62
  st.session_state.code = default_code
63
  st.session_state.prev_language = selected_lang
64
 
65
+ # ── ACE Editor ──────────────────────────────
66
  code = st_ace.st_ace(
67
  value=st.session_state.code,
68
  placeholder=f"Start typing your {selected_lang} code…",
 
80
  if code != st.session_state.code:
81
  st.session_state.code = code
82
 
83
+ # ── Input for stdin ──────────────────────────────
84
  user_input = st.text_area(
85
  "πŸ“₯ Input (stdin)",
86
  value=st.session_state.stdin,
87
  height=100,
88
+ placeholder="Enter input values, one per line",
89
  key="stdin_input"
90
  )
 
91
  if user_input != st.session_state.stdin:
92
  st.session_state.stdin = user_input
93
 
94
+ # ── Execute Button ──────────────────────────────
95
  if st.button("▢️ Run"):
 
96
  start_time = time.perf_counter()
97
  process = psutil.Process()
98
  mem_before = process.memory_info().rss
 
105
 
106
  exec_time = time.perf_counter() - start_time
107
  mem_after = process.memory_info().rss
108
+ mem_used = (mem_after - mem_before) / 1024 # KB
109
 
110
  st.session_state.code_output = out
111
  st.session_state.error_output = err or exc
112
 
113
+ st.text_area("πŸ“€ Output", out or "(no output)", height=120)
114
  if err or exc:
115
  st.error(err or exc)
116
+
117
  st.markdown(f"⏱️ **Execution Time:** `{exec_time:.4f}s`")
118
  st.markdown(f"πŸ’Ύ **Memory Used:** `{mem_used:.2f} KB`")
119
 
120
+ # ── Download Button ──────────────────────────────
121
  if st.session_state.code:
 
122
  lang_extension = {
123
  "Python": "py",
124
  "C": "c",
 
135
  file_name=f"code.{ext}",
136
  mime="text/plain"
137
  )