Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,11 +2,12 @@ import streamlit as st
|
|
| 2 |
import speedtest
|
| 3 |
import time
|
| 4 |
import pandas as pd
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
st.set_page_config(page_title="Internet Speed Test", layout="centered"
|
| 8 |
|
| 9 |
-
# Custom
|
| 10 |
st.markdown("""
|
| 11 |
<style>
|
| 12 |
body { background-color: #111827; color: #f9fafb; }
|
|
@@ -15,50 +16,66 @@ st.markdown("""
|
|
| 15 |
""", unsafe_allow_html=True)
|
| 16 |
|
| 17 |
st.title("πΆ Internet Speed Test")
|
| 18 |
-
st.caption("
|
| 19 |
|
| 20 |
-
#
|
| 21 |
if "history" not in st.session_state:
|
| 22 |
-
st.session_state
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
if st.button("βΆοΈ Start Speed Test"):
|
| 26 |
with st.spinner("Running test... please wait..."):
|
| 27 |
try:
|
| 28 |
-
|
| 29 |
-
best = stt.get_best_server()
|
| 30 |
-
|
| 31 |
-
start = time.time()
|
| 32 |
-
download = stt.download()
|
| 33 |
-
upload = stt.upload()
|
| 34 |
-
end = time.time()
|
| 35 |
-
|
| 36 |
-
download_mbps = round(download / 1_000_000, 2)
|
| 37 |
-
upload_mbps = round(upload / 1_000_000, 2)
|
| 38 |
-
test_time_sec = round(end - start, 2)
|
| 39 |
-
|
| 40 |
-
# Add to history
|
| 41 |
-
result = {
|
| 42 |
-
"Download (Mbps)": download_mbps,
|
| 43 |
-
"Upload (Mbps)": upload_mbps,
|
| 44 |
-
"Time (s)": test_time_sec,
|
| 45 |
-
"ISP": best['sponsor'],
|
| 46 |
-
"Server": f"{best['name']}, {best['country']}"
|
| 47 |
-
}
|
| 48 |
-
st.session_state.history.append(result)
|
| 49 |
-
|
| 50 |
-
# Show results
|
| 51 |
-
st.success("β
Test complete!")
|
| 52 |
-
st.markdown(f"**π₯ Download:** `{download_mbps} Mbps`")
|
| 53 |
-
st.markdown(f"**π€ Upload :** `{upload_mbps} Mbps`")
|
| 54 |
-
st.markdown(f"**β±οΈ Time :** `{test_time_sec} sec`")
|
| 55 |
-
st.markdown(f"**π’ ISP :** `{best['sponsor']}`")
|
| 56 |
-
st.markdown(f"**π Server :** `{best['name']}, {best['country']}`")
|
| 57 |
-
|
| 58 |
except Exception as e:
|
| 59 |
st.error(f"β Speed test failed: {e}")
|
| 60 |
|
| 61 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
if st.session_state.history:
|
| 63 |
st.markdown("---")
|
| 64 |
st.subheader("π Test History")
|
|
@@ -67,4 +84,4 @@ if st.session_state.history:
|
|
| 67 |
st.line_chart(df[["Download (Mbps)", "Upload (Mbps)"]])
|
| 68 |
|
| 69 |
with st.expander("π See all results"):
|
| 70 |
-
st.dataframe(df, use_container_width=True)
|
|
|
|
| 2 |
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 |
""", 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()
|
| 35 |
+
|
| 36 |
+
start = time.time()
|
| 37 |
+
download = stt.download()
|
| 38 |
+
upload = stt.upload()
|
| 39 |
+
end = time.time()
|
| 40 |
+
|
| 41 |
+
download_mbps = round(download / 1_000_000, 2)
|
| 42 |
+
upload_mbps = round(upload / 1_000_000, 2)
|
| 43 |
+
test_time_sec = round(end - start, 2)
|
| 44 |
+
|
| 45 |
+
result = {
|
| 46 |
+
"Timestamp": datetime.now().strftime("%H:%M:%S"),
|
| 47 |
+
"Download (Mbps)": download_mbps,
|
| 48 |
+
"Upload (Mbps)": upload_mbps,
|
| 49 |
+
"Time (s)": test_time_sec,
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
st.session_state.history.append(result)
|
| 53 |
+
st.session_state.last_test = time.time()
|
| 54 |
+
|
| 55 |
+
st.success("β
Test complete!")
|
| 56 |
+
st.markdown(f"**π₯ Download:** `{download_mbps} Mbps`")
|
| 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:
|
| 64 |
+
run_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:
|
| 72 |
+
with st.spinner(f"Auto-retesting every {interval} minutes..."):
|
| 73 |
+
try:
|
| 74 |
+
run_speed_test()
|
| 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 |
st.line_chart(df[["Download (Mbps)", "Upload (Mbps)"]])
|
| 85 |
|
| 86 |
with st.expander("π See all results"):
|
| 87 |
+
st.dataframe(df, use_container_width=True)
|