Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Dev-iL
Copy link
Contributor

@Dev-iL Dev-iL commented Jun 20, 2024

Describe your changes

The purpose of these changes is that a user selection of one or more cells but not an entire row/col would trigger the DataFrame's on_select method, returning the selection "rectangle".

Screencast.from.26-06-24.12.57.27.webm
Demo app (python)
from functools import partial

import pandas as pd

import streamlit as st


def process_single_selection(table):
    selection = st.session_state["SS_DF"]["selection"]
    if selection is None:
        st.warning("Nothing was single-selected.")
    row = selection["rows"][0]
    col = selection["columns"][0]

    st.info(f"Selected cell at [{row},{col}]: {table.at[row, col]}")


def process_multi_selection(table: pd.DataFrame):
    selection = st.session_state["MS_DF"]["selection"]
    if selection is None:
        st.warning("Nothing was multi-selected.")
    rows = selection["rows"]
    cols = selection["columns"]

    st.info(f"Selected range: [{rows=}, {cols=}], with contents:\n{table.loc[rows, cols].to_markdown()}")


def main():
    df = pd.DataFrame({
        "A": [1, 2, 3],
        "B": [4, 5, 6],
        "C": [7, 8, 9]
    })
    clbl_s = partial(process_single_selection, table=df)
    clbl_m = partial(process_multi_selection, table=df)

    st.subheader("Single-selection demo")
    st.dataframe(df, selection_mode="single-cell", on_select=clbl_s, key="SS_DF")
    st.subheader("Multi-selection demo")
    st.dataframe(df, selection_mode="multi-cell", on_select=clbl_m, key="MS_DF")

    st.markdown(
        """
        <style>
        [data-testid="stElementToolbar"] {
            display: none;
        }
        </style>
        """,
        unsafe_allow_html=True
    )


main()

GitHub Issue Link (if applicable)

#6781

Testing Plan

  • playwright: add two new dataframes to st_dataframe_selections.py with the new selection mode + the associated unit tests.
  • react: add tests for the new modes to useSelectionHandler.
  • arrow: add tests for the new modes + tests for invalid selection mode combinations.

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@Dev-iL
Copy link
Contributor Author

Dev-iL commented Jun 20, 2024

Dear maintainers,
I am very interested in this feature, and even created a workaround using existing capabilities (see issue). The present state of the code is "compiling correctly but untested otherwise". Unfortunately, I am not familiar enough with the inner workings of Streamlit to finish this, or even to judge if what I did makes sense. I hope to either get some feedback to direct me to what else needs doing or be the motivation and foundation for a "proper" implementation by someone else.

@Dev-iL Dev-iL changed the title Add single- and multi-cell selection options to st.DataFrame [Help Wanted] Add single- and multi-cell selection options to st.DataFrame Jun 21, 2024
@Dev-iL Dev-iL force-pushed the Dev-iL/feature/dataframe-cell-selection branch 2 times, most recently from 2e645ca to eb1753e Compare June 26, 2024 09:59
@Dev-iL Dev-iL marked this pull request as ready for review June 26, 2024 10:06
@Dev-iL Dev-iL requested a review from a team as a code owner June 26, 2024 10:06
@Dev-iL
Copy link
Contributor Author

Dev-iL commented Jun 26, 2024

Please suggest how to add tests for this functionality.

@Dev-iL Dev-iL changed the title [Help Wanted] Add single- and multi-cell selection options to st.DataFrame Add single- and multi-cell selection options to st.DataFrame Jun 26, 2024
@Dev-iL Dev-iL force-pushed the Dev-iL/feature/dataframe-cell-selection branch from eb1753e to 7a44838 Compare June 27, 2024 13:28
@lukasmasuch
Copy link
Collaborator

lukasmasuch commented Jun 27, 2024

@Dev-iL Thanks a lot for the contribution! Implementation-wise, this already looks promising. However, adding this feature will require some internal discussions and decisions. I will share this with our product team. One potentially disappointing outcome might be that we will close the PR for now.

We actively decided against adding cell selection in the initial version of dataframe selections since the cell selection event is already overloaded with other functionality (e.g., opening cell details, copy/pasting, editing, etc.). We are closely monitoring #6781 to see how interest in this feature evolves.

lib/setup.py Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is the reason for this change? Did you run into a problem if this is not specified?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, couldn't pip install on Windows otherwise. I think I added some info in the commit where this change was added.

@Dev-iL
Copy link
Contributor Author

Dev-iL commented Jun 27, 2024

We actively decided against adding cell selection in the initial version of dataframe selections since the cell selection event is already overloaded with other functionality (e.g., opening cell details, copy/pasting, editing, etc.). We are closely monitoring #6781 to see how interest in this feature evolves.

@lukasmasuch Thank you for your reply. Interestingly, my rather simple use case requires none of that other functionality, whereas cell selection is critical. I even created a workaround using very ugly tornado hacks that achieves roughly the same thing (linked in the issue).

Might I suggest adding a setting somewhere (perhaps to the df component) that allows a user to choose between cell selection and the other interactions you mentioned?

@Dev-iL Dev-iL force-pushed the Dev-iL/feature/dataframe-cell-selection branch 3 times, most recently from 15128d6 to 0b2a581 Compare July 3, 2024 22:15
@Dev-iL Dev-iL force-pushed the Dev-iL/feature/dataframe-cell-selection branch 2 times, most recently from effb6e9 to 6bd8be6 Compare July 9, 2024 11:10
@jrieke
Copy link
Collaborator

jrieke commented Jul 15, 2024

