-
Notifications
You must be signed in to change notification settings - Fork 2
/
web.py
178 lines (148 loc) · 6.11 KB
/
web.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""
streamlit run web.py --server.port 8000
"""
import datetime
from datetime import date
import pandas as pd
import plotly.express as px
import requests
import streamlit as st
from utils.config import load_config
config = load_config()
class TaskAPIClient:
def __init__(self, base_url: str):
self.base_url = base_url.rstrip("/")
def create_task(self, name: str) -> requests.Response:
url = f"{self.base_url}/v1/tasks"
return requests.post(url, json={"name": name})
def get_task_status(self, task_id: str) -> requests.Response:
url = f"{self.base_url}/v1/tasks/{task_id}"
return requests.get(url)
def get_task_list(self, task_date: date) -> requests.Response:
url = f"{self.base_url}/v1/tasks/list/{task_date}"
return requests.get(url)
def cancel_task(self, task_id: str) -> requests.Response:
url = f"{self.base_url}/v1/tasks/{task_id}/cancel"
return requests.post(url)
def get_queue_status(self) -> requests.Response:
url = f"{self.base_url}/v1/tasks/queue/status"
return requests.get(url)
def init_session_state():
if "authenticated" not in st.session_state:
st.session_state.authenticated = False
if "current_task" not in st.session_state:
st.session_state.current_task = None
def format_task_data(tasks: list) -> pd.DataFrame:
if not tasks:
return pd.DataFrame()
df = pd.DataFrame(tasks)
df["create_time"] = pd.to_datetime(df["create_time"]).dt.strftime(
"%Y-%m-%d %H:%M:%S"
)
return df
def render_task_status(status: str) -> str:
colors = {
"pending": "blue",
"running": "orange",
"completed": "green",
"failed": "red",
"timeout": "gray",
}
return f":{colors.get(status, 'black')}[{status}]"
def main():
st.set_page_config(page_title="Task Management System", layout="wide")
init_session_state()
if not st.session_state.authenticated:
password = st.text_input(
"Please input the passwrd", type="password", placeholder="Enter Password"
)
if st.button("Login"):
if password == ADMIN_PASSWORD:
st.session_state.authenticated = True
st.rerun()
else:
st.error("Incorrect password!")
return
tab1, tab2, tab3 = st.tabs(["Task List", "Create Task", "Task Details"])
with tab1:
st.subheader("Task List")
col1, col2 = st.columns([2, 1])
with col1:
task_date = st.date_input("Select Date", datetime.date.today())
with col2:
if st.button("Refresh"):
st.rerun()
if task_date:
response = api_client.get_task_list(task_date)
if response.status_code == 200:
df = format_task_data(response.json())
if not df.empty:
st.dataframe(df, use_container_width=True)
fig = px.pie(df, names="status", title="Task Status Distribution")
st.plotly_chart(fig)
else:
st.info("No task records for the selected date")
else:
st.error("Failed to retrieve task list")
with tab2:
st.subheader("Create New Task")
with st.form("create_task_form"):
task_name = st.text_input("Task Name")
submitted = st.form_submit_button("Create Task")
if submitted and task_name:
response = api_client.create_task(task_name)
if response.status_code == 200:
st.success(f"Task created successfully!")
data = response.json()
st.json(data)
st.session_state.current_task = data["id"]
else:
st.error("Failed to create task")
with tab3:
st.subheader("Task Details")
task_id = st.text_input("Task ID", value=st.session_state.current_task or "")
col1, col2 = st.columns([1, 1])
with col1:
if st.button("Check Status"):
if task_id:
response = api_client.get_task_status(task_id)
if response.status_code == 200:
task_data = response.json()
st.markdown("### Task Information")
st.markdown(
f"- **Status**: {render_task_status(task_data['status'])}"
)
st.markdown(f"- **Created Time**: {task_data['create_time']}")
if task_data["result"]:
st.markdown("### Task Result")
if task_data["status"] == "completed":
st.markdown("### Task Result")
st.video(task_data["result"])
with open(task_data["result"], "rb") as f:
st.download_button(
label="Download Video",
data=f,
file_name=f"{task_data['id']}.mp4",
mime="video/mp4",
)
else:
st.markdown(f"```{task_data['result']}```")
else:
st.error("Failed to retrieve task status")
else:
st.warning("Please enter a Task ID")
with col2:
if st.button("Cancel Task"):
if task_id:
response = api_client.cancel_task(task_id)
if response.status_code == 200:
st.success("Task has been canceled")
else:
st.error("Failed to cancel task")
else:
st.warning("Please enter a Task ID")
base_url = f"http://localhost:{config['api']['app_port']}"
api_client = TaskAPIClient(base_url)
ADMIN_PASSWORD = config["web"]["password"]
if __name__ == "__main__":
main()