Data Editor Checkbox on Change

Hey guys:

I am fairly new to Streamlit and have a question about values in Streamlit Data Editor. I have a data editor that is displaying records that folks can either check as Approved or Denied and it places their choices in two separate data frames . So there are two check boxes - one called Approve and one called Deny. What I would like to happen is that if a person checks Approve it will make the Deny Checkbox false and if they click Deny it will make the Approve checkbox false. In other words they can’t have both Approve and Deny checked at the same time. Obviously this would occur at whatever row they were in - so if there were 10 records and they were approving row 1 I would only like the change/validation to happen at row 1 and not affect checkboxes of other rows. I am also new to Python so trying to understand iterating through stuff. If anyone has done something similar to this already would sure appreciate how to handle these changes. Again this is when it is inside the table of the data editor - not checkboxes that are outside of data editor. Thanks in advance for any advice.

Here is a toy example of how you can do that:

import streamlit as st
import pandas as pd

if "df" not in st.session_state:
    st.session_state.df = pd.DataFrame(
        {
            "Name":["A","B","C"],
            "Approve":[False,False,False],
            "Deny":[False,False,False]
        }
    )
df = st.session_state.df

new_df = st.data_editor(df, key="edits")

if st.session_state.edits["edited_rows"]:
    for row, edits in st.session_state.edits["edited_rows"].items():
        for col, value in edits.items():
            st.session_state.df.loc[row, col] = value
        if "Approve" in edits and edits["Approve"] == True:
            st.session_state.df.loc[row, "Deny"] = False
        elif "Deny" in edits and edits["Deny"] == True:
            st.session_state.df.loc[row, "Approve"] = False
    st.rerun()

Thank you I will give it a shot :blush:

Shawn Colvard

CFIS IT Data Specialist – CFIS BI & DW

Caterpillar Financial Services Corporation

Phone: 615-341-8390

Tie Line: 7-284-8390

E-Mail: Shawn.Colvard@Cat.com

Caterpillar Confidential: Green

www.catfinancial.com