Reporting another bug | st.data_editor

Version 1.37
Report about the next bug…

If there is a column of the “DateColumn” type inside the “data_editor”

“Start Date”: st.column_config.DateColumn(
           “Start Date”,
           help=“TIP for ”,
           width=None,
           required=True,
           default=datetime.now(),
           format=str(st.session_state[‘date_format’]),
           ),

The value of the “hide_index” flag is considered only during the first rendering.

st.session_state[‘edited_df_pta’] = st.data_editor(df, key=“pta_data_editor”, hide_index=True, column_config=column_config, num_rows=“dynamic”)

Since the presence of a column of the “DateColumn” type breaks the correct operation of the “data_editor” (for example, the contents of the st.session_state[‘edited_df_pta’] are reset every time the user completed adding a new row) it is necessary to use tricks like

        if 'edited_df_pta' in st.session_state:
            df = st.session_state['edited_df_pta'].copy()

        if 'pta_data_editor' in st.session_state:
            for item in st.session_state['pta_data_editor']["added_rows"]:
                df.loc[len(df.index)] = [item["A"], item["B"], item["C"], datetime.strptime(item["Start Date"],'%Y-%m-%d'), item["D"]]
            rows_for_remove = []
            for item in st.session_state['pta_data_editor']["deleted_rows"]:
                rows_for_remove.append(item)
            df = df.drop(index = rows_for_remove)
            df = df.reset_index(drop = True)
            for item in st.session_state['pta_data_editor']["edited_rows"]:
                for elem in st.session_state['pta_data_editor']["edited_rows"][item]:
                    df.loc[item, elem] = st.session_state['pta_data_editor']["edited_rows"][item][elem]

before the

st.session_state[‘edited_df_pta’] = st.data_editor(df, key=“pta_data_editor”, hide_index=True, column_config=column_config, num_rows=“dynamic”)

In the end, the incorrect behavior of the “data_editor” can be bypassed, but the fact that the Streamlit
ignores the value of the hide_index=True flag for all rendering except the first one requires a quick fix…

However, you can use another trick and add the following column to the column_config:

            "_index": st.column_config.NumberColumn(
                "_index",
                width=None,
                default=10,
                disabled=True,
                required=True,
            ),

In this case, the index will stop being displayed, but it should work without such tricks (IMHO).

1 Like