Filtered Dataframe Code giving key error

Summary

I am unable to save a filtered dataframe using the st.download_button function when I pair it with a filtered dataframe using the “filter_dataframe” function from this post Auto-generate a dataframe filtering UI in Streamlit with filter_dataframe!. My dashboard throws an error "there are multiple widgets with the same key=‘add filters checkbox’. error, but this goes away when I remove the download button code (even though the error is related to the filter_dataframe and not the download button).

Steps to reproduce

Code snippet:


def filter_dataframe(df: pd.DataFrame) -> pd.DataFrame:
    """
    Adds a UI on top of a dataframe to let viewers filter columns

    Args:
        df (pd.DataFrame): Original dataframe

    Returns:
        pd.DataFrame: Filtered dataframe
    """
    modify = st.checkbox("Add filters", key="add filters checkbox") #This is line that generates the error

    if not modify:
        return df

    df = df.copy()

    # Try to convert datetimes into a standard format (datetime, no timezone)
    for col in df.columns:
        if is_object_dtype(df[col]):
            try:
                df[col] = pd.to_datetime(df[col])
            except Exception:
                pass

        if is_datetime64_any_dtype(df[col]):
            df[col] = df[col].dt.tz_localize(None)

    modification_container = st.container()

    with modification_container:
        to_filter_columns = st.multiselect("Filter dataframe on", df.columns)
        for column in to_filter_columns:
            left, right = st.columns((1, 20))
            left.write("↳")
            # Treat columns with < 10 unique values as categorical
            if is_categorical_dtype(df[column]) or df[column].nunique() < 10:
                user_cat_input = right.multiselect(
                    f"Values for {column}",
                    df[column].unique(),
                    default=list(df[column].unique()),
                )
                df = df[df[column].isin(user_cat_input)]
            elif is_numeric_dtype(df[column]):
                _min = float(df[column].min())
                _max = float(df[column].max())
                step = (_max - _min) / 100
                user_num_input = right.slider(
                    f"Values for {column}",
                    _min,
                    _max,
                    (_min, _max),
                    step=step,
                )
                df = df[df[column].between(*user_num_input)]
            elif is_datetime64_any_dtype(df[column]):
                user_date_input = right.date_input(
                    f"Values for {column}",
                    value=(
                        df[column].min(),
                        df[column].max(),
                    ),
                )
                if len(user_date_input) == 2:
                    user_date_input = tuple(map(pd.to_datetime, user_date_input))
                    start_date, end_date = user_date_input
                    df = df.loc[df[column].between(start_date, end_date)]
            else:
                user_text_input = right.text_input(
                    f"Substring or regex in {column}",
                )
                if user_text_input:
                    df = df[df[column].str.contains(user_text_input)]

    return df

if "prediction_df" not in st.session_state:
    st.session_state["prediction_df"] = pd.read_excel(
        "Data.xlsx"
    ).set_index(["id", "id2"])


filtered_df = st.dataframe(
    filter_dataframe(st.session_state["prediction_df"].reset_index())
)


@st.cache
def convert_df(df):
    # IMPORTANT: Cache the conversion to prevent computation on every rerun
    return df.to_csv().encode("utf-8")


csv = convert_df(filter_dataframe(st.session_state["prediction_df"]))

st.download_button(
    label="Download data as CSV",
    data=csv,
    file_name="large_df.csv",
    mime="text/csv",
    key="download_button_for_csv",
)


If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

It should create a button below my dataframe (the filtered dataframe generated from the session state variable. This will give me the option to export the dataframe after applying a filter.

Actual behavior:

No button is generated, and instead throws an error.




**DuplicateWidgetID**: There are multiple widgets with the same `key='add filters checkbox'`.

To fix this, please make sure that the `key` argument is unique for each widget you create.

Traceback:

File '…\streamlit.py", line 309, in
csv = convert_df(predictions)File “…\streamlit.py”, line 34, in filter_dataframe
modify = st.checkbox(“Add filters”, key=“add filters checkbox”)

Debug info

  • Streamlit version: 1.25.0
  • Python version: 3.8.10
  • VS Code for IDE, Chrome for the browser
  • OS version: Windows 10 Enterprise
  • Browser version: Version 117.0.5938.134

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