Skip to content

Add support for per-column custom comparators#13973

Merged
bryevdv merged 15 commits intobranch-3.6from
bv/13893-custom-table-sort
Jul 25, 2024
Merged

Add support for per-column custom comparators#13973
bryevdv merged 15 commits intobranch-3.6from
bv/13893-custom-table-sort

Conversation

@bryevdv
Copy link
Copy Markdown
Member

@bryevdv bryevdv commented Jul 9, 2024

@bokeh/dev This would still needs tests, docs, and examples, but I wanted to get some early feedback on the approach, which seems to work. I am also sure the TypeScript will needs improvements, and I am also not sure how if at all the current "default" sorter code needs to be considered (i.e. whatever it was trying to do with nans already seem to not be desired, can it just be jettisoned?)

Here is a test script based on the OP:

from bokeh.io import show
from bokeh.models import ColumnDataSource, DataTable, TableColumn, CustomJSCompare

source = ColumnDataSource(data=dict(foo=["AB 1", "AB 10", "AB 2"]))

sorter = CustomJSCompare(code="""
    const xn = Number(x.split(" ")[1])
    const yn = Number(y.split(" ")[1])
    if (xn == yn) { return 0 }
    else if (xn < yn) { return -1 }
    else return 1
""")
columns = [
    TableColumn(field="foo", title="Foo", sorter=sorter),
]
data_table = DataTable(source=source, columns=columns, width=400, height=280)

show(data_table)

With results for:

  • Ascending

    Screenshot 2024-07-09 at 16 45 41
  • Descending
    Screenshot 2024-07-09 at 16 45 37

