waqasbm commited on
Commit
52253d3
Β·
verified Β·
1 Parent(s): f508a3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -10
app.py CHANGED
@@ -3,11 +3,12 @@ import speedtest
3
  import time
4
  import pandas as pd
5
  from datetime import datetime
 
6
 
7
- # UI setup
8
  st.set_page_config(page_title="Internet Speed Test", layout="centered")
9
 
10
- # Custom dark mode styling
11
  st.markdown("""
12
  <style>
13
  body { background-color: #111827; color: #f9fafb; }
@@ -16,19 +17,32 @@ st.markdown("""
16
  """, unsafe_allow_html=True)
17
 
18
  st.title("πŸ“Ά Internet Speed Test")
19
- st.caption("Test your download/upload speed with history and auto-retest option.")
20
 
21
- # Session state to store history & timing
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  if "history" not in st.session_state:
23
  st.session_state.history = []
24
  if "last_test" not in st.session_state:
25
  st.session_state.last_test = 0
26
 
27
- # Set retest interval (in minutes)
28
  interval = st.selectbox("⏱️ Retest every (minutes)", [0, 1, 5, 10, 15, 30, 60], index=0)
29
  auto_retest = interval > 0
30
 
31
- # Perform test
32
  def run_speed_test():
33
  stt = speedtest.Speedtest()
34
  stt.get_best_server()
@@ -57,7 +71,7 @@ def run_speed_test():
57
  st.markdown(f"**πŸ“€ Upload :** `{upload_mbps} Mbps`")
58
  st.markdown(f"**⏱️ Time :** `{test_time_sec} sec`")
59
 
60
- # Manual test
61
  if st.button("▢️ Start Speed Test"):
62
  with st.spinner("Running test... please wait..."):
63
  try:
@@ -65,7 +79,7 @@ if st.button("▢️ Start Speed Test"):
65
  except Exception as e:
66
  st.error(f"❌ Speed test failed: {e}")
67
 
68
- # Auto retest logic (based on interval)
69
  if auto_retest:
70
  now = time.time()
71
  if now - st.session_state.last_test > interval * 60:
@@ -75,7 +89,7 @@ if auto_retest:
75
  except Exception as e:
76
  st.error(f"❌ Auto test failed: {e}")
77
 
78
- # Display history if available
79
  if st.session_state.history:
80
  st.markdown("---")
81
  st.subheader("πŸ“Š Test History")
@@ -84,4 +98,4 @@ if st.session_state.history:
84
  st.line_chart(df[["Download (Mbps)", "Upload (Mbps)"]])
85
 
86
  with st.expander("πŸ“‹ See all results"):
87
- st.dataframe(df, use_container_width=True)
 
3
  import time
4
  import pandas as pd
5
  from datetime import datetime
6
+ import requests
7
 
8
+ # ========== PAGE SETUP ==========
9
  st.set_page_config(page_title="Internet Speed Test", layout="centered")
10
 
11
+ # Custom dark theme
12
  st.markdown("""
13
  <style>
14
  body { background-color: #111827; color: #f9fafb; }
 
17
  """, unsafe_allow_html=True)
18
 
19
  st.title("πŸ“Ά Internet Speed Test")
 
20
 
21
+ # ========== GET PUBLIC IP ==========
22
+ def get_public_ip():
23
+ try:
24
+ response = requests.get("https://api.ipify.org?format=json", timeout=5)
25
+ if response.status_code == 200:
26
+ return response.json()["ip"]
27
+ else:
28
+ return "Unavailable"
29
+ except Exception:
30
+ return "Unavailable"
31
+
32
+ public_ip = get_public_ip()
33
+ st.markdown(f"🌍 **Your Public IP:** `{public_ip}`")
34
+
35
+ # ========== SESSION SETUP ==========
36
  if "history" not in st.session_state:
37
  st.session_state.history = []
38
  if "last_test" not in st.session_state:
39
  st.session_state.last_test = 0
40
 
41
+ # ========== RETEST INTERVAL ==========
42
  interval = st.selectbox("⏱️ Retest every (minutes)", [0, 1, 5, 10, 15, 30, 60], index=0)
43
  auto_retest = interval > 0
44
 
45
+ # ========== SPEED TEST FUNCTION ==========
46
  def run_speed_test():
47
  stt = speedtest.Speedtest()
48
  stt.get_best_server()
 
71
  st.markdown(f"**πŸ“€ Upload :** `{upload_mbps} Mbps`")
72
  st.markdown(f"**⏱️ Time :** `{test_time_sec} sec`")
73
 
74
+ # ========== MANUAL TEST BUTTON ==========
75
  if st.button("▢️ Start Speed Test"):
76
  with st.spinner("Running test... please wait..."):
77
  try:
 
79
  except Exception as e:
80
  st.error(f"❌ Speed test failed: {e}")
81
 
82
+ # ========== AUTO RETEST ==========
83
  if auto_retest:
84
  now = time.time()
85
  if now - st.session_state.last_test > interval * 60:
 
89
  except Exception as e:
90
  st.error(f"❌ Auto test failed: {e}")
91
 
92
+ # ========== SHOW HISTORY ==========
93
  if st.session_state.history:
94
  st.markdown("---")
95
  st.subheader("πŸ“Š Test History")
 
98
  st.line_chart(df[["Download (Mbps)", "Upload (Mbps)"]])
99
 
100
  with st.expander("πŸ“‹ See all results"):
101
+ st.dataframe(df, use_container_width=True)