Select single or multiple rows in st.dataframe and return the selected data

My question is similar to below

but here i am fetching data from a link using submit button on sidebar. So after fetching the get populated on the app but as soon as i select any row both dataframes get deleted.

import streamlit as st
import pandas as pd

def load_data():
    return pd.read_csv(
        "https://gist.githubusercontent.com/curran/a08a1080b88344b0c8a7/raw/0e7a9b0a5d22642a06d3d5b9bcbad9890c8ee534/iris.csv"
    )


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,
    )
    # Filter the dataframe using the temporary column
    selected_rows = edited_df[edited_df["Select"]]
    return selected_rows.drop("Select", axis=1)


def main():
    st.sidebar.title("Data Selection")
    if st.sidebar.button("Submit"):
        df = load_data()
        selection = dataframe_with_selections(df)
        st.write("Your selection:")
        st.write(selection)


if __name__ == "__main__":
    main()

Then don’t put the code that shows the dataframes inside the if block.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.