vsj0702 commited on
Commit
dd04902
·
verified ·
1 Parent(s): ac8dc7c

Adding file upload

Browse files
Files changed (1) hide show
  1. code_editor.py +37 -10
code_editor.py CHANGED
@@ -2,6 +2,7 @@
2
 
3
  import streamlit as st
4
  import streamlit_ace as st_ace
 
5
  from utils import execute_code
6
 
7
  # Default templates for each language
@@ -14,14 +15,31 @@ DEFAULT_SNIPPETS = {
14
  "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}'''
15
  }
16
 
 
 
 
 
 
 
 
 
17
 
18
  def render_code_editor(selected_lang, ace_theme, editor_key):
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- # Load the default code for the current language
21
  default_code = DEFAULT_SNIPPETS[selected_lang]
22
  previous_lang = st.session_state.get("prev_language")
23
 
24
- # Reset code when switching language or if empty
25
  if (
26
  st.session_state.get("code") is None
27
  or st.session_state.code.strip() == ""
@@ -31,7 +49,6 @@ def render_code_editor(selected_lang, ace_theme, editor_key):
31
  st.session_state.code = default_code
32
  st.session_state.prev_language = selected_lang
33
 
34
- # Code editor block
35
  code = st_ace.st_ace(
36
  value=st.session_state.code,
37
  placeholder=f"Start typing your {selected_lang} code…",
@@ -49,7 +66,6 @@ def render_code_editor(selected_lang, ace_theme, editor_key):
49
  if code != st.session_state.code:
50
  st.session_state.code = code
51
 
52
- # Input area for stdin
53
  user_input = st.text_area(
54
  "📥 Input (stdin)",
55
  value=st.session_state.stdin,
@@ -61,19 +77,30 @@ def render_code_editor(selected_lang, ace_theme, editor_key):
61
  if user_input != st.session_state.stdin:
62
  st.session_state.stdin = user_input
63
 
64
- # Run button
65
  if st.button("▶️ Run"):
 
 
 
 
 
66
  out, err, exc = execute_code(
67
  code=st.session_state.code,
68
  stdin=st.session_state.stdin,
69
  language=selected_lang
70
  )
 
 
 
 
 
71
  st.session_state.code_output = out
72
  st.session_state.error_output = err or exc
73
 
74
- # Output section
75
- if st.session_state.get("code_output"):
76
- st.text_area("Output", st.session_state.code_output, height=120)
 
 
77
 
78
- if st.session_state.get("error_output"):
79
- st.error(st.session_state.error_output)
 
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
 
15
  "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}'''
16
  }
17
 
18
+ EXT_LANG_MAP = {
19
+ ".py": "Python",
20
+ ".cpp": "C++",
21
+ ".c": "C",
22
+ ".java": "Java",
23
+ ".js": "JavaScript",
24
+ ".cs": "C#"
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
 
 
43
  if (
44
  st.session_state.get("code") is None
45
  or st.session_state.code.strip() == ""
 
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
  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,
 
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
85
+
86
  out, err, exc = execute_code(
87
  code=st.session_state.code,
88
  stdin=st.session_state.stdin,
89
  language=selected_lang
90
  )
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
+ st.download_button("📥 Download Code", st.session_state.code, file_name=f"code_{selected_lang.lower()}.txt")