Hey @Dev-iL, sorry for the long delay, we had a few vacations on the team. Lukas is still out of the office, but I'll talk with him about your PR when he's back this week. Unfortunately, as Lukas pointed out, there are a few UX inconsistencies we'd first want to clean up before adding cell selections (also added a longer comment to the issue here), so I think it's quite likely we'll close this PR for now. I know it's a bit frustrating for your current use case but having an amazing user experience is one of our primary goals for Streamlit. I also don't think we'd want to add a (temporary) parameter to switch between the behaviors, since that seems a bit hacky and unfinished.

@Dev-iL
Copy link
Contributor Author

Dev-iL commented Jul 15, 2024

Hi @jrieke, thank you for your clarifications. I respectfully disagree that knowing where one clicked in a table goes against "an amazing user experience". I'd say that adding advanced functionality (such as putting interactive charts in cells, or viewing cell details) while skipping the basics is what leads to a suboptimal experience. Having said that, I'm sure your company has considerably more experience designing UX than a lone developer, and one would assume that you guys could come up with a better solution than a parameter, if you wanted. Maybe one of those experiment controls?

BTW, why the urgency to close the PR? Can't you simply ignore it until you're ready to include the feature?

@Dev-iL Dev-iL force-pushed the Dev-iL/feature/dataframe-cell-selection branch 3 times, most recently from 011e9ec to 40f0ccf Compare July 20, 2024 10:44
@Dev-iL Dev-iL force-pushed the Dev-iL/feature/dataframe-cell-selection branch from 40f0ccf to 94be5ac Compare July 29, 2024 13:43
@jrieke
Copy link
Collaborator

jrieke commented Aug 2, 2024

Hey @Dev-iL, sorry for the long delay, I was out on vacation.

I respectfully disagree that knowing where one clicked in a table goes against "an amazing user experience"

Totally agree with you that it doesn't have to go against a good UX, and as I pointed out, we'd totally love to have this feature! But as Lukas and I mentioned, it conflicts with some of the current behavior, which is essential for basic functionality like copy-pasting or viewing cell details. (Please also note that these are behaviors that come with the underlying library we're using, not things we added ourselves).

We'll absolutely get to the point where we can figure out all of these things and add cell selections but as you can see on our issues page, we got over 600 feature requests, so we need to prioritize very aggressively. Right now, cell selections are unfortunately not at the top of that priority list.

I can totally leave this PR open, and please be assured that we'll come back to it at some point!

@Dev-iL Dev-iL force-pushed the Dev-iL/feature/dataframe-cell-selection branch from 94be5ac to 73c4551 Compare August 7, 2024 19:31
@github-actions
Copy link
Contributor

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale label Aug 22, 2024
@Dev-iL Dev-iL force-pushed the Dev-iL/feature/dataframe-cell-selection branch from 73c4551 to a620e8f Compare August 22, 2024 04:43
@Dev-iL Dev-iL force-pushed the Dev-iL/feature/dataframe-cell-selection branch 3 times, most recently from f248529 to db247ce Compare May 28, 2025 06:02
@Dev-iL Dev-iL force-pushed the Dev-iL/feature/dataframe-cell-selection branch 2 times, most recently from bd53439 to 9046773 Compare June 18, 2025 14:40
@Dev-iL Dev-iL force-pushed the Dev-iL/feature/dataframe-cell-selection branch 2 times, most recently from 9bcc0fe to 511d1b0 Compare June 25, 2025 05:50
@Dev-iL Dev-iL force-pushed the Dev-iL/feature/dataframe-cell-selection branch 2 times, most recently from 3f0300b to ca671f0 Compare July 8, 2025 07:57
@Dev-iL
Copy link
Contributor Author

Dev-iL commented Jul 8, 2025

@lukasmasuch @jrieke
Can you please update what's your actual plan regarding this functionality? It's been tagged with needs-product-approval and planning for months - and now pushed to October according to the roadmap.

Codewise, is there anything that needs to be changed in or added to the PR? Otherwise, let's merge and be done with it!

@jrieke
Copy link
Collaborator

jrieke commented Jul 22, 2025

Can you please update what's your actual plan regarding this functionality? It's been tagged with needs-product-approval and planning for months - and now pushed to October according to the roadmap.

@Dev-iL sorry for the delay, we had lots of other things going on. We added cell selections to our plan for Q3 (which runs from Augst to October), will have a look into which behavior we want and if we can use your PR then!

@Dev-iL Dev-iL force-pushed the Dev-iL/feature/dataframe-cell-selection branch from ca671f0 to 8328008 Compare July 22, 2025 15:09
@Dev-iL Dev-iL force-pushed the Dev-iL/feature/dataframe-cell-selection branch from 8328008 to d2aeff0 Compare August 6, 2025 08:40
@lukasmasuch lukasmasuch self-assigned this Aug 12, 2025
@lukasmasuch lukasmasuch changed the title Add single- and multi-cell selection options to st.DataFrame Add single- and multi-cell selection options to st.dataframe Aug 12, 2025
Copy link
Collaborator

@lukasmasuch lukasmasuch left a comment

Choose a reason for hiding this comment

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

LGTM 👍 The selection feature was already merged in #11393. Merging this PR as an (almost) empty commit to give some attribution.

@lukasmasuch lukasmasuch enabled auto-merge (squash) August 12, 2025 09:05
@lukasmasuch lukasmasuch added security-assessment-completed Security assessment has been completed for PR status:product-approved Community PR is approved by product team and removed status:needs-product-approval PR requires product approval before merging labels Aug 12, 2025
@lukasmasuch lukasmasuch merged commit f18f346 into streamlit:develop Aug 12, 2025
37 of 38 checks passed
@Dev-iL Dev-iL deleted the Dev-iL/feature/dataframe-cell-selection branch August 16, 2025 12:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:feature PR contains new feature or enhancement implementation impact:users PR changes affect end users security-assessment-completed Security assessment has been completed for PR status:product-approved Community PR is approved by product team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants