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

[Coming soon] Allow dataframe rows to be selectable and trigger a function in Python code #688

Closed
timforr opened this issue Nov 14, 2019 · 30 comments · Fixed by #8411
Closed
Labels
feature:st.dataframe status:in-progress We're on it! type:enhancement Requests for feature enhancements or new features

Comments

@timforr
Copy link

timforr commented Nov 14, 2019

Problem

I have a st.table displaying search results. I want to be able to click on a specific row (or select several rows) and have that trigger an event in Python to update the page.

Solution

MVP: Table rows are clickable and trigger a custom event

Possible additions: What are other things that could be added to the MVP over time to make it better?

Preferred solution: More fully-featured solution similar to DTTables where we can specify custom table styling, embed objects like images in to table cells, select multiple rows or cells, etc.


Community voting on feature requests enables the Streamlit team to understand which features are most important to our users.

If you'd like the Streamlit team to prioritize this feature request, please use the 👍 (thumbs up emoji) reaction in response to the initial post.

@timforr timforr added the type:enhancement Requests for feature enhancements or new features label Nov 14, 2019
@nthmost
Copy link
Contributor

nthmost commented Nov 18, 2019

Hi @timforr -- Thanks for the feature request! This seems like a natural progression for Streamlit, pending some conversation within the team of course.

@chkoar
Copy link

chkoar commented Mar 30, 2020

Do we have an update on this?

@ajhayes83
Copy link

Yes this functionality would be very extremely useful! I would be keen to know if there is an update . Thanks!

@dennysemko
Copy link

dennysemko commented Apr 19, 2021

I have found myself multiple times over the past year needing this -- I am surprised it is still not implemented!

Something as easy as
clicks row in st.dataframe or st.table
and
receives back the index of the row in the DataFrame

@usapkota
Copy link

Is there any workaround until this enhancement is completed?

@anmol1ratn
Copy link

Any update on this? It would be really useful to be able to click on a specific cell/row and have that trigger an event in Python to update the page.

@fplanque
Copy link

I need this too.

Just a simple on_click event and a way to get the line index would already do wonders !

