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

Add simple nested editor to Tabulator #7251

Merged
merged 7 commits into from
Sep 12, 2024
Merged

Conversation

hoxbro
Copy link
Member

@hoxbro hoxbro commented Sep 10, 2024

Screencast.2024-09-10.18.57.55.mp4
import pandas as pd
import panel as pn

pn.extension("tabulator")

df = pd.DataFrame({"0": ["A", "B", "A"], "1": [1, 2, 3], "2": [None, None, None]})

options = {
    "A": [1, 2, 3, 4, 5],
    "B": {
        "1": list(range(5, 10)),
        "2": list(range(10, 15)),
        "3": list(range(15, 20)),
    },
}
tabulator_editors = {
    "0": {"type": "list", "values": ["A", "B"]},
    "1": {"type": "list", "values": [1, 2, 3]},
    "2": {"type": "nested", "options": options, "lookup_order": ["0", "1"]},
}

def on_edit(event):
    if event.column in ("0", "1"):
        edit_table.patch({"2": [(event.row, None)]})

edit_table = pn.widgets.Tabulator(df, editors=tabulator_editors, show_index=False, on_edit=on_edit)

edit_table

@hoxbro hoxbro marked this pull request as draft September 10, 2024 17:02
Copy link

codecov bot commented Sep 10, 2024

Codecov Report

Attention: Patch coverage is 41.66667% with 14 lines in your changes missing coverage. Please review.

Project coverage is 82.23%. Comparing base (0453e9c) to head (81389c8).
Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
panel/tests/ui/widgets/test_tabulator.py 41.66% 14 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7251      +/-   ##
==========================================
- Coverage   82.23%   82.23%   -0.01%     
==========================================
  Files         337      337              
  Lines       50251    50317      +66     
==========================================
+ Hits        41325    41379      +54     
- Misses       8926     8938      +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hoxbro hoxbro marked this pull request as ready for review September 12, 2024 08:16
@hoxbro hoxbro changed the title Add simple nestedEditor for tabulator Add simple nested editor to Tabulator Sep 12, 2024
const row = cell.getRow().getData()
let values = editorParams.options
for (const i of editorParams.lookup_order) {
values = values[row[i]]
Copy link
Member

Choose a reason for hiding this comment

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

What happens if there is no entry here? Does it handle it gracefully?

Copy link
Member Author

Choose a reason for hiding this comment

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

It handled it by not showing up anything. I have pushed a change so it now will show:

image

(not using the same example as the post)

Copy link
Member

@philippjfr philippjfr left a comment

Choose a reason for hiding this comment

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

Asked on clarifying question, otherwise this looks great!

@philippjfr philippjfr merged commit c5e3125 into main Sep 12, 2024
16 checks passed
@philippjfr philippjfr deleted the nested_editor_tabulator branch September 12, 2024 10:06
@MarcSkovMadsen
Copy link
Collaborator

MarcSkovMadsen commented Sep 19, 2024

This is an alternative example in an attempt to make it more clear which columns contains the nested selections and how the options depends on the selections in other columns. Its also more verbose, so its not clear to me if I should contribute this as and improvement to the docs. Thus I just persist it here for now.

nested-selections

nested-selections.mp4
import pandas as pd
import panel as pn

pn.extension("tabulator")

df = pd.DataFrame({"0": ["A", "B", "A"], "1": [1, 2, 3], "Nested Selection": [None, None, None]})

options = {
    "A": ["A.1", "A.2", "A.3", "A.4", "A.5"],
    "B": {
        "1": ["B1.1", "B1.2", "B1.3"],
        "2": ["B2.1", "B2.2", "B2.3"],
        "3": ["B3.1", "B3.2", "B3.3"],
    },
}
tabulator_editors = {
    "0": {"type": "list", "values": ["A", "B"]},
    "1": {"type": "list", "values": [1, 2, 3]},
    "Nested Selection": {"type": "nested", "options": options, "lookup_order": ["0", "1"]},
}


def on_edit(event):
    if event.column in ("0", "1"):
        edit_table.patch({"Nested Selection": [(event.row, None)]})


edit_table = pn.widgets.Tabulator(df, editors=tabulator_editors, show_index=False, on_edit=on_edit)

edit_table.servable()

@hoxbro
Copy link
Member Author

hoxbro commented Sep 19, 2024

I think it is a better example, so you are welcome to open a PR with your suggested changes.

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

Successfully merging this pull request may close these issues.

3 participants