I am afraid that it is impossible. I tried for about 3 hours, then made many types of AIs attempt, and they all had to make another editor. If you figure it out, .
import streamlit as st
import pandas as pd
# 1. Create a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df['C'] = df['A'] + df['B']
# 2. Add df to session state if it doesn't exist
if 'df' not in st.session_state:
st.session_state['df'] = df
# 3. Create an "on change" function that will be called when the data_editor is changed
# and update the original dataframe with the new values
def on_change():
# print(st.session_state['data_editor'])
# Example of data_editor:
# {'edited_rows': {0: {'A': 3}}, 'added_rows': [], 'deleted_rows': []}
edited_rows = st.session_state['data_editor']['edited_rows']
# now update original dataframe with the new values
for index, row in edited_rows.items():
for col, value in row.items():
if col in df.columns:
st.session_state['df'].at[index, col] = value
else:
st.warning(f"Column {col} does not exist in the dataframe.")
# Recalculate column C based on the updated values in A and B
st.session_state['df']['C'] = st.session_state['df']['A'] + st.session_state['df']['B']
st.data_editor(
st.session_state['df'],
disabled=['C'], # Disable editing for column C that is calculated from A and B
key='data_editor',
on_change=on_change
)
# print(st.session_state['df'])
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.