Spaces:
Sleeping
Sleeping
Restoring changes
Browse files
app.py
CHANGED
|
@@ -1,19 +1,24 @@
|
|
| 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 |
-
# Page configuration (must be first)
|
| 7 |
st.set_page_config(
|
| 8 |
page_title="Pro Code Playground",
|
| 9 |
page_icon="💻",
|
| 10 |
layout="wide"
|
| 11 |
)
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
BG = "#0f1620" if dm else "#f5f5f5"
|
| 18 |
PANEL_BG = "#1c2330" if dm else "#ffffff"
|
| 19 |
TEXT = "#e3e8f1" if dm else "#1a1a1a"
|
|
@@ -22,27 +27,29 @@ BORDER = "#2a3240" if dm else "#dddddd"
|
|
| 22 |
SHADOW = "rgba(0,0,0,0.3)" if dm else "rgba(0,0,0,0.1)"
|
| 23 |
ACE_THEME = "monokai" if dm else "chrome"
|
| 24 |
|
| 25 |
-
# Global CSS
|
| 26 |
st.markdown(f"""
|
| 27 |
<style>
|
| 28 |
.stApp {{ background-color: {BG}; color: {TEXT}; }}
|
| 29 |
[data-testid="stSidebar"] {{ background-color: {PANEL_BG} !important; }}
|
| 30 |
.ace_editor, .ace_scroller {{
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
}}
|
| 35 |
textarea, input, .stTextArea textarea {{
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
}}
|
| 41 |
button, .stDownloadButton > button {{
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
}}
|
|
|
|
| 46 |
.chat-container {{ background: {PANEL_BG} !important; border: 1px solid {BORDER} !important; border-radius: 8px !important; padding: 1rem; max-height: 480px; overflow-y: auto; }}
|
| 47 |
.chat-message {{ margin-bottom: 1rem; padding: 0.75rem 1rem; border-radius: 12px; }}
|
| 48 |
.user-message {{ background: rgba(100,149,237,0.2); align-self: flex-end; }}
|
|
@@ -51,17 +58,17 @@ st.markdown(f"""
|
|
| 51 |
</style>
|
| 52 |
""", unsafe_allow_html=True)
|
| 53 |
|
| 54 |
-
# App header
|
| 55 |
st.title("Pro Code Playground")
|
| 56 |
st.markdown("Write, execute & export Python snippets, with built‑in AI assistance.")
|
| 57 |
|
| 58 |
-
# Main layout
|
| 59 |
-
|
| 60 |
|
| 61 |
-
with
|
| 62 |
st.subheader("Editor")
|
| 63 |
code = st_ace.st_ace(
|
| 64 |
-
placeholder="Start typing Python code…",
|
| 65 |
language="python",
|
| 66 |
theme=ACE_THEME,
|
| 67 |
keybinding="vscode",
|
|
@@ -71,42 +78,16 @@ with col1:
|
|
| 71 |
wrap=True,
|
| 72 |
auto_update=True
|
| 73 |
)
|
| 74 |
-
|
| 75 |
-
# Detect input() calls
|
| 76 |
-
input_calls = code.count("input(")
|
| 77 |
-
inputs = []
|
| 78 |
-
if input_calls:
|
| 79 |
-
st.info(f"Your code expects {input_calls} input(s). Provide them below:")
|
| 80 |
-
for i in range(input_calls):
|
| 81 |
-
val = st.text_input(f"Input #{i+1}", key=f"inp_{i}")
|
| 82 |
-
inputs.append(val)
|
| 83 |
-
if not all(inputs):
|
| 84 |
-
st.warning("Please fill all inputs to run the code.")
|
| 85 |
-
|
| 86 |
-
# Run button
|
| 87 |
if st.button("▶️ Run"):
|
| 88 |
-
|
| 89 |
-
if input_calls:
|
| 90 |
-
# Prep input override
|
| 91 |
-
vals = ",".join([f'"{v}"' for v in inputs])
|
| 92 |
-
stub = (
|
| 93 |
-
"import builtins\n"
|
| 94 |
-
"builtins._orig_input = builtins.input\n"
|
| 95 |
-
f"inputs = iter([{vals}])\n"
|
| 96 |
-
"builtins.input = lambda prompt='': next(inputs)\n"
|
| 97 |
-
)
|
| 98 |
-
exec_code = stub + exec_code
|
| 99 |
-
out, err, exc = execute_code(exec_code)
|
| 100 |
st.session_state.code_output = out
|
| 101 |
st.session_state.error_output = err or exc
|
| 102 |
-
|
| 103 |
-
# Show output / errors
|
| 104 |
if st.session_state.get("code_output"):
|
| 105 |
st.text_area("Output", st.session_state.code_output, height=120)
|
| 106 |
if st.session_state.get("error_output"):
|
| 107 |
st.error(st.session_state.error_output)
|
| 108 |
|
| 109 |
-
with
|
| 110 |
st.subheader("Code Assistant")
|
| 111 |
render_chatbot(
|
| 112 |
code,
|
|
@@ -114,7 +95,38 @@ with col2:
|
|
| 114 |
st.session_state.get("error_output", "")
|
| 115 |
)
|
| 116 |
|
| 117 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
st.markdown(f"""
|
| 119 |
<div style='text-align:center; margin-top:1rem; color:{TEXT}66;'>
|
| 120 |
Built with ❤️ & Streamlit
|
|
|
|
| 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(
|
| 9 |
page_title="Pro Code Playground",
|
| 10 |
page_icon="💻",
|
| 11 |
layout="wide"
|
| 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"
|
| 23 |
PANEL_BG = "#1c2330" if dm else "#ffffff"
|
| 24 |
TEXT = "#e3e8f1" if dm else "#1a1a1a"
|
|
|
|
| 27 |
SHADOW = "rgba(0,0,0,0.3)" if dm else "rgba(0,0,0,0.1)"
|
| 28 |
ACE_THEME = "monokai" if dm else "chrome"
|
| 29 |
|
| 30 |
+
# 4️⃣ Global CSS styling
|
| 31 |
st.markdown(f"""
|
| 32 |
<style>
|
| 33 |
.stApp {{ background-color: {BG}; color: {TEXT}; }}
|
| 34 |
[data-testid="stSidebar"] {{ background-color: {PANEL_BG} !important; }}
|
| 35 |
.ace_editor, .ace_scroller {{
|
| 36 |
+
background: {PANEL_BG} !important;
|
| 37 |
+
box-shadow: 0 4px 8px {SHADOW} !important;
|
| 38 |
+
border-radius: 8px !important;
|
| 39 |
}}
|
| 40 |
textarea, input, .stTextArea textarea {{
|
| 41 |
+
background: {PANEL_BG} !important;
|
| 42 |
+
color: {TEXT} !important;
|
| 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; }}
|
|
|
|
| 58 |
</style>
|
| 59 |
""", unsafe_allow_html=True)
|
| 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",
|
|
|
|
| 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,
|
|
|
|
| 95 |
st.session_state.get("error_output", "")
|
| 96 |
)
|
| 97 |
|
| 98 |
+
# 7️⃣ Input section
|
| 99 |
+
# Allow users to specify runtime inputs (comma-separated)
|
| 100 |
+
st.session_state.setdefault('user_inputs', '')
|
| 101 |
+
user_inputs = st.text_input(
|
| 102 |
+
"🔢 Enter input values (comma-separated)",
|
| 103 |
+
value=st.session_state.user_inputs,
|
| 104 |
+
help="E.g., 5,7 for two numbers"
|
| 105 |
+
)
|
| 106 |
+
st.session_state.user_inputs = user_inputs
|
| 107 |
+
|
| 108 |
+
# Update run button to pass inputs into code execution
|
| 109 |
+
if st.button("▶️ Run"):
|
| 110 |
+
# If user provided inputs, inject into code via input() override
|
| 111 |
+
exec_code = code
|
| 112 |
+
if user_inputs:
|
| 113 |
+
# Prepare input simulation
|
| 114 |
+
values = user_inputs.split(',')
|
| 115 |
+
input_stub = '
|
| 116 |
+
'.join([f"print({v.strip()})" for v in values])
|
| 117 |
+
exec_code = f"{input_stub}
|
| 118 |
+
" + code
|
| 119 |
+
out, err, exc = execute_code(exec_code)
|
| 120 |
+
st.session_state.code_output = out
|
| 121 |
+
st.session_state.error_output = err or exc
|
| 122 |
+
|
| 123 |
+
# Display results
|
| 124 |
+
if st.session_state.get("code_output"):
|
| 125 |
+
st.text_area("Output", st.session_state.code_output, height=120)
|
| 126 |
+
if st.session_state.get("error_output"):
|
| 127 |
+
st.error(st.session_state.error_output)
|
| 128 |
+
|
| 129 |
+
# 8️⃣ Footer
|
| 130 |
st.markdown(f"""
|
| 131 |
<div style='text-align:center; margin-top:1rem; color:{TEXT}66;'>
|
| 132 |
Built with ❤️ & Streamlit
|