1+ import tkinter as tk
2+ from tkinter import filedialog , messagebox
3+ import pandas as pd
4+ import os
5+
6+ class CSVAnnotatorApp :
7+ def __init__ (self , root ):
8+ self .root = root
9+ self .root .title ("CSV Data Annotator" )
10+ self .root .geometry ("800x600" )
11+
12+ self .csv_file_path = ""
13+ self .df = None
14+
15+ self .upload_button = tk .Button (root , text = "Upload CSV" , command = self .upload_csv )
16+ self .upload_button .pack (pady = 20 )
17+
18+ self .text_widget = tk .Text (root , height = 20 , width = 100 )
19+ self .text_widget .pack (pady = 20 )
20+
21+ self .annotate_button = tk .Button (root , text = "Add Annotation" , command = self .add_annotation )
22+ self .annotate_button .pack (pady = 10 )
23+
24+ self .save_button = tk .Button (root , text = "Save Annotated CSV" , command = self .save_csv )
25+ self .save_button .pack (pady = 20 )
26+
27+ def upload_csv (self ):
28+ file_path = filedialog .askopenfilename (filetypes = [("CSV Files" , "*.csv" )])
29+ if file_path :
30+ self .csv_file_path = file_path
31+ self .df = pd .read_csv (self .csv_file_path )
32+ self .display_csv_content ()
33+
34+ def display_csv_content (self ):
35+ if self .df is not None :
36+ self .text_widget .delete (1.0 , tk .END )
37+ self .text_widget .insert (tk .END , self .df .to_string ())
38+
39+ def add_annotation (self ):
40+ if self .df is not None :
41+ row_idx = int (self .simpledialog ("Enter Row Index to Annotate" ))
42+ column_name = self .simpledialog ("Enter Column Name to Annotate" )
43+ annotation = self .simpledialog ("Enter Annotation" )
44+ if row_idx in self .df .index and column_name in self .df .columns :
45+ self .df .at [row_idx , column_name ] = annotation
46+ self .display_csv_content ()
47+ else :
48+ messagebox .showerror ("Error" , "Invalid row index or column name." )
49+
50+ def simpledialog (self , prompt ):
51+ return tk .simpledialog .askstring ("Input" , prompt )
52+
53+ def save_csv (self ):
54+ if self .df is not None :
55+ save_path = filedialog .asksaveasfilename (defaultextension = ".csv" , filetypes = [("CSV Files" , "*.csv" )])
56+ if save_path :
57+ self .df .to_csv (save_path , index = False )
58+ messagebox .showinfo ("Success" , f"Annotated CSV saved to { save_path } " )
59+
60+ if __name__ == "__main__" :
61+ root = tk .Tk ()
62+ app = CSVAnnotatorApp (root )
63+ root .mainloop ()
0 commit comments