-
Notifications
You must be signed in to change notification settings - Fork 815
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
Sort method inDataTable
doesn't support rich.Text
types
#2261
Comments
My original thought (from the PR linked above):
@darrenburns might be better suited to determine if this makes sense. |
A |
@willmcgugan adding them to |
This is somewhat related to issue Textualize#2261
This is somewhat related to issue Textualize#2261
This is somewhat related to issue Textualize#2261
The `DataTable` widget now takes the `by` argument instead of `columns`, allowing the table to also be sorted using a custom function (or other callable). This is a breaking change since it requires all calls to the `sort` method to include an iterable of key(s) (or a singular function/callable). Covers Textualize#2261 using [suggested function signature](Textualize#2512 (comment)) from @darrenburns on PR Textualize#2512.
Only having the ordering functions on the Adding a One possible solution to the above problem is to allow conveying the values and their representation separately. Then, either use the raw values for ordering, or also allow a custom function to extract the sort keys. Meanwhile, for a workaround I extend the @functools.total_ordering
class Sortable(Text):
"""Like rich.text.Text, but with ordering based on a raw value"""
def __init__(self, value, *args, **kwargs):
self.value = value
super().__init__(*args, **kwargs)
def __lt__(self, other: "Sortable") -> bool:
return self.value < other.value
def __eq__(self, other: "Sortable") -> bool:
return self.value == other.value and later use it like so: def watch_xyz(self, ...):
...
self.table.add_row(
...
Sortable(file_size, naturalsize(file_size), ...),
...
) It allows customizing the ordering logic in any way necessary by changing the |
I'm assuming you've looked at #3090, could you give an example where this would not work? |
As far as I understand the example from above still holds, but please let me know if I am missing something. If I understand the change well, the new |
In the sort routine you can always un-humanize the numbers. You might get some round-off errors if you don't have enough precision in your human readable numbers, but in general works pretty well. (I do this in my app today with a DataTable and a custom sort() function.) |
The |
I would very much like to supply my own callback to the |
* DataTable sort by function (or other callable) The `DataTable` widget now takes the `by` argument instead of `columns`, allowing the table to also be sorted using a custom function (or other callable). This is a breaking change since it requires all calls to the `sort` method to include an iterable of key(s) (or a singular function/callable). Covers #2261 using [suggested function signature](#2512 (comment)) from @darrenburns on PR #2512. * argument change and functionaloty update Changed back to orinal `columns` argument and added a new `key` argument which takes a function (or other callable). This allows the PR to NOT BE a breaking change. * better example for docs - Updated the example file for the docs to better show the functionality of the change (especially when using `columns` and `key` together). - Added one new tests to cover a similar situation to the example changes * removed unecessary code from example - the sort by clicked column function was bloat in my opinion * requested changes * simplify method and terminology * combine key_wrapper and default sort * Removing some tests from DataTable.sort as duplicates. Ensure there is test coverage of the case where a key, but no columns, is passed to DataTable.sort. * Remove unused import * Fix merge issues in CHANGELOG, update DataTable sort-by-key changelog PR link --------- Co-authored-by: Darren Burns <[email protected]> Co-authored-by: Darren Burns <[email protected]>
I stumbled across this open issue looking for something else. You can now sort your DataTable with a custom function (or other callable) in Textual v0.41.0 - see https://textual.textualize.io/widgets/data_table/#sorting |
Thanks @TomJGooding, looks like we can close this issue now. |
Don't forget to star the repository! Follow @textualizeio for Textual updates. |
Coming from #2243.
The
sort
method inDataTable
doesn't supportrich.Text
types stored in the cells. When that is done aTypeError: '<' not supported between instances of 'Text' and 'Text'
error is thrown becauserich.Text
have no rich comparison operators defined.cc @rodrigogiraoserrao
The text was updated successfully, but these errors were encountered: