Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REQUEST : Add a sort function #238

Open
AnonymousVibrate opened this issue Jun 19, 2024 · 8 comments
Open

REQUEST : Add a sort function #238

AnonymousVibrate opened this issue Jun 19, 2024 · 8 comments
Labels
enhancement New feature or request

Comments

@AnonymousVibrate
Copy link

AnonymousVibrate commented Jun 19, 2024

Can we add a functionality that sort the column when you clicked the header ?

import pandas as pd
from tksheet import Sheet
from customtkinter import CTk
from tkinter import TOP, BOTH, X

data = pd.DataFrame(['Banana', 'Apple', 'Carrots', 'Pumpkin', 'Watermelon', 'Coconut', 'Strawberry'])

def custom_sheet():
    sheet = Sheet(app, data=data.values.tolist())
    sheet.pack(side=TOP, fill=BOTH, expand=True)
    sheet.enable_bindings()
    sheet.headers(['Sort this column']) #add additional settings to enable header clickable right side arrow to sort values.

app = CTk()
custom_sheet()
app.mainloop()
@AnonymousVibrate AnonymousVibrate closed this as not planned Won't fix, can't repro, duplicate, stale Jun 30, 2024
@TweetleDee916
Copy link

Id like to add this as well. Maybe at least start with a straight forward way to sort a sheet by a single column in the sheet. That could then be exposed/used without a custom approach every time a user wants to sort a sheet. Something similar could be done for filtering a sheet. using just a single column initially

@ragardner ragardner reopened this Feb 6, 2025
@ragardner ragardner added the enhancement New feature or request label Feb 6, 2025
@ragardner
Copy link
Owner

I am looking into this due to the number of requests, if you have any further tips or advice let me know!

@TweetleDee916
Copy link

TweetleDee916 commented Feb 6, 2025 via email

@AnonymousVibrate
Copy link
Author

AnonymousVibrate commented Feb 13, 2025

I am looking into this due to the number of requests, if you have any further tips or advice let me know!

Hey, I somehow managed to do a sorting of data in tksheet:

import tkinter as tk
import pandas as pd
from tksheet import Sheet
from tkinter import messagebox

# Sample data
data = [
    ["Alice", "Smith", "Math", 20],
    ["Bob", "Johnson", "Science", 18],
    ["Charlie", "Brown", "History", 22],
    ["David", "Wilson", "Math", 19],
    ["Emma", "Moore", "Science", 21]
]
columns = ["First Name", "Last Name", "Course", "Age"]

# Track sorting order
sort_order = {}

def sort_table(col):
    # Get the index of the selected column
    selected_column_index = col['selected'].column
    headers = sheet.headers()
    selected_header = headers[selected_column_index]

    answer = messagebox.askyesno(f"Confirmation", f"Sort by {selected_header}?", parent= root)

    if answer:
        # Retrieve all data from the table
        full_data = sheet.get_sheet_data()

        # Convert the full data to a DataFrame for easier sorting
        df_full = pd.DataFrame(full_data, columns=headers)

        # Sort the full DataFrame by the selected column
        sorted_df = df_full.sort_values(by=selected_header, ascending=True)

        # Update the table with the sorted data
        sheet.set_sheet_data(sorted_df.values.tolist())
        
        #Enable bindings
        sheet.enable_bindings("single_select",
                                    "row_select",
                                    "column_width_resize",
                                    "column_select",
                                    "copy",
                                    "up",
                                    "down",
                                    )
        # Refresh the table to apply changes
        sheet.refresh()

# Create Tkinter window
root = tk.Tk()
root.title("TkSheet Sorting Example")
root.geometry("600x400")

# Create tksheet
sheet = Sheet(root, data=data, headers=columns)
sheet.enable_bindings("all")
sheet.pack(fill="both", expand=True)

# Bind click event for sorting when column headers are clicked
sheet.extra_bindings('column_select', sort_table)

# Run Tkinter main loop
root.mainloop()

@ragardner
Copy link
Owner

Hello, sorting is coming soon I am just finishing the latest version which will hopefully be released within the next 5 days, changes include:

  • make the treeview have full table functionality including drag and drop, insert rows, delete rows, undo, redo and sorting
  • cell text wrapping, character or word
  • Sheet.sorting functions which accept a key (default is a natural sorting key which has the aim of try to handle any python object) and reverse arguments, including:
    • sorting a box of cells
    • sorting values in rows
    • sorting values in columns
    • re-ordering rows based on the values of a column
    • re-ordering columns based on the values of a row

If you can think of any other sorting functions you would like to see let me know

@TweetleDee916
Copy link

TweetleDee916 commented Feb 14, 2025 via email

ragardner added a commit that referenced this issue Feb 18, 2025
#### Changed:
- Text now wraps by character by default, can also disable wrapping or wrap by word
- Significant changes to how text is rendered
- Removed mousewheel scrolling lines in header, replaced with vertical axis table scroll
- Resizing row height to text is now based on the existing column width for the cell/cells, includes double click resizing
- Treeview mode `Node` class now uses `str` for parent and `list[str]` for children attributes
- Function `get_nodes()` renamed -> `get_iids()`
- Removed `data_indexes` parameter from `mapping_move...` functions
- Reduce default treeview indent
- `move()` function now returns the same as other move rows functions
- All `Sheet()` functions with an `undo` parameter have been set to `True` by default
- Using `Sheet.set_data()` or `Span`s to set an individual cell's data as `None` will now do so instead of returning
- Setting `show_top_left` during Sheet() initialization will now make the top left rectangle always show

#### Added:
- Natural sorting functionality [#238](#238)
- Treeview mode now works with all normal tksheet functions, including dragging and dropping rows
- Cell text overflow `allow_cell_overflow: bool = False` to adjacent cells for left and right alignments, disabled by default
- Text wrap for table, header and index
```python
# "" no wrap, "w" word wrap, "c" char wrap
table_wrap: Literal["", "w", "c"] = "c",
index_wrap: Literal["", "w", "c"] = "c",
header_wrap: Literal["", "w", "c"] = "c",
```

- `tree` parameter to `insert_rows()` function, used internally

#### Fixed:
- Index fonts now work correctly
- Functions `column_width()` and `row_height()` work correctly for any parameters
- Down sizing rows/columns when scrolled to the end of the axis would result in a rapid movement of row height/column width
- Address [#269](#269)

#### Improved:
- Minor performance improvements for:
    - `item_displayed()`
    - `show_rows()` / `show_columns()`
    - `move()`
    - Row insertion
@ragardner
Copy link
Owner

Hello,

Sorting functionality has now been added in version 7.4.0 along with many other changes

https://github.com/ragardner/tksheet/wiki/Version-7#sorting-the-table

Let me know if you think there should be adjustments or additional functionality 😊

@ragardner
Copy link
Owner

ragardner commented Feb 19, 2025

The latest version is now 7.4.2, some issues with sorting have been ironed out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants