Skip to content

Commit b390d64

Browse files
committed
feat: added new functions (saving measurement history)
1 parent 8d93820 commit b390d64

File tree

1 file changed

+89
-25
lines changed

1 file changed

+89
-25
lines changed

alexs_speedtest.py

Lines changed: 89 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@
44
from tkinter import ttk
55
import speedtest as st
66
import network_adapter_information
7+
import os
8+
from test_history import save_test_results, view_history, plot_history
9+
10+
11+
# Get the history file path in the Documents folder
12+
def get_history_path():
13+
"""Returns the path to the history file in the Documents folder."""
14+
home = os.path.expanduser("~")
15+
documents_path = os.path.join(home, "Documents")
16+
os.makedirs(documents_path, exist_ok=True) # Creates the folder if it doesn't exist
17+
return os.path.join(documents_path, "test_history.json")
18+
19+
20+
# Path to the history file
21+
history_path = get_history_path()
722

823

924
def update_progress(value, message):
@@ -15,35 +30,43 @@ def update_progress(value, message):
1530

1631
def perform_speedtest():
1732
"""Performs the speed test and updates the UI with results."""
18-
test = st.Speedtest()
19-
result_container = {}
33+
try:
34+
test = st.Speedtest()
35+
result_container = {}
36+
37+
# Download speed test
38+
update_progress(0, "Starting download speed test...")
39+
download_thread = threading.Thread(target=test_download_speed,
40+
args=(test, result_container))
41+
download_thread.start()
42+
download_thread.join() # Wait for the download test to finish
2043

21-
# Download speed test
22-
update_progress(0, "Starting download speed test...")
23-
download_thread = threading.Thread(target=test_download_speed, args=(test, result_container))
24-
download_thread.start()
25-
download_thread.join() # Wait for the download test to finish
44+
if 'error' in result_container:
45+
raise ValueError(result_container['error'])
2646

27-
if 'error' in result_container:
28-
messagebox.showerror("Error", result_container['error'])
29-
return
47+
download_speed = result_container['download']
3048

31-
download_speed = result_container['download']
49+
# Upload speed test
50+
update_progress(0, "Starting upload speed test...")
51+
upload_thread = threading.Thread(target=test_upload_speed, args=(test, result_container))
52+
upload_thread.start()
53+
upload_thread.join() # Wait for the upload test to finish
3254

33-
# Upload speed test
34-
update_progress(0, "Starting upload speed test...")
35-
upload_thread = threading.Thread(target=test_upload_speed, args=(test, result_container))
36-
upload_thread.start()
37-
upload_thread.join() # Wait for the upload test to finish
55+
if 'error' in result_container:
56+
raise ValueError(result_container['error'])
3857

39-
if 'error' in result_container:
40-
messagebox.showerror("Error", result_container['error'])
41-
return
58+
upload_speed = result_container['upload']
59+
ping = test.results.ping
4260

43-
upload_speed = result_container['upload']
44-
ping = test.results.ping
61+
display_results(download_speed, upload_speed, ping)
4562

46-
display_results(download_speed, upload_speed, ping)
63+
except st.ConfigRetrievalError as e:
64+
# Catch speedtest specific errors and show in UI
65+
messagebox.showerror("Speedtest Error",
66+
f"Error retrieving configuration: {e}")
67+
except Exception as e:
68+
# Catch any other exceptions and show in UI
69+
messagebox.showerror("Error", f"An error occurred: {e}")
4770

4871

4972
def test_download_speed(test, result_container):
@@ -92,16 +115,29 @@ def display_results(down_speed, up_speed, ping):
92115
result_label.config(text=result_text)
93116
update_progress(100, "Test complete!") # Set progress to 100%
94117

118+
# Hide the progress bar after the test is complete
119+
progress_label.pack_forget()
120+
progress_bar.pack_forget()
121+
122+
# Save results to history
123+
save_test_results(down_speed, up_speed, ping, history_path)
124+
95125
# Show "Repeat Speed Test" button and hide the exit button
96126
repeat_button.pack(pady=10)
97127

128+
# Show history buttons after the test is complete
129+
history_frame.pack(pady=10)
130+
98131

99132
def start_speedtest():
100133
"""Starts the speed test when the button is pressed."""
101134
result_label.config(text="Running speed test...")
102135
start_button.pack_forget() # Hide the start button
103136
repeat_button.pack_forget() # Hide the repeat button if it was visible
104137

138+
# Hide history buttons when repeating the test
139+
history_frame.pack_forget()
140+
105141
# Show progress bar
106142
progress_label.pack(pady=10)
107143
progress_bar.pack(pady=20)
@@ -128,6 +164,18 @@ def display_system_info():
128164
system_info_label.config(text=info_text)
129165

130166

167+
def repeat_speedtest():
168+
"""Repeats the speed test."""
169+
result_label.config(text="Running speed test again...")
170+
history_frame.pack_forget() # Hide history buttons before repeating the test
171+
172+
# Hide the progress bar before repeating the test
173+
progress_label.pack_forget()
174+
progress_bar.pack_forget()
175+
176+
start_speedtest() # Restart the test
177+
178+
131179
def exit_program():
132180
"""Closes the program."""
133181
root.quit()
@@ -138,13 +186,18 @@ def exit_program():
138186
root.title("Internet Speed Test")
139187
root.geometry("400x450")
140188

189+
# Label to display system information
190+
system_info_label = tk.Label(root, text="", justify="left", anchor="w")
191+
system_info_label.pack(pady=10, padx=20)
192+
141193
# Button to start speed test
142194
start_button = tk.Button(root, text="Start Speed Test", command=start_speedtest)
143195
start_button.pack(pady=20)
144196

145197
# Button to repeat speed test
146198
repeat_button = tk.Button(root, text="Repeat Speed Test", command=start_speedtest)
147199
repeat_button.pack_forget() # Initially hidden
200+
repeat_button.config(command=repeat_speedtest) # Connect the new function to the button
148201

149202
# Label to display results
150203
result_label = tk.Label(root, text="")
@@ -158,9 +211,20 @@ def exit_program():
158211
progress_bar = ttk.Progressbar(root, length=300, mode='determinate')
159212
# Initially, progress is not visible
160213

161-
# Label to display system information
162-
system_info_label = tk.Label(root, text="", justify="left", anchor="w")
163-
system_info_label.pack(pady=10, padx=20)
214+
# Create frame for history buttons
215+
history_frame = tk.Frame(root)
216+
217+
# Button to view history
218+
history_button = tk.Button(history_frame, text="View Test History table",
219+
command=lambda: view_history(root, history_path))
220+
history_button.pack(side="left", padx=10)
221+
222+
plot_button = tk.Button(history_frame, text="Show graph Test History",
223+
command=lambda: plot_history(root, history_path))
224+
plot_button.pack(side="left", padx=10)
225+
226+
# Hide the history frame until the test is complete
227+
history_frame.pack_forget()
164228

165229
# Exit button
166230
exit_button = tk.Button(root, text="Exit", command=exit_program)

0 commit comments

Comments
 (0)