iasjkk commited on
Commit
e264886
·
verified ·
1 Parent(s): cf9249e

Update ui.py

Browse files
Files changed (1) hide show
  1. ui.py +27 -15
ui.py CHANGED
@@ -13,10 +13,14 @@ def get_system_metrics():
13
 
14
  # Function to get internet speed
15
  def get_internet_speed():
16
- st = speedtest.Speedtest()
17
- download_speed = st.download() / 1_000_000 # Convert to Mbps
18
- upload_speed = st.upload() / 1_000_000 # Convert to Mbps
19
- return download_speed, upload_speed
 
 
 
 
20
 
21
  # Function to create a gauge chart
22
  def create_gauge_chart(value, title, min_value=0, max_value=100):
@@ -34,9 +38,11 @@ def create_gauge_chart(value, title, min_value=0, max_value=100):
34
  # Streamlit layout
35
  st.title("System Monitoring Dashboard")
36
 
37
- # Create columns for each gauge
38
- col1, col2 = st.columns(2)
39
- col3, col4 = st.columns(2)
 
 
40
 
41
  # Update the metrics in real-time
42
  while True:
@@ -44,14 +50,20 @@ while True:
44
  download_speed, upload_speed = get_internet_speed()
45
 
46
  # Create gauge charts
47
- with col1:
48
  st.plotly_chart(create_gauge_chart(cpu_usage, "CPU Usage"), use_container_width=True)
49
- with col2:
50
  st.plotly_chart(create_gauge_chart(memory_usage, "Memory Usage"), use_container_width=True)
51
- with col3:
52
- st.plotly_chart(create_gauge_chart(download_speed, "Download Speed (Mbps)", 0, 1000), use_container_width=True)
53
- with col4:
54
- st.plotly_chart(create_gauge_chart(upload_speed, "Upload Speed (Mbps)", 0, 1000), use_container_width=True)
 
 
 
 
 
 
55
 
56
- # Refresh the metrics every 5 seconds
57
- time.sleep(5)
 
13
 
14
  # Function to get internet speed
15
  def get_internet_speed():
16
+ try:
17
+ st = speedtest.Speedtest()
18
+ st.get_best_server()
19
+ download_speed = st.download() / 1_000_000 # Convert to Mbps
20
+ upload_speed = st.upload() / 1_000_000 # Convert to Mbps
21
+ return download_speed, upload_speed
22
+ except:
23
+ return None, None
24
 
25
  # Function to create a gauge chart
26
  def create_gauge_chart(value, title, min_value=0, max_value=100):
 
38
  # Streamlit layout
39
  st.title("System Monitoring Dashboard")
40
 
41
+ # Create placeholders for each gauge
42
+ cpu_placeholder = st.empty()
43
+ memory_placeholder = st.empty()
44
+ download_placeholder = st.empty()
45
+ upload_placeholder = st.empty()
46
 
47
  # Update the metrics in real-time
48
  while True:
 
50
  download_speed, upload_speed = get_internet_speed()
51
 
52
  # Create gauge charts
53
+ with cpu_placeholder:
54
  st.plotly_chart(create_gauge_chart(cpu_usage, "CPU Usage"), use_container_width=True)
55
+ with memory_placeholder:
56
  st.plotly_chart(create_gauge_chart(memory_usage, "Memory Usage"), use_container_width=True)
57
+ with download_placeholder:
58
+ if download_speed is not None:
59
+ st.plotly_chart(create_gauge_chart(download_speed, "Download Speed (Mbps)", 0, 1000), use_container_width=True)
60
+ else:
61
+ st.write("Error retrieving download speed")
62
+ with upload_placeholder:
63
+ if upload_speed is not None:
64
+ st.plotly_chart(create_gauge_chart(upload_speed, "Upload Speed (Mbps)", 0, 1000), use_container_width=True)
65
+ else:
66
+ st.write("Error retrieving upload speed")
67
 
68
+ # Refresh the metrics every 10 seconds
69
+ time.sleep(10)