Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import threading
|
| 4 |
-
import time
|
| 5 |
import uuid
|
| 6 |
-
import
|
| 7 |
from fastapi import FastAPI, UploadFile, File
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
import streamlit as st
|
|
@@ -14,6 +13,7 @@ LOG_DIR = "logs"
|
|
| 14 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 15 |
os.makedirs(LOG_DIR, exist_ok=True)
|
| 16 |
|
|
|
|
| 17 |
executions = {}
|
| 18 |
exec_counter = 0
|
| 19 |
|
|
@@ -76,62 +76,50 @@ def stop(exec_id: int):
|
|
| 76 |
def list_execs():
|
| 77 |
return [{"id": k, "file": v["file"], "status": v["status"]} for k, v in executions.items()]
|
| 78 |
|
| 79 |
-
# ------------------ Streamlit
|
| 80 |
-
def show_system_stats():
|
| 81 |
-
cpu = psutil.cpu_percent()
|
| 82 |
-
ram = psutil.virtual_memory()
|
| 83 |
-
threads = psutil.cpu_count()
|
| 84 |
-
st.markdown("### System Resource Usage")
|
| 85 |
-
st.metric("CPU Usage", f"{cpu}%")
|
| 86 |
-
st.metric("RAM Usage", f"{ram.percent}% of {round(ram.total / 1e9, 2)} GB")
|
| 87 |
-
st.metric("CPU Threads", threads)
|
| 88 |
-
|
| 89 |
def streamlit_ui():
|
| 90 |
-
st.set_page_config("
|
| 91 |
-
st.title("
|
| 92 |
|
| 93 |
-
|
| 94 |
-
st.divider()
|
| 95 |
-
|
| 96 |
-
file = st.file_uploader("Select a Python file", type=["py"])
|
| 97 |
|
| 98 |
-
if file and st.button("
|
| 99 |
res = requests.post("http://localhost:8000/upload/", files={"file": file})
|
| 100 |
exec_id = res.json()["exec_id"]
|
| 101 |
-
st.success(f"
|
| 102 |
|
| 103 |
-
st.
|
|
|
|
| 104 |
|
| 105 |
execs = requests.get("http://localhost:8000/executions/").json()
|
| 106 |
if execs:
|
| 107 |
exec_ids = [str(e["id"]) + f" - {os.path.basename(e['file'])}" for e in execs]
|
| 108 |
-
selected = st.selectbox("
|
| 109 |
selected_id = int(selected.split(" - ")[0])
|
| 110 |
|
| 111 |
col1, col2 = st.columns(2)
|
| 112 |
with col1:
|
| 113 |
-
if st.button("
|
| 114 |
requests.post(f"http://localhost:8000/stop/{selected_id}")
|
| 115 |
|
| 116 |
with col2:
|
| 117 |
-
if st.button("
|
| 118 |
-
st.subheader(f"
|
| 119 |
log_box = st.empty()
|
| 120 |
status_box = st.empty()
|
| 121 |
|
| 122 |
while True:
|
| 123 |
status = requests.get(f"http://localhost:8000/status/{selected_id}").json()
|
| 124 |
-
status_box.markdown(f"
|
| 125 |
logs = requests.get(f"http://localhost:8000/log/{selected_id}").json()
|
| 126 |
log_box.code(logs["log"])
|
| 127 |
if status["status"] != "running":
|
| 128 |
break
|
| 129 |
-
show_system_stats()
|
| 130 |
time.sleep(2)
|
| 131 |
else:
|
| 132 |
-
st.info("
|
| 133 |
|
| 134 |
-
# ------------------
|
| 135 |
if __name__ == "__main__":
|
| 136 |
def start_api():
|
| 137 |
subprocess.run(["uvicorn", "app:app", "--port", "8000", "--reload"])
|
|
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
import threading
|
|
|
|
| 4 |
import uuid
|
| 5 |
+
import time
|
| 6 |
from fastapi import FastAPI, UploadFile, File
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
import streamlit as st
|
|
|
|
| 13 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 14 |
os.makedirs(LOG_DIR, exist_ok=True)
|
| 15 |
|
| 16 |
+
# مدیریت اجرای فایلها
|
| 17 |
executions = {}
|
| 18 |
exec_counter = 0
|
| 19 |
|
|
|
|
| 76 |
def list_execs():
|
| 77 |
return [{"id": k, "file": v["file"], "status": v["status"]} for k, v in executions.items()]
|
| 78 |
|
| 79 |
+
# ------------------ Streamlit ------------------ #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
def streamlit_ui():
|
| 81 |
+
st.set_page_config("اجرای فایل پایتون", layout="centered")
|
| 82 |
+
st.title("آپلود و اجرای فایل پایتون")
|
| 83 |
|
| 84 |
+
file = st.file_uploader("فایل پایتون را انتخاب کنید", type=["py"])
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
if file and st.button("آپلود و اجرا"):
|
| 87 |
res = requests.post("http://localhost:8000/upload/", files={"file": file})
|
| 88 |
exec_id = res.json()["exec_id"]
|
| 89 |
+
st.success(f"اجرا با آیدی {exec_id} شروع شد")
|
| 90 |
|
| 91 |
+
st.divider()
|
| 92 |
+
st.subheader("فایلهای اجرا شده")
|
| 93 |
|
| 94 |
execs = requests.get("http://localhost:8000/executions/").json()
|
| 95 |
if execs:
|
| 96 |
exec_ids = [str(e["id"]) + f" - {os.path.basename(e['file'])}" for e in execs]
|
| 97 |
+
selected = st.selectbox("انتخاب اجرا برای مشاهده:", exec_ids)
|
| 98 |
selected_id = int(selected.split(" - ")[0])
|
| 99 |
|
| 100 |
col1, col2 = st.columns(2)
|
| 101 |
with col1:
|
| 102 |
+
if st.button("توقف اجرا", key="stop"):
|
| 103 |
requests.post(f"http://localhost:8000/stop/{selected_id}")
|
| 104 |
|
| 105 |
with col2:
|
| 106 |
+
if st.button("نمایش لاگ زنده", key="log"):
|
| 107 |
+
st.subheader(f"لاگ اجرای {selected_id}")
|
| 108 |
log_box = st.empty()
|
| 109 |
status_box = st.empty()
|
| 110 |
|
| 111 |
while True:
|
| 112 |
status = requests.get(f"http://localhost:8000/status/{selected_id}").json()
|
| 113 |
+
status_box.markdown(f"**وضعیت:** {status['status']}")
|
| 114 |
logs = requests.get(f"http://localhost:8000/log/{selected_id}").json()
|
| 115 |
log_box.code(logs["log"])
|
| 116 |
if status["status"] != "running":
|
| 117 |
break
|
|
|
|
| 118 |
time.sleep(2)
|
| 119 |
else:
|
| 120 |
+
st.info("فعلاً هیچ اجرایی انجام نشده.")
|
| 121 |
|
| 122 |
+
# ------------------ اجرا ------------------ #
|
| 123 |
if __name__ == "__main__":
|
| 124 |
def start_api():
|
| 125 |
subprocess.run(["uvicorn", "app:app", "--port", "8000", "--reload"])
|