vsj0702 commited on
Commit
10fc7f1
Β·
verified Β·
1 Parent(s): f94219b

Making more beautiful

Browse files
Files changed (1) hide show
  1. app.py +97 -81
app.py CHANGED
@@ -5,7 +5,7 @@ from chatbot import render_chatbot
5
  import json
6
 
7
  # β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
8
- # 1️⃣ PAGE CONFIG (MUST be the first Streamlit call)
9
  st.set_page_config(
10
  page_title="Interactive Code Editor with AI Assistant",
11
  page_icon="πŸ’»",
@@ -16,56 +16,94 @@ st.set_page_config(
16
  # 2️⃣ THEME SELECTOR
17
  theme = st.sidebar.selectbox("🎨 App Theme", ["Light", "Dark"])
18
  is_dark = (theme == "Dark")
19
-
20
- # 3️⃣ MAP TO ACE EDITOR THEME
21
  ace_theme = "monokai" if is_dark else "chrome"
22
 
23
- # 4️⃣ DEFINE COLORS FOR GLOBAL CSS
24
- bg_color = "#1e1e1e" if is_dark else "#ffffff"
25
- text_color = "#e5e5e5" if is_dark else "#000000"
26
- sidebar_bg = "#252526" if is_dark else "#f0f2f6"
27
- button_bg = "#3a3d41" if is_dark else "#e7eaf0"
28
- chat_bg = "#2e2e2e" if is_dark else "#f9f9f9"
29
- border_color = "#444444" if is_dark else "#dddddd"
30
- user_bg = "#3d5a80" if is_dark else "#e3f2fd"
31
- bot_bg = "#555555" if is_dark else "#f5f5f5"
32
-
33
- # 5️⃣ INJECT GLOBAL CSS
34
  st.markdown(f"""
 
35
  <style>
36
- /* Main app background and text */
 
 
 
 
37
  .stApp {{
38
- background-color: {bg_color};
39
- color: {text_color};
 
 
 
 
 
 
 
 
 
 
 
40
  }}
41
- /* Sidebar styling */
42
- .css-1d391kg .css-1d391kg {{ /* wrapper class Streamlit uses for sidebar */
43
- background-color: {sidebar_bg} !important;
44
- color: {text_color};
45
  }}
46
- /* Buttons */
47
- .stButton>button {{
48
- background-color: {button_bg} !important;
49
- color: {text_color} !important;
 
50
  }}
51
- /* Chat container and messages */
 
 
 
 
 
 
 
 
 
 
 
 
52
  .chat-container {{
53
- background-color: {chat_bg} !important;
54
- color: {text_color} !important;
55
- border: 1px solid {border_color} !important;
 
 
 
 
 
 
 
 
 
56
  }}
57
  .user-message {{
58
- background-color: {user_bg} !important;
59
- color: {text_color} !important;
 
60
  }}
61
  .bot-message {{
62
- background-color: {bot_bg} !important;
63
- color: {text_color} !important;
 
 
 
 
 
 
64
  }}
65
- /* Text area output box */
66
- textarea {{
67
- background-color: {chat_bg} !important;
68
- color: {text_color} !important;
 
 
 
 
69
  }}
70
  </style>
71
  """, unsafe_allow_html=True)
@@ -73,28 +111,25 @@ st.markdown(f"""
73
  # β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
74
 
75
  # Initialize session state
76
- if 'code_output' not in st.session_state:
77
- st.session_state.code_output = ""
78
- if 'error_output' not in st.session_state:
79
- st.session_state.error_output = ""
80
 
81
- # Title and description
82
  st.title("πŸ’» Interactive Code Editor")
83
  st.markdown("""
84
  Write, execute, and export Python code in this interactive editor.
85
- The editor supports syntax highlighting and autocompletion.
86
  """)
87
 
88
  # Main layout: two columns
89
- col1, col2 = st.columns([1, 1])
90
 
91
  with col1:
92
- # Code Editor Section
93
  st.subheader("Code Editor")
94
  code = st_ace.st_ace(
95
  placeholder="Write your Python code here...",
96
  language="python",
97
- theme=ace_theme, # ← use the selected ACE theme
98
  keybinding="vscode",
99
  font_size=14,
100
  min_lines=25,
@@ -104,55 +139,36 @@ with col1:
104
  show_print_margin=True,
105
  auto_update=True
106
  )
 
 
 
 
107
 
108
- # Execute button and output capture
109
- if st.button("▢️ Run Code", type="primary"):
110
- output, error, exception = execute_code(code)
111
- st.session_state.code_output = output
112
- st.session_state.error_output = error if error else exception
113
-
114
- # Display immediate results
115
  if st.session_state.code_output:
116
- st.text_area("Output", st.session_state.code_output, height=100)
117
  if st.session_state.error_output:
118
  st.error(st.session_state.error_output)
119
 
120
  with col2:
121
- # AI Assistant Section
122
  st.subheader("πŸ€– Code Assistant")
123
  render_chatbot(code, st.session_state.code_output, st.session_state.error_output)
124
 
125
  # Export options
126
  st.markdown("---")
127
  st.subheader("Export Options")
128
-
129
- export_data = export_session(
130
- code,
131
- st.session_state.code_output,
132
- st.session_state.error_output
133
- )
134
-
135
- col1_export, col2_export = st.columns([5, 4])
136
-
137
- with col1_export:
138
- st.download_button(
139
- "πŸ“„ Export as JSON",
140
- data=json.dumps(export_data, indent=2),
141
- file_name="code_session.json",
142
- mime="application/json"
143
- )
144
-
145
- with col2_export:
146
- st.download_button(
147
- "πŸ“ Export as Text",
148
- data=f"""# Code:\n{code}\n\n# Output:\n{st.session_state.code_output}\n\n# Errors:\n{st.session_state.error_output}""",
149
- file_name="code_session.txt",
150
- mime="text/plain"
151
- )
152
 
153
  # Footer
154
  st.markdown("""
155
- <div style='text-align: center'>
156
- <p>Built with Streamlit By Vibhav with ❀️</p>
157
  </div>
158
  """, unsafe_allow_html=True)
 
5
  import json
6
 
7
  # β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
8
+ # 1️⃣ PAGE CONFIG (MUST be first)
9
  st.set_page_config(
10
  page_title="Interactive Code Editor with AI Assistant",
11
  page_icon="πŸ’»",
 
16
  # 2️⃣ THEME SELECTOR
17
  theme = st.sidebar.selectbox("🎨 App Theme", ["Light", "Dark"])
18
  is_dark = (theme == "Dark")
 
 
19
  ace_theme = "monokai" if is_dark else "chrome"
20
 
21
+ # 3️⃣ GLOBAL CSS & FONTS
22
+ FONT_URL = "https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400;500&display=swap"
 
 
 
 
 
 
 
 
 
23
  st.markdown(f"""
24
+ <link href="{FONT_URL}" rel="stylesheet">
25
  <style>
26
+ /* --------------- base --------------- */
27
+ * {{
28
+ font-family: 'Roboto Mono', monospace !important;
29
+ transition: background-color .3s, color .3s;
30
+ }}
31
  .stApp {{
32
+ background-color: {'#121212' if is_dark else '#f6f8fa'} !important;
33
+ color: {'#e1e1e1' if is_dark else '#202124'} !important;
34
+ padding: 1rem 2rem;
35
+ }}
36
+ /* --------------- sidebar --------------- */
37
+ [data-testid="stSidebar"] {{
38
+ background-color: {'#1e1e1e' if is_dark else '#ffffff'} !important;
39
+ color: {'#e1e1e1' if is_dark else '#202124'} !important;
40
+ border-right: 1px solid {'#333' if is_dark else '#ddd'};
41
+ }}
42
+ /* --------------- headings --------------- */
43
+ h1, h2, h3 {{
44
+ font-weight: 500 !important;
45
  }}
46
+ /* --------------- editor & panels --------------- */
47
+ .ace_editor, .ace_scroller {{
48
+ border-radius: 0.5rem !important;
49
+ box-shadow: 0 4px 8px rgba(0,0,0,0.2);
50
  }}
51
+ textarea, .stTextArea>div>textarea {{
52
+ background-color: {'#1e1e1e' if is_dark else '#fafafa'} !important;
53
+ color: {'#e1e1e1' if is_dark else '#202124'} !important;
54
+ border-radius: 0.5rem !important;
55
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
56
  }}
57
+ /* --------------- buttons --------------- */
58
+ button {{
59
+ border: none !important;
60
+ padding: 0.5rem 1rem !important;
61
+ border-radius: 0.375rem !important;
62
+ background-color: {'#0a84ff' if is_dark else '#1a73e8'} !important;
63
+ color: #fff !important;
64
+ font-weight: 500 !important;
65
+ }}
66
+ button:hover {{
67
+ background-color: {'#0060df' if is_dark else '#1558b0'} !important;
68
+ }}
69
+ /* --------------- chat container --------------- */
70
  .chat-container {{
71
+ background-color: {'#1e1e1e' if is_dark else '#ffffff'} !important;
72
+ border: 1px solid {'#333' if is_dark else '#ddd'} !important;
73
+ border-radius: 0.5rem !important;
74
+ padding: 1rem !important;
75
+ max-height: 500px;
76
+ overflow-y: auto;
77
+ }}
78
+ .chat-message {{
79
+ margin-bottom: 1rem !important;
80
+ padding: 0.75rem 1rem !important;
81
+ position: relative;
82
+ animation: fadeIn 0.3s ease-out both;
83
  }}
84
  .user-message {{
85
+ background-color: {'#2a2a2a' if is_dark else '#e8f0fe'} !important;
86
+ align-self: flex-end;
87
+ border-top-right-radius: 0;
88
  }}
89
  .bot-message {{
90
+ background-color: {'#333333' if is_dark else '#f1f3f4'} !important;
91
+ align-self: flex-start;
92
+ border-top-left-radius: 0;
93
+ }}
94
+ /* --------------- animations --------------- */
95
+ @keyframes fadeIn {{
96
+ from {{ opacity: 0; transform: translateY(10px); }}
97
+ to {{ opacity: 1; transform: translateY(0); }}
98
  }}
99
+ /* --------------- shadows & cards --------------- */
100
+ .stInfo, .stError, .stSuccess {{
101
+ box-shadow: 0 2px 6px rgba(0,0,0,0.15) !important;
102
+ border-radius: 0.5rem !important;
103
+ }}
104
+ hr {{
105
+ border: none; height: 1px;
106
+ background-color: {'#333' if is_dark else '#ddd'}; margin: 1rem 0;
107
  }}
108
  </style>
109
  """, unsafe_allow_html=True)
 
111
  # β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
112
 
113
  # Initialize session state
114
+ st.session_state.setdefault('code_output', "")
115
+ st.session_state.setdefault('error_output', "")
 
 
116
 
117
+ # Title & description
118
  st.title("πŸ’» Interactive Code Editor")
119
  st.markdown("""
120
  Write, execute, and export Python code in this interactive editor.
121
+ The editor supports syntax highlighting, autocompletion, and an AI assistant.
122
  """)
123
 
124
  # Main layout: two columns
125
+ col1, col2 = st.columns([1, 1], gap="large")
126
 
127
  with col1:
 
128
  st.subheader("Code Editor")
129
  code = st_ace.st_ace(
130
  placeholder="Write your Python code here...",
131
  language="python",
132
+ theme=ace_theme,
133
  keybinding="vscode",
134
  font_size=14,
135
  min_lines=25,
 
139
  show_print_margin=True,
140
  auto_update=True
141
  )
142
+ if st.button("▢️ Run Code"):
143
+ out, err, exc = execute_code(code)
144
+ st.session_state.code_output = out
145
+ st.session_state.error_output = err or exc
146
 
 
 
 
 
 
 
 
147
  if st.session_state.code_output:
148
+ st.text_area("Output", st.session_state.code_output, height=120)
149
  if st.session_state.error_output:
150
  st.error(st.session_state.error_output)
151
 
152
  with col2:
 
153
  st.subheader("πŸ€– Code Assistant")
154
  render_chatbot(code, st.session_state.code_output, st.session_state.error_output)
155
 
156
  # Export options
157
  st.markdown("---")
158
  st.subheader("Export Options")
159
+ export_data = export_session(code, st.session_state.code_output, st.session_state.error_output)
160
+ c1, c2 = st.columns([5,4])
161
+ with c1:
162
+ st.download_button("πŸ“„ Export JSON", data=json.dumps(export_data, indent=2),
163
+ file_name="session.json", mime="application/json")
164
+ with c2:
165
+ st.download_button("πŸ“ Export Text",
166
+ data=f"# Code:\n{code}\n\n# Output:\n{st.session_state.code_output}\n\n# Errors:\n{st.session_state.error_output}",
167
+ file_name="session.txt", mime="text/plain")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
 
169
  # Footer
170
  st.markdown("""
171
+ <div style='text-align:center; margin-top:2rem;'>
172
+ <small>Built with ❀️ using Streamlit & Groq AI</small>
173
  </div>
174
  """, unsafe_allow_html=True)