@bryevdv bryevdv added this to the 3.6 milestone Jul 9, 2024
Comment on lines +27 to +35
protected compute(x: any, y: any): number {
if (isNaN(x)) {
return this.ascending_first ? -1 : 1
}
if (isNaN(y)) {
return this.ascending_first ? 1 : -1
}
return x==y ? 0 : x < y ? -1 : 1
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isNaN() can somewhat work in place of isNumber(), but it's finicky, and only as long as any type is involved on the type-level. Given the inputs to this method can be anything, I would change the types to unknown and add isNumber() guards to make sure isNaN() has actual numbers to work with.

Copy link
Copy Markdown
Member Author

@bryevdv bryevdv Jul 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this change but it also limited the comparison in general to numeric types, I could have imagined sorting nans first or last relative to non-numeric types in case someone used nan as "missing". Currently returning 0 for non-numeric types, but perhaps an error is also an option?

@bryevdv
Copy link
Copy Markdown
Member Author

bryevdv commented Jul 11, 2024

@mattpap we are not currently using DataView correct? Does that seem like something worth pursuing in the future, or did you intend to look at a new/different table implementation entirely at some point?

Copy link
Copy Markdown
Contributor

@mosc9575 mosc9575 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments on the docstrings to make sure they won't be missed.

@bryevdv
Copy link
Copy Markdown
Member Author

bryevdv commented Jul 14, 2024

@bokeh/dev thoughts on what level of docs support this needs? Will definitely add basic reference guide materials. Some other possibilities:

  • Add a comparision to some table example or examples (which ones?)
  • Add a dedicated user guide sub-section on comparisons (where?)
  • Combination of the above...

@mattpap any guidance on BokehJS tests? Can you point me towards something analogous that I can use as a basis to emulate?

@bryevdv
Copy link
Copy Markdown
Member Author

bryevdv commented Jul 17, 2024

I've added basic ref docs. Absent feedback about other docs or tests I am marking this ready.

@mattpap
Copy link
Copy Markdown
Contributor

mattpap commented Jul 19, 2024

we are not currently using DataView correct? Does that seem like something worth pursuing in the future, or did you intend to look at a new/different table implementation entirely at some point?

Apparently not. I don't have any plans for data tables besides updating SlickGrid to a newer version (PR #13522).

@mattpap
Copy link
Copy Markdown
Contributor

mattpap commented Jul 19, 2024

Here is a test script based on the OP: (...)

I would turn this sample code into an example under examples/interaction/widgets.

@mattpap
Copy link
Copy Markdown
Contributor

mattpap commented Jul 19, 2024

any guidance on BokehJS tests? Can you point me towards something analogous that I can use as a basis to emulate?

There's a single good test that can be a basis for this:

it("should allow sorting a non-selectable table", async () => {
const source = new ColumnDataSource({data: {foo: [10, 20, 30, 40], bar: [3.4, 1.2, 0, -10]}})
const foo_col = new TableColumn({field: "foo", title: "Foo", width: 200})
const bar_col = new TableColumn({field: "bar", title: "Bar", width: 200})
const columns = [foo_col, bar_col]
const table = new DataTable({source, columns, sortable: true, selectable: false})
const {view} = await display(table, [600, 400])
const el = view.shadow_el.querySelectorAll(".slick-header-column")[2]
await mouse_click(el)
await view.ready

Put the construction code in a function, configure with NanSorter and CustomJSCompare, then have a test with an image captured with default sorting and another after a click on a header. That should be cheap and easy to cover the basics.

bryevdv and others added 8 commits July 20, 2024 10:53
Co-authored-by: Mateusz Paprocki <mattpap@gmail.com>
Co-authored-by: Moritz Schreiber <68053396+mosc9575@users.noreply.github.com>
Co-authored-by: Mateusz Paprocki <mattpap@gmail.com>
@bryevdv bryevdv force-pushed the bv/13893-custom-table-sort branch from dca102d to 4d03eec Compare July 20, 2024 18:21
@bryevdv
Copy link
Copy Markdown
Member Author

bryevdv commented Jul 20, 2024

Apparently not. I don't have any plans for data tables besides updating SlickGrid to a newer version (PR #13522).

I'll make an issue soon. It's probably something worth looking into at some point so that we can jettison our adhoc homegrown view

@bryevdv bryevdv force-pushed the bv/13893-custom-table-sort branch from 4d03eec to 87ee9cb Compare July 20, 2024 18:35
@bryevdv bryevdv requested review from mattpap and mosc9575 July 20, 2024 19:33
Co-authored-by: Moritz Schreiber <68053396+mosc9575@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

@mattpap mattpap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

bryevdv and others added 2 commits July 25, 2024 08:08
Co-authored-by: Mateusz Paprocki <mattpap@gmail.com>
@bryevdv bryevdv merged commit 6404785 into branch-3.6 Jul 25, 2024
@bryevdv bryevdv deleted the bv/13893-custom-table-sort branch July 25, 2024 16:11
Chiemezuo pushed a commit to Chiemezuo/bokeh that referenced this pull request Aug 27, 2024
* Add support for per-column custom comparators

* Apply suggestions from code review

Co-authored-by: Mateusz Paprocki <mattpap@gmail.com>

* review comments

* fix defaults tests

* fix js tests

* Apply suggestions from code review

Co-authored-by: Moritz Schreiber <68053396+mosc9575@users.noreply.github.com>

* add basic ref docs

* Apply suggestions from code review

Co-authored-by: Mateusz Paprocki <mattpap@gmail.com>

* NanSorter -> NanCompare

* add example

* bokehjs integration test

* update baselines

* Apply suggestions from code review

Co-authored-by: Moritz Schreiber <68053396+mosc9575@users.noreply.github.com>

* Update bokehjs/test/integration/tables.ts

Co-authored-by: Mateusz Paprocki <mattpap@gmail.com>

* lint

---------

Co-authored-by: Mateusz Paprocki <mattpap@gmail.com>
Co-authored-by: Moritz Schreiber <68053396+mosc9575@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown

github-actions bot commented Nov 2, 2024

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Custom sorting of columns in a table [FEATURE] Option for DataTable column sorting to place NaNs last

3 participants