|
| 1 | +import sys |
| 2 | +from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QPushButton, QFileDialog, QWidget, QInputDialog |
| 3 | +import pandas as pd |
| 4 | +import matplotlib.pyplot as plt |
| 5 | + |
| 6 | +class CSVViewerApp(QMainWindow): |
| 7 | + def __init__(self): |
| 8 | + super().__init__() |
| 9 | + self.initUI() |
| 10 | + |
| 11 | + def initUI(self): |
| 12 | + self.setWindowTitle('CSV Viewer') |
| 13 | + self.setGeometry(100, 100, 800, 600) |
| 14 | + |
| 15 | + layout = QVBoxLayout() |
| 16 | + |
| 17 | + self.load_btn = QPushButton('Load CSV') |
| 18 | + self.load_btn.clicked.connect(self.load_csv) |
| 19 | + layout.addWidget(self.load_btn) |
| 20 | + |
| 21 | + self.plot_btn = QPushButton('Plot Data') |
| 22 | + self.plot_btn.clicked.connect(self.show_plot_dialog) |
| 23 | + layout.addWidget(self.plot_btn) |
| 24 | + |
| 25 | + self.central_widget = QWidget() |
| 26 | + self.central_widget.setLayout(layout) |
| 27 | + self.setCentralWidget(self.central_widget) |
| 28 | + |
| 29 | + def load_csv(self): |
| 30 | + file_name, _ = QFileDialog.getOpenFileName(self, "Open CSV File", "", "CSV Files (*.csv);;All Files (*)") |
| 31 | + if file_name: |
| 32 | + self.df = pd.read_csv(file_name) |
| 33 | + |
| 34 | + def plot_data(self, columns): |
| 35 | + if hasattr(self, 'df'): |
| 36 | + self.df[columns].plot(kind='bar') |
| 37 | + plt.title(f'CSV Data Visualization') |
| 38 | + plt.xlabel('X-axis Label') |
| 39 | + plt.ylabel('Y-axis Label') |
| 40 | + plt.legend(columns) |
| 41 | + plt.show() |
| 42 | + |
| 43 | + def show_plot_dialog(self): |
| 44 | + if hasattr(self, 'df'): |
| 45 | + columns, ok = QInputDialog.getText(self, "Column Selection", "Enter the column names or indices (comma-separated) to plot:") |
| 46 | + if ok: |
| 47 | + try: |
| 48 | + column_indices = [int(idx) for idx in columns.split(",")] |
| 49 | + except ValueError: |
| 50 | + column_indices = columns.split(",") |
| 51 | + |
| 52 | + selected_columns = [] |
| 53 | + for idx in column_indices: |
| 54 | + if isinstance(idx, int): |
| 55 | + selected_columns.append(self.df.columns[idx]) |
| 56 | + else: |
| 57 | + selected_columns.append(idx.strip()) |
| 58 | + |
| 59 | + valid_columns = [col for col in selected_columns if col in self.df.columns] |
| 60 | + |
| 61 | + if valid_columns: |
| 62 | + self.plot_data(valid_columns) |
| 63 | + else: |
| 64 | + print("Invalid column names or indices.") |
| 65 | + |
| 66 | +if __name__ == '__main__': |
| 67 | + app = QApplication(sys.argv) |
| 68 | + window = CSVViewerApp() |
| 69 | + window.show() |
| 70 | + sys.exit(app.exec()) |
0 commit comments