Spaces:
Sleeping
Sleeping
Making more beautiful
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ from chatbot import render_chatbot
|
|
| 5 |
import json
|
| 6 |
|
| 7 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 8 |
-
# 1οΈβ£ PAGE CONFIG (MUST be
|
| 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 |
-
#
|
| 24 |
-
|
| 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 |
-
/*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
.stApp {{
|
| 38 |
-
background-color: {
|
| 39 |
-
color: {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
}}
|
| 41 |
-
/*
|
| 42 |
-
.
|
| 43 |
-
|
| 44 |
-
|
| 45 |
}}
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
| 50 |
}}
|
| 51 |
-
/*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
.chat-container {{
|
| 53 |
-
background-color: {
|
| 54 |
-
|
| 55 |
-
border:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
}}
|
| 57 |
.user-message {{
|
| 58 |
-
background-color: {
|
| 59 |
-
|
|
|
|
| 60 |
}}
|
| 61 |
.bot-message {{
|
| 62 |
-
background-color: {
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
}}
|
| 65 |
-
/*
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
}}
|
| 70 |
</style>
|
| 71 |
""", unsafe_allow_html=True)
|
|
@@ -73,28 +111,25 @@ st.markdown(f"""
|
|
| 73 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 74 |
|
| 75 |
# Initialize session state
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
if 'error_output' not in st.session_state:
|
| 79 |
-
st.session_state.error_output = ""
|
| 80 |
|
| 81 |
-
# Title
|
| 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
|
| 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,
|
| 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=
|
| 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 |
-
|
| 130 |
-
|
| 131 |
-
st.
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 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:
|
| 156 |
-
<
|
| 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)
|