Modifying a column based on 'is_widget' value

Hello!
I am new to the community and this is my first post. I hope that I am posting in the right place and using the appropriate context.

I am wondering if it is possible to modify the value of a column in an editable data frame based on the user’s interaction with the ‘is_widget’ column.

Here is my data frame:

df = pd.DataFrame(
[
{“command”: “text1”, “value”: 1, “is_widget”: True},
{“command”: “text2”, “value”: 0, “is_widget”: False},
{“command”: “text3”, “value”: 1, “is_widget”: True},
]
)
edited_df = st.data_editor(df)

When a user checks the box for ‘is_widget’ column, I want the corresponding row in the ‘value’ column to be 1, otherwise 0 when unchecked.

Can this be done?

Many thanks in advance!

I am running the streamlit app locally.

Try this and check the update_values().

from streamlit import session_state as ss, data_editor as de, rerun as rr
import pandas as pd
import streamlit as st

df = pd.DataFrame(
    [
        {'command': 'text1', 'value': 1, 'is_widget': True},
        {'command': 'text2', 'value': 0, 'is_widget': False},
        {'command': 'text3', 'value': 1, 'is_widget': True},
    ]
)

if 'start_df' not in ss:
    ss.start_df = df
    
def update_values():
    for i in range(len(st.session_state.start_df)):
        st.session_state.start_df.at[i, 'value'] = int(st.session_state.start_df.at[i, 'is_widget'])

def main():
    edited_df = de(ss.start_df)
    
    if not ss.start_df.equals(edited_df):
        ss.start_df = edited_df
        update_values()    
        rr()
        

if __name__ == '__main__':
     main() 
1 Like

Thank you so much!

I was able to get your code to work.

Now on to the next phase
integrating this into my larger app! My main app also has an upload file functionality plus a few other functions to modify the uploaded data frame.

Thanks again!

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