-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.py
More file actions
80 lines (70 loc) · 2.36 KB
/
Copy pathapp.py
File metadata and controls
80 lines (70 loc) · 2.36 KB
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
import multiprocessing
import pandas as pd
import platform
import psutil
import subprocess
import sys
from shiny import App, reactive, render, ui
from urllib.parse import urlparse
app_ui = ui.page_fluid(
ui.h2("System details:"),
ui.output_table("system"),
ui.h2("Request details:"),
ui.output_text_verbatim("request_output"),
ui.input_text_area("cmd", "Command to run", placeholder="Enter text"),
ui.output_text_verbatim("cmd_output"),
ui.input_text_area("logme", "Text to log", placeholder="Enter text"),
ui.input_checkbox("stderr", "log to stderr", False),
ui.input_action_button("log_button", "Log"),
ui.output_text_verbatim("logged"),
)
def server(input, output, session):
@output
@render.table
def system():
host_total = psutil.virtual_memory().total
host_mem = f"{int(host_total / 1024 / 1024 / 1024)} GiB ({host_total} bytes)"
cpu_max = run(["cat", "/sys/fs/cgroup/cpu.max"])
parts = cpu_max.split()
if len(parts) == 2:
cpu_limit = int(parts[0]) / int(parts[1])
else:
cpu_limit = cpu_max
memory_max=int(run(["cat", "/sys/fs/cgroup/memory.max"]))
pod_mem = f"{int(memory_max / 1024 / 1024 / 1024)} GiB ({memory_max} bytes)"
return pd.DataFrame([
{"name":"python version","value":platform.python_version()},
{"name":"host cpu count","value":multiprocessing.cpu_count()},
{"name":"host memory","value":host_mem},
{"name":"cpu limit","value":cpu_limit},
{"name":"memory limit","value":pod_mem},
])
@output
@render.text
def cmd_output():
cmd=input.cmd()
try:
return subprocess.check_output(cmd, shell=True).decode()
except Exception as e:
return f"Error: {e}"
@output
@render.text
def request_output():
search = session.input[".clientdata_url_search"]()
return urlparse(search)
@output
@render.text()
@reactive.event(input.log_button)
def logged():
l = input.logme()
if input.stderr():
print(l, file=sys.stderr)
else:
print(l)
return l
app = App(app_ui, server)
def run(input: list[str]) -> str:
try:
return subprocess.check_output(input).decode("utf-8").strip()
except Exception as e:
return f"Error: {e}"