4
4
import plotly .graph_objects as go
5
5
6
6
class CSVPlotterApp :
7
- def __init__ (self , root ):
7
+ def __init__ (self , root ): # Initialize the main application window
8
8
self .root = root
9
9
self .root .title ("CSV Plotter GUI" )
10
- self .filename = None
11
- self .data = None
12
- self .create_widgets ()
10
+ self .filename = None # Variable to store the selected CSV file name
11
+ self .data = None # Variable to store the loaded data
12
+ self .create_widgets () # Call the method to create widgets
13
13
14
14
def create_widgets (self ):
15
15
# Create a frame for buttons and file name display
@@ -41,19 +41,19 @@ def create_widgets(self):
41
41
self .plot_button .pack (pady = 10 )
42
42
43
43
def load_csv (self ):
44
- self .filename = filedialog .askopenfilename (filetypes = [("CSV files" , "*.csv" )])
44
+ self .filename = filedialog .askopenfilename (filetypes = [("CSV files" , "*.csv" )]) # Open file dialog to select CSV file
45
45
if self .filename :
46
46
try :
47
- with open (self .filename , "r" , encoding = "utf-8" ) as f :
48
- lines = f .readlines ()
47
+ with open (self .filename , "r" , encoding = "utf-8" ) as f : # Open the selected CSV file
48
+ lines = f .readlines () # Read all lines into a list for header detection
49
49
50
- header_index = None # Find the row where 'Counter' appears
51
- for i , line in enumerate (lines ):
50
+ header_index = None # Initialize the variable to track where (Header) 'Counter' appears
51
+ for i , line in enumerate (lines ): # Iterate through lines to find the header line
52
52
if "Counter" in line :
53
- header_index = i
54
- break
53
+ header_index = i # Store the index of the line containing 'Counter'
54
+ break # If found, break the loop
55
55
56
- if header_index is None :
56
+ if header_index is None : # If no header row with 'Counter' was found, show error and exit
57
57
messagebox .showerror ("Error" , "CSV file must contain a 'Counter' column." )
58
58
return
59
59
@@ -73,7 +73,7 @@ def load_csv(self):
73
73
messagebox .showerror ("Error" , f"Could not load CSV file: { e } " )
74
74
75
75
def setup_dropdown_menu (self ):
76
- # Get available channel columns (Channel1 to Channel6)
76
+ # Get available channel columns
77
77
channel_columns = [col for col in self .data .columns if 'Channel' in col ]
78
78
79
79
# Populate dropdown menu with available channels
@@ -82,6 +82,7 @@ def setup_dropdown_menu(self):
82
82
self .channel_selection .set (channel_columns [0 ]) # Default selection to the first channel
83
83
84
84
def plot_data (self ):
85
+ """Creates an interactive plot of the selected data channel using Plotly."""
85
86
selected_channel = self .channel_selection .get () # Get the selected channel
86
87
if not selected_channel :
87
88
messagebox .showerror ("Error" , "No channel selected for plotting" )
@@ -96,9 +97,9 @@ def plot_data(self):
96
97
yaxis_title = "Value" ,
97
98
template = "plotly_white"
98
99
)
99
- fig .show ()
100
+ fig .show () # Display the plot in a new window
100
101
101
102
if __name__ == "__main__" :
102
- root = tk .Tk ()
103
- app = CSVPlotterApp (root )
104
- root .mainloop ()
103
+ root = tk .Tk () # Create the main Tkinter root window
104
+ app = CSVPlotterApp (root ) # Create an instance of the CSVPlotterApp class
105
+ root .mainloop () # Start the Tkinter main loop
0 commit comments