Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -58,82 +58,84 @@ def status_sandbox_api(sandbox_id):
|
|
| 58 |
info = user_sandboxes.get(sandbox_id)
|
| 59 |
if not info:
|
| 60 |
return "Sandbox not found"
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
except Exception as e:
|
| 64 |
-
return str(e)
|
| 65 |
|
| 66 |
# -------------------------
|
| 67 |
-
#
|
| 68 |
# -------------------------
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
# Background updater
|
| 96 |
-
def updater():
|
| 97 |
-
while sandbox_id in user_sandboxes:
|
| 98 |
-
elapsed = int(time.time() - info["start_time"])
|
| 99 |
-
runtime_box.value = str(elapsed)
|
| 100 |
-
logs_box.value = fetch_logs_api(sandbox_id)
|
| 101 |
-
if elapsed >= SANDBOX_TTL:
|
| 102 |
-
kill_sandbox_api(sandbox_id)
|
| 103 |
-
break
|
| 104 |
-
time.sleep(2)
|
| 105 |
-
sandbox_container.remove(card)
|
| 106 |
-
|
| 107 |
-
threading.Thread(target=updater, daemon=True).start()
|
| 108 |
-
return card
|
| 109 |
|
| 110 |
# -------------------------
|
| 111 |
# Main Gradio UI
|
| 112 |
# -------------------------
|
| 113 |
-
with gr.Blocks(css=
|
| 114 |
-
.
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
|
| 120 |
-
}
|
| 121 |
-
""") as demo:
|
| 122 |
-
gr.Markdown("# Sandbox Dashboard")
|
| 123 |
-
|
| 124 |
-
code_input = gr.Textbox(label="Main Code", lines=10)
|
| 125 |
-
req_input = gr.Textbox(label="requirements.txt", lines=5)
|
| 126 |
launch_btn = gr.Button("Launch Sandbox")
|
| 127 |
launch_status = gr.Textbox(label="Launch Status")
|
| 128 |
|
| 129 |
sandbox_container = gr.Column()
|
| 130 |
|
|
|
|
|
|
|
|
|
|
| 131 |
def on_launch(code, requirements):
|
| 132 |
sandbox_id, backend_or_error = launch_sandbox_api(code, requirements)
|
| 133 |
if not sandbox_id:
|
| 134 |
return f"Error: {backend_or_error}"
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
launch_btn.click(on_launch, inputs=[code_input, req_input], outputs=launch_status)
|
| 139 |
|
|
|
|
| 58 |
info = user_sandboxes.get(sandbox_id)
|
| 59 |
if not info:
|
| 60 |
return "Sandbox not found"
|
| 61 |
+
elapsed = int(time.time() - info["start_time"])
|
| 62 |
+
return f"Running for {elapsed} seconds"
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# -------------------------
|
| 65 |
+
# Dark-themed CSS
|
| 66 |
# -------------------------
|
| 67 |
+
DARK_CSS = """
|
| 68 |
+
body {
|
| 69 |
+
background-color: #1e1e1e;
|
| 70 |
+
color: #f0f0f0;
|
| 71 |
+
}
|
| 72 |
+
.sandbox-card {
|
| 73 |
+
border-radius: 12px;
|
| 74 |
+
padding: 15px;
|
| 75 |
+
margin: 10px;
|
| 76 |
+
background-color: #2b2b2b;
|
| 77 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.4);
|
| 78 |
+
}
|
| 79 |
+
.gr-button {
|
| 80 |
+
background-color: #3a3a3a;
|
| 81 |
+
color: #f0f0f0;
|
| 82 |
+
border: 1px solid #555;
|
| 83 |
+
}
|
| 84 |
+
.gr-button:hover {
|
| 85 |
+
background-color: #505050;
|
| 86 |
+
}
|
| 87 |
+
.gr-textbox, .gr-markdown {
|
| 88 |
+
background-color: #1e1e1e;
|
| 89 |
+
color: #f0f0f0;
|
| 90 |
+
border: 1px solid #444;
|
| 91 |
+
}
|
| 92 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
# -------------------------
|
| 95 |
# Main Gradio UI
|
| 96 |
# -------------------------
|
| 97 |
+
with gr.Blocks(css=DARK_CSS) as demo:
|
| 98 |
+
gr.Markdown("# 🛡 Sandbox Dashboard")
|
| 99 |
+
|
| 100 |
+
with gr.Row():
|
| 101 |
+
code_input = gr.Textbox(label="Main Code", lines=10)
|
| 102 |
+
req_input = gr.Textbox(label="requirements.txt", lines=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
launch_btn = gr.Button("Launch Sandbox")
|
| 104 |
launch_status = gr.Textbox(label="Launch Status")
|
| 105 |
|
| 106 |
sandbox_container = gr.Column()
|
| 107 |
|
| 108 |
+
# -------------------------
|
| 109 |
+
# Handlers for dynamic sandbox cards
|
| 110 |
+
# -------------------------
|
| 111 |
def on_launch(code, requirements):
|
| 112 |
sandbox_id, backend_or_error = launch_sandbox_api(code, requirements)
|
| 113 |
if not sandbox_id:
|
| 114 |
return f"Error: {backend_or_error}"
|
| 115 |
+
|
| 116 |
+
# Create a new card in the container
|
| 117 |
+
with sandbox_container:
|
| 118 |
+
with gr.Column(elem_classes="sandbox-card") as card:
|
| 119 |
+
gr.Markdown(f"**Sandbox ID:** {sandbox_id}")
|
| 120 |
+
gr.Markdown(f"**Backend:** {backend_or_error}")
|
| 121 |
+
runtime_box = gr.Textbox(label="Runtime (sec)", value="0", interactive=False)
|
| 122 |
+
logs_box = gr.Textbox(label="Logs", lines=10, interactive=False)
|
| 123 |
+
|
| 124 |
+
with gr.Row():
|
| 125 |
+
code_box = gr.Textbox(label="Main Code", value=code, lines=10, interactive=False)
|
| 126 |
+
req_box = gr.Textbox(label="requirements.txt", value=requirements, lines=10, interactive=False)
|
| 127 |
+
|
| 128 |
+
# Buttons
|
| 129 |
+
logs_btn = gr.Button("Fetch Logs")
|
| 130 |
+
kill_btn = gr.Button("Kill Sandbox")
|
| 131 |
+
status_btn = gr.Button("Check Status")
|
| 132 |
+
|
| 133 |
+
# Assign clicks using lambdas
|
| 134 |
+
logs_btn.click(lambda sid=sandbox_id: fetch_logs_api(sid), inputs=[], outputs=logs_box)
|
| 135 |
+
kill_btn.click(lambda sid=sandbox_id: kill_sandbox_api(sid), inputs=[], outputs=logs_box)
|
| 136 |
+
status_btn.click(lambda sid=sandbox_id: status_sandbox_api(sid), inputs=[], outputs=runtime_box)
|
| 137 |
+
|
| 138 |
+
return f"Sandbox {sandbox_id} launched on {backend_or_error}"
|
| 139 |
|
| 140 |
launch_btn.click(on_launch, inputs=[code_input, req_input], outputs=launch_status)
|
| 141 |
|