Skip to content

Commit 3dd23e7

Browse files
committed
update csvplotter.py so that now it does not create error if any csv file contains metadata as well.
1 parent 2dcf279 commit 3dd23e7

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

csvplotter.py

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import tkinter as tk
1+
import tkinter as tk
22
from tkinter import filedialog, messagebox, ttk
33
import pandas as pd
44
import plotly.graph_objects as go
5-
from plotly.subplots import make_subplots
65

76
class CSVPlotterApp:
87
def __init__(self, root):
@@ -46,26 +45,58 @@ def load_csv(self):
4645
self.filename = filedialog.askopenfilename(filetypes=[("CSV files", "*.csv")])
4746
if self.filename:
4847
try:
49-
self.data = pd.read_csv(self.filename) # Load the CSV file
50-
self.setup_dropdown_menu()
51-
self.file_label.config(text=f"File: {self.filename.split('/')[-1]}")
48+
# Read file with pandas, skipping metadata if any
49+
self.data = self.load_csv_data(self.filename)
50+
51+
if self.data is not None:
52+
self.setup_dropdown_menu()
53+
self.file_label.config(text=f"File: {self.filename.split('/')[-1]}")
54+
else:
55+
messagebox.showerror("Error", "CSV does not contain the expected columns.")
56+
5257
except Exception as e:
5358
messagebox.showerror("Error", f"Could not load CSV file: {e}")
5459

60+
def load_csv_data(self, file_path):
61+
# Open the CSV file and find the line with headers
62+
with open(file_path, 'r') as file:
63+
lines = file.readlines()
64+
65+
# Look for the line containing the headers
66+
headers = ['Counter', 'Channel1', 'Channel2', 'Channel3', 'Channel4', 'Channel5', 'Channel6']
67+
header_line_index = None
68+
for i, line in enumerate(lines):
69+
if all(header in line for header in headers):
70+
header_line_index = i
71+
break
72+
73+
if header_line_index is None:
74+
return None # No header found, return None to indicate error
75+
76+
# Load the CSV data starting from the header line
77+
data = pd.read_csv(file_path, skiprows=header_line_index)
78+
79+
# Ensure the required columns exist
80+
if all(col in data.columns for col in headers):
81+
return data
82+
else:
83+
return None # Return None if required columns are missing
84+
5585
def setup_dropdown_menu(self):
56-
# Populate dropdown menu with the CSV column names (channels)
86+
# Populate dropdown menu with the CSV channel columns (ignore Timestamp and Counter)
5787
columns = list(self.data.columns)
58-
self.dropdown_menu['values'] = columns
59-
if columns:
60-
self.channel_selection.set(columns[0]) # Default selection
88+
channel_columns = [col for col in columns if 'Channel' in col]
89+
self.dropdown_menu['values'] = channel_columns
90+
if channel_columns:
91+
self.channel_selection.set(channel_columns[0]) # Default selection
6192

6293
def plot_data(self):
6394
selected_channel = self.channel_selection.get() # Get the selected channel
6495
if not selected_channel:
6596
messagebox.showerror("Error", "No channel selected for plotting")
6697
return
6798

68-
fig = go.Figure() # Plot the selected channel using Plotly
99+
fig = go.Figure() # Plot the selected channel using Plotly
69100
fig.add_trace(go.Scatter(x=self.data.index, y=self.data[selected_channel], mode='lines', name=selected_channel))
70101

71102
fig.update_layout(

0 commit comments

Comments
 (0)