Spaces:
Sleeping
Sleeping
Restoring
Browse files
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 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 |
|
| 6 |
# 1️⃣ Page configuration (must be first)
|
| 7 |
st.set_page_config(
|
|
@@ -11,7 +12,11 @@ st.set_page_config(
|
|
| 11 |
)
|
| 12 |
|
| 13 |
# 2️⃣ Dark mode toggle
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# 3️⃣ Theme variables
|
| 17 |
BG = "#0f1620" if dm else "#f5f5f5"
|
|
@@ -38,7 +43,13 @@ st.markdown(f"""
|
|
| 38 |
border: 1px solid {BORDER} !important;
|
| 39 |
border-radius: 4px !important;
|
| 40 |
}}
|
| 41 |
-
button
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
.chat-container {{ background: {PANEL_BG} !important; border: 1px solid {BORDER} !important; border-radius: 8px !important; padding: 1rem; max-height: 480px; overflow-y: auto; }}
|
| 43 |
.chat-message {{ margin-bottom: 1rem; padding: 0.75rem 1rem; border-radius: 12px; }}
|
| 44 |
.user-message {{ background: rgba(100,149,237,0.2); align-self: flex-end; }}
|
|
@@ -49,69 +60,64 @@ st.markdown(f"""
|
|
| 49 |
|
| 50 |
# 5️⃣ App header
|
| 51 |
st.title("Pro Code Playground")
|
| 52 |
-
st.markdown("Write, execute Python snippets, with built‑in AI assistance.")
|
| 53 |
|
| 54 |
-
# 6️⃣
|
| 55 |
-
|
| 56 |
|
| 57 |
-
with
|
| 58 |
st.subheader("Editor")
|
| 59 |
code = st_ace.st_ace(
|
| 60 |
-
placeholder="
|
| 61 |
language="python",
|
| 62 |
theme=ACE_THEME,
|
| 63 |
keybinding="vscode",
|
| 64 |
font_size=14,
|
| 65 |
-
min_lines=
|
| 66 |
show_gutter=True,
|
| 67 |
wrap=True,
|
| 68 |
auto_update=True
|
| 69 |
)
|
| 70 |
-
|
| 71 |
-
# 7️⃣ Detect input() calls and collect values
|
| 72 |
-
input_count = code.count("input(")
|
| 73 |
-
inputs = []
|
| 74 |
-
if input_count > 0:
|
| 75 |
-
st.info(f"Your code expects {input_count} input() call(s). Please provide values:")
|
| 76 |
-
for i in range(input_count):
|
| 77 |
-
val = st.text_input(f"Input value #{i+1}", key=f"in_{i}", help="Enter value for input() call")
|
| 78 |
-
inputs.append(val)
|
| 79 |
-
if not all(val.strip() for val in inputs):
|
| 80 |
-
st.warning("Please fill all input fields before running the code.")
|
| 81 |
-
|
| 82 |
-
# 8️⃣ Run code
|
| 83 |
if st.button("▶️ Run"):
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
exec_code = code
|
| 88 |
-
if input_count > 0:
|
| 89 |
-
# Override builtins.input to return provided values in order
|
| 90 |
-
vals_list = [repr(v) for v in inputs]
|
| 91 |
-
stub = (
|
| 92 |
-
"import builtins
|
| 93 |
-
"
|
| 94 |
-
f"vals = iter([{', '.join(vals_list)}])
|
| 95 |
-
"
|
| 96 |
-
"builtins.input = lambda prompt='': next(vals)
|
| 97 |
-
"
|
| 98 |
-
)
|
| 99 |
-
exec_code = stub + exec_code
|
| 100 |
-
|
| 101 |
-
out, err, exc = execute_code(exec_code)
|
| 102 |
-
st.session_state.code_output = out
|
| 103 |
-
st.session_state.error_output = err or exc
|
| 104 |
-
|
| 105 |
-
# 9️⃣ Display output and errors and errors
|
| 106 |
if st.session_state.get("code_output"):
|
| 107 |
st.text_area("Output", st.session_state.code_output, height=120)
|
| 108 |
if st.session_state.get("error_output"):
|
| 109 |
st.error(st.session_state.error_output)
|
| 110 |
|
| 111 |
-
with
|
| 112 |
st.subheader("Code Assistant")
|
| 113 |
render_chatbot(
|
| 114 |
code,
|
| 115 |
st.session_state.get("code_output", ""),
|
| 116 |
st.session_state.get("error_output", "")
|
| 117 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import streamlit_ace as st_ace
|
| 3 |
+
from utils import execute_code, export_session
|
| 4 |
from chatbot import render_chatbot
|
| 5 |
+
import json
|
| 6 |
|
| 7 |
# 1️⃣ Page configuration (must be first)
|
| 8 |
st.set_page_config(
|
|
|
|
| 12 |
)
|
| 13 |
|
| 14 |
# 2️⃣ Dark mode toggle
|
| 15 |
+
if 'dark_mode' not in st.session_state:
|
| 16 |
+
st.session_state.dark_mode = True
|
| 17 |
+
|
| 18 |
+
dm = st.checkbox("🌙 Dark mode", value=st.session_state.dark_mode)
|
| 19 |
+
st.session_state.dark_mode = dm
|
| 20 |
|
| 21 |
# 3️⃣ Theme variables
|
| 22 |
BG = "#0f1620" if dm else "#f5f5f5"
|
|
|
|
| 43 |
border: 1px solid {BORDER} !important;
|
| 44 |
border-radius: 4px !important;
|
| 45 |
}}
|
| 46 |
+
button, .stDownloadButton > button {{
|
| 47 |
+
background-color: {ACCENT} !important;
|
| 48 |
+
color: #fff !important;
|
| 49 |
+
border-radius: 6px !important;
|
| 50 |
+
transition: transform 0.1s;
|
| 51 |
+
}}
|
| 52 |
+
button:hover {{ transform: scale(1.02) !important; }}
|
| 53 |
.chat-container {{ background: {PANEL_BG} !important; border: 1px solid {BORDER} !important; border-radius: 8px !important; padding: 1rem; max-height: 480px; overflow-y: auto; }}
|
| 54 |
.chat-message {{ margin-bottom: 1rem; padding: 0.75rem 1rem; border-radius: 12px; }}
|
| 55 |
.user-message {{ background: rgba(100,149,237,0.2); align-self: flex-end; }}
|
|
|
|
| 60 |
|
| 61 |
# 5️⃣ App header
|
| 62 |
st.title("Pro Code Playground")
|
| 63 |
+
st.markdown("Write, execute & export Python snippets, with built‑in AI assistance.")
|
| 64 |
|
| 65 |
+
# 6️⃣ Main layout
|
| 66 |
+
gen, bot = st.columns((2,1), gap="large")
|
| 67 |
|
| 68 |
+
with gen:
|
| 69 |
st.subheader("Editor")
|
| 70 |
code = st_ace.st_ace(
|
| 71 |
+
placeholder="Start typing your Python code…",
|
| 72 |
language="python",
|
| 73 |
theme=ACE_THEME,
|
| 74 |
keybinding="vscode",
|
| 75 |
font_size=14,
|
| 76 |
+
min_lines=20,
|
| 77 |
show_gutter=True,
|
| 78 |
wrap=True,
|
| 79 |
auto_update=True
|
| 80 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
if st.button("▶️ Run"):
|
| 82 |
+
out, err, exc = execute_code(code)
|
| 83 |
+
st.session_state.code_output = out
|
| 84 |
+
st.session_state.error_output = err or exc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
if st.session_state.get("code_output"):
|
| 86 |
st.text_area("Output", st.session_state.code_output, height=120)
|
| 87 |
if st.session_state.get("error_output"):
|
| 88 |
st.error(st.session_state.error_output)
|
| 89 |
|
| 90 |
+
with bot:
|
| 91 |
st.subheader("Code Assistant")
|
| 92 |
render_chatbot(
|
| 93 |
code,
|
| 94 |
st.session_state.get("code_output", ""),
|
| 95 |
st.session_state.get("error_output", "")
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
# 7️⃣ Export section
|
| 99 |
+
st.markdown("---")
|
| 100 |
+
st.subheader("Export")
|
| 101 |
+
export_data = export_session(
|
| 102 |
+
code,
|
| 103 |
+
st.session_state.get("code_output", ""),
|
| 104 |
+
st.session_state.get("error_output", "")
|
| 105 |
+
)
|
| 106 |
+
col1, col2 = st.columns(2)
|
| 107 |
+
with col1:
|
| 108 |
+
st.download_button(
|
| 109 |
+
"📦 JSON",
|
| 110 |
+
data=json.dumps(export_data, indent=2),
|
| 111 |
+
file_name="session.json",
|
| 112 |
+
mime="application/json"
|
| 113 |
+
)
|
| 114 |
+
with col2:
|
| 115 |
+
txt = f"# Code\n{code}\n\n# Output\n{st.session_state.get('code_output','')}\n\n# Errors\n{st.session_state.get('error_output','')}"
|
| 116 |
+
st.download_button("📝 Text", txt, "session.txt", "text/plain")
|
| 117 |
+
|
| 118 |
+
# 8️⃣ Footer
|
| 119 |
+
st.markdown(f"""
|
| 120 |
+
<div style='text-align:center; margin-top:1rem; color:{TEXT}66;'>
|
| 121 |
+
Built with ❤️ & Streamlit
|
| 122 |
+
</div>
|
| 123 |
+
""", unsafe_allow_html=True)
|