(Checkboxes on each row would also be an alternative, though in my case I don't need multiselect)

@sfc-gh-shsharma
Copy link

This would unlock a lot of interesting use cases. Typically data is presented in a table. Being able to click would enable pivoting views which is common operation on data.

@luca-serra
Copy link

Hi all, any update on this?
This feature would enable to do so many things with Streamlit!
Looking forward to it.

@jrieke
Copy link
Collaborator

jrieke commented Feb 10, 2023

We're working on this at the moment. No concrete launch data but we should have something during the next few months!

@jrieke jrieke added the status:in-progress We're on it! label Feb 10, 2023
@jace-parkinson
Copy link

jace-parkinson commented Mar 3, 2023

Might be able to use newly added feature to cover this issue.
https://blog.streamlit.io/editable-dataframes-are-here/

@fplanque
Copy link

fplanque commented Mar 3, 2023

Newly added feature should be able to cover this issue.
https://blog.streamlit.io/editable-dataframes-are-here/

How does editing cells in the table allow to react to a clic or to know which row has been selected?

These are different use cases.

@jace-parkinson
Copy link

jace-parkinson commented Mar 4, 2023

Newly added feature should be able to cover this issue.
https://blog.streamlit.io/editable-dataframes-are-here/

How does editing cells in the table allow to react to a clic or to know which row has been selected?

These are different use cases.

Yes, true. Based on examples in https://docs.streamlit.io/library/advanced-features/dataframes?ref=streamlit it seems possible using the session state (though maybe not yet straight forward). If not, the Next Up section in the original link states a more straight forward way to do this should be here soon:

"We have a bunch of new features for st.dataframe and st.experimental_data_editor in the pipeline for the next few months: showing images, clickable URLs in tables, letting the users select rows, and more."

@LukasMasuch LukasMasuch changed the title Allow table rows to be clickable and trigger a function in Python code Allow dataframe rows to be selectable and trigger a function in Python code Jun 4, 2023
@LukasMasuch
Copy link
Collaborator

LukasMasuch commented Jun 4, 2023

We have paused the work on a row selection feature temporarily but will get back to finishing implementing this soon. In the meantime, there is a good workaround with the latest Streamlit update (1.23) to implement a row selection via a checkbox column:

import streamlit as st
import numpy as np
import pandas as pd

df = pd.DataFrame(
    {
        "Animal": ["Lion", "Elephant", "Giraffe", "Monkey", "Zebra"],
        "Class": ["Mammal", "Mammal", "Mammal", "Mammal", "Mammal"],
        "Habitat": ["Savanna", "Forest", "Savanna", "Forest", "Savanna"],
        "Lifespan (years)": [15, 60, 25, 20, 25],
        "Average weight (kg)": [190, 5000, 800, 10, 350],
    }
)


def dataframe_with_selections(df):
    df_with_selections = df.copy()
    df_with_selections.insert(0, "Select", False)
    edited_df = st.data_editor(
        df_with_selections,
        hide_index=True,
        column_config={"Select": st.column_config.CheckboxColumn(required=True)},
        disabled=df.columns,
    )
    selected_indices = list(np.where(edited_df.Select)[0])
    selected_rows = df[edited_df.Select]
    return {"selected_rows_indices": selected_indices, "selected_rows": selected_rows}


selection = dataframe_with_selections(df)
st.write("Your selection:")
st.write(selection)
image

@lqmike
Copy link

lqmike commented Jun 5, 2023

st.dataframe is getting so much better now, i think with returning clicked row selection feature we will not need the st aggrid for most use cases, looking forward thanks a lot!!

@sfc-gh-jrieke sfc-gh-jrieke added status:likely Will probably implement but no timeline yet and removed status:in-progress We're on it! labels Jun 5, 2023
@u3Izx9ql7vW4
Copy link

We have paused the work on a row selection feature temporarily but will get back to finishing implementing this soon. In the meantime, there is a good workaround with the latest Streamlit update (1.23) to implement a row selection via a checkbox column:

import streamlit as st
import numpy as np
import pandas as pd

df = pd.DataFrame(
    {
        "Animal": ["Lion", "Elephant", "Giraffe", "Monkey", "Zebra"],
        "Class": ["Mammal", "Mammal", "Mammal", "Mammal", "Mammal"],
        "Habitat": ["Savanna", "Forest", "Savanna", "Forest", "Savanna"],
        "Lifespan (years)": [15, 60, 25, 20, 25],
        "Average weight (kg)": [190, 5000, 800, 10, 350],
    }
)


def dataframe_with_selections(df):
    df_with_selections = df.copy()
    df_with_selections.insert(0, "Select", False)
    edited_df = st.data_editor(
        df_with_selections,
        hide_index=True,
        column_config={"Select": st.column_config.CheckboxColumn(required=True)},
        disabled=df.columns,
    )
    selected_indices = list(np.where(edited_df.Select)[0])
    selected_rows = df[edited_df.Select]
    return {"selected_rows_indices": selected_indices, "selected_rows": selected_rows}


selection = dataframe_with_selections(df)
st.write("Your selection:")
st.write(selection)
image

Hi this looks great, when I'm using it realtime with placeholder, each refresh wipes the selection. Is there a quick fix for this?

@pkonduri
Copy link

pkonduri commented Nov 4, 2023

Hi, checking it to see if there's an update to this beyond the checkbox column hack? My use case is that if a user selects any cell in that row, the values in that row are used as inputs to a different API call.

@jrieke
Copy link
Collaborator

jrieke commented Nov 14, 2023

Update

See my comment here.

@chuckpaulson
Copy link

chuckpaulson commented Nov 22, 2023

The checkbox work around helps, but requires the user to unselect the row before selecting a new one if you are trying to just select one row. Also you have to add a new checkbox column and are not able to click anywhere on the row. The click a row and then call a function with row index would be really useful. Thanks.

@pkonduri
Copy link

pkonduri commented Nov 22, 2023

@chuckpaulson Agree, the checkbox "works" but is a poor option if the user should only be able to select 1 box (there are many use cases for this). I have a nasty hack where whenever a user selects a checkbox, I use the st.data_editor's callback to a function which resets all the checkboxes in the column. This has a bad side effect where it undos any table filtering/sorting the user may have done.

@jrieke It would be really awesome if we could somehow get this feature prioritized (I know it's on your roadmap but it seems like its still quite a few months out). For example, even a simple optional parameter to the st.checkbox called selectOne which is a bool, and determines whether a user can select only one checkbox would really help in this case.

@pkonduri
Copy link

Hi @jrieke any updates on this?

@irfanc
Copy link

irfanc commented Mar 1, 2024

Any updates on this?

@jrieke
Copy link
Collaborator

jrieke commented Mar 8, 2024

Hey all! We're just starting to pick this feature back up and are planning to build a first prototype by the end of April. I'll update here once I have more news about the status or the API.

@ymbender
Copy link

Hi,
i would really appreciate to have some callback or just some way to allow updating a separate text field based on the selection made in the dataframe (where the workaround with the selection column is not applicable). Really looking forward to have this.
In general having more "click events" for Streamlit components would really help in many cases (already missed that for tabs component and some other cases).

@jrieke
Copy link
Collaborator

jrieke commented Apr 8, 2024

We just finished a very rough prototype for dataframe selections: https://dataframe-row-selections.streamlit.app/

Feel free to give it a try but note that it's still under active development, so things might and will change! Let me know if you have any feedback.

For a broader picture on selection events, see my comment here.

@LukasMasuch LukasMasuch changed the title Allow dataframe rows to be selectable and trigger a function in Python code [Coming soon] Allow dataframe rows to be selectable and trigger a function in Python code Apr 10, 2024
@jrieke jrieke added status:in-progress We're on it! and removed status:likely Will probably implement but no timeline yet labels Apr 17, 2024
@jrieke jrieke pinned this issue Apr 17, 2024
@jrieke
Copy link
Collaborator

jrieke commented Apr 17, 2024

Quick update: we're quite advanced on the prototype I posted above, as well as on the API discussions. We're currently planning to release dataframe selections for both rows + columns sometime in May or early June mid May. It might still change a bit, though, as we're finalizing our release schedule for the next few months. I'll post some more details on the API soon!

LukasMasuch added a commit that referenced this issue May 13, 2024
## Describe your changes

This PR adds row and column selection support to `st.dataframe`. It can
be used like this:

```python
selection = st.dataframe(
    df,
    on_select="rerun",
    selection_mode="single-row"
)
```

- [Demo App](https://dataframe-row-selections.streamlit.app/)


https://github.com/streamlit/streamlit/assets/2852129/f3ff476a-0bd0-4b82-bc97-6bda3a3be98c

## GitHub Issue Link (if applicable)

- Closes #688
- Closes #7134
- #455
- #8319

## Testing Plan

- Added unit tests
- Added e2e tests (see #8634)

```[tasklist]
### e2e tests
- [x] Single row/column selection
- [x] Multi row/column selection
- [x] Mixed selections
- [x] Screenshot of a dataframe with multiple selections
- [x] Clear selections via toolbar
- [x] Clear selections via escape
- [x] Select all rows in multi-row selection via top checkbox
- [x] Optional: Test drag and drop selection
- [x] Optional: Test shift selections
- [x] Optional: Test selections in form
- [x] Have some test cases work with session state and others with return value
- [x] Add a test case validating that the callback gets called
``` 


---

**Contribution License Agreement**

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

---------

Co-authored-by: Benjamin Räthlein <benjamin.raethlein@gmail.com>
@LukasMasuch
Copy link
Collaborator

LukasMasuch commented May 13, 2024

We will release row & column selections for st.dataframe in the 1.35 release. You can activate selections via on_select and configure it via selection_mode, e.g.:

selection = st.dataframe(df, on_select="rerun", selection_mode="single-row")

Or for multi-row & column selections:

selection = st.dataframe(df, on_select="rerun", selection_mode=("multi-row", "multi-column"))

You can try it out in this demo.

@LukasMasuch LukasMasuch unpinned this issue May 14, 2024
@chrispy-snps
Copy link

@LukasMasuch - thanks for the demo, this will be great for our application!

@rutuja1409
Copy link

We will release row & column selections for st.dataframe in the 1.35 release. You can activate selections via on_select and configure it via selection_mode, e.g.:

selection = st.dataframe(df, on_select="rerun", selection_mode="single-row")

Or for multi-row & column selections:

selection = st.dataframe(df, on_select="rerun", selection_mode=("multi-row", "multi-column"))

You can try it out in this demo.

Thank you soo much for this update. @LukasMasuch can you let us know when streamlit is planning to release version 1.35? It will be very helpful.

@LukasMasuch
Copy link
Collaborator

@rutuja1409 Probably today or tomorrow :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature:st.dataframe status:in-progress We're on it! type:enhancement Requests for feature enhancements or new features
Projects
None yet
Development

Successfully merging a pull request may close this issue.