Rafactor the code. Add one line docstrings and change the names to be more appropriate…
- User
Rafactor the code. Add one line docstrings and change the names to be more appropriate (the code does not actually support multisort). Lets think step by step. Python code: from tksheet import Sheet import tkinter as tk
class MultiSortSheet(tk.Tk): def init(self): tk.Tk.init(self) self.title("Multi-Sort tksheet Example")
self.data = [ ["Apple", 10, 2.5], ["Banana", 5, 1.0], ["Apple", 15, 3.0], ["Orange", 8, 2.0], ["Banana", 12, 2.8], ["Apple", 20, 4.0], ["Orange", 10, 2.5], ] self.headers = ["Fruit", "Quantity", "Price"] self.sort_columns = [] self.sheet = Sheet(self, data=self.data, headers=self.headers) self.sheet.enable_bindings("all") self.sheet.bind("<Button-1>", self.sort_column) self.sheet.pack(expand=True, fill="both") self.sheet.bind("<<SheetModified>>", self.handle_sheet_modified) def sort_column(self, event): if self.sheet.identify_region(event) != "header": return col_index = self.sheet.identify_column(event) if col_index is None: return new_sort_columns = [] ascending = True for col, asc in self.sort_columns: if col == col_index: ascending = not asc else: new_sort_columns.append((col, asc)) new_sort_columns.insert(0, (col_index, ascending)) self.sort_columns = new_sort_columns self.multi_sort(self.sort_columns) self.sheet.set_sheet_data(self.data) def multi_sort(self, sort_columns): def sort_key(row): return tuple(self.get_sortable_value(row[col]) for col, asc in sort_columns) self.data.sort(key=sort_key, reverse=not sort_columns[0][1]) def get_sortable_value(self, value): return str(value) # Convert everything to a string def handle_sheet_modified(self, event): if "added" in event and "columns" in event["added"] and event["added"]["columns"]: self.sheet.bind("<Button-1>", self.sort_column)if name == "main": app = MultiSortSheet() app.mainloop()
- Assistant
- User
instead of the example data use the data in the following json file. D:\Bilder\Python\Database\Data.json
- Assistant
- User
Traceback (most recent call last): File "D:\Bilder\Python\Database\Database 75 - Kopie (2).py", line 78, in app = SortableSheetApp(json_file_path) File "D:\Bilder\Python\Database\Database 75 - Kopie (2).py", line 14, in init self.headers = self.data[0] # Assuming the first row contains headers ~~~~~~~~~^^^ KeyError: 0
- Assistant
- User
of Superman', '2018', 'Yes', '', ''], ['All-Star Superman', '2011', 'No', 'Yes', ''], ['Act Of Valor', '2012', 'Yes', '', ''], ['Iron Monkey', '1993', 'Yes', '', ''], ['Police Story', '1985', 'No', 'Yes', ''], ['Police Story 2', '1988', 'Yes', '', ''], ['Twister', '1996', 'Yes', '', ''], ['UHF', '1989', 'No', 'Yes', ''], ['Under Suspicion', '2000', 'Yes', '', ''], ['Despicable Me 3', '2017', 'Yes', '', ''], ['Hush', '2016', 'Yes', '', ''], ['Blink Twice', '2024', 'Yes', '', ''], ['Despicable Me 4', '2024', 'Yes', '', ''], ['IF', '2024', 'Yes', '', ''], ['Kind of Kindness', '2024', 'Yes', '', ''], ['Longlegs', '2024', 'Yes', '', ''], ['Rebel Ridge', '2024', 'Yes', '', ''], ['Speak No Evil', '2024', 'Yes', '', ''], ['The Wild Robot', '2024', 'Yes', '', ''], ['Thelma', '2024', 'Yes', '', ''], ['Wicked Little Letters', '2024', 'Yes', '', ''], ['Wolfs', '2024', 'Yes', '', ''], ['Alien: Romulus', '2024', 'Yes', '', ''], ['Bad Boys Ride or Die', '2024', 'Yes', '', ''], ['Beetlejuice Beetlejuice', '2024', 'Yes', '', ''], ['Horizon An American Saga: Chapter 1', '2024', 'Yes', '', ''], ['Inside Out 2', '2024', 'Yes', '', ''], ['Kalki 2898 AD', '2024', 'Yes', '', ''], ['Strange Darling', '2024', 'Yes', '', ''], ['Jawan', '2023', 'Yes', '', ''], ['Am I Racist', '2024', 'No', 'Yes', ''], ['What is a Woman', '2022', 'No', 'Yes', '']], 'Table 147': [['ID', 'Name', 'Active'], [1, 'Item 1', True], [2, 'Item 2', False], [3, 'Item 3', True]], 'Table 148': [['ID', 'Name', 'Active'], [1, 'Item 1', True], [2, 'Item 2', False], [3, 'Item 3', True]], 'Table 149': [['Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5'], ['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', '']], 'Table 150': [['Column 1', 'Column 2', 'Column 3', 'Active', 'Column 5'], ['', '', '', 'True', ''], ['', '', '', 'True', ''], [True, True, True, True, True], ['tttrr', '', '', '', ''], ['t', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', '']]} The JSON structure is incorrect. Expected a list of lists
- Assistant