Trigger function from an st.data_editor

im running a local streamlit app.

on user action (be it a click or, preferably, a selection) of a row/cell
i would like to show content of a json file to st.json below the table.

import streamlit as st
import pandas as pd

# sample DataFrame
data = {
    "Column 1": [1, 2],
    "Column 2": ['A', 'B']
}

#sample json
jdata ='''
    {
        "glossary": {
            "title": "example glossary",
        }
    }
'''

# Create a DataFrame from the sample data  
df = pd.DataFrame(data)

# Display the DataFrame in the Streamlit app
st.dataframe(df,
        column_config={
            "json": st.column_config.LinkColumn(
            "JSON",
            width='small',
            )
        }
)

#only show the following on click of row/cell
st.json(jdata)
1 Like

Hi @yoshco

If you’d like to use the st.data_editor, you can replace st.dataframe with st.data_editor.

In order to trigger a function upon widget interaction or changes to the dataframe, you can make use of the on_change parameter by assigning it a callback function that essentially will tell it what to do once the trigger occurs.

More info on st.data_editor in the Docs:

More info on Session states also in the Docs:

1 Like

thank you for the links, mybe i’ll rephrase my question as i dont see the solution in those links. |
how do i retrun on callback the specific cell/row i clicked?

1 Like

Hello, I have a somewhat similar question.
In my dataframe, say I have a Pay_Due, Pay_Amount, and Pay_Balance columns, among others. How can I make the Pay_Balance be automatically computed as Pay_Amount - Pay_Due inside st.data_editor(), so when I make an update to any Pay_Amount value in my dataframe, it gets picked up by the component and updates it automatically?

1 Like

image

Here is an example code.

import streamlit as st
from streamlit import session_state as ss
import pandas as pd


data = {
    'Name': ['AAA', 'BBB', 'CCC'],
    'Pay Due': [200, 200, 200],
    'Pay Amount': [50, 50, 50],
    'Pay Balance': [0, 0, 0]
}


if 'df' not in ss:
    ss.df = pd.DataFrame(columns=list(data.keys()))


def change():
    # Get the edited row index, column name and value.
    er = ss.pay['edited_rows']
    affected_index = list(er.keys())[0]
    affected_pay_amount = er[affected_index]['Pay Amount']

    # Update the pay amount at the affected index.
    ss.df.at[affected_index, 'Pay Amount'] = affected_pay_amount    

    # Calculate the new value for pay balance at the affected index.
    pay_bal = ss.df.at[affected_index, 'Pay Amount'] - ss.df.at[affected_index, 'Pay Due']

    # Update the value of pay_bal at the affected index.
    ss.df.at[affected_index, 'Pay Balance'] = pay_bal


def main():
    if not len(ss.df):
        ss.df = pd.DataFrame(data)

    st.data_editor(ss.df, hide_index=True, on_change=change, key='pay')


if __name__ == '__main__':
    main()
2 Likes

Thank you @ferdy.
It worked perfectly as expected!

@ferdy
Since you are a Streamlit moderator, I wanted to ask this question that, because after many research on Streamlit forum and looking up the Streamlit Deployment guides posted on 🚀 Deployment - Streamlit, I was not able to get an answer.

How can I deploy my Streamlit app on a shared hosting site? My site is operated by A2hosting.com and I have a domain I own that I would like to have my Streamlit app be myapp.mydomain.com.

A2hosting allow python apps via either Flask or Django frameworks. I followed their guide but both frameworks differ from Streamlit, and I much prefer Streamlit and don’t want to rewrite my apps. Their customer service was not able to provide me with a new Guide specifically to run Streamlit on their site (could be many reasons).

Is there a way I can get answers to this question? How to deply my Streamlit apps on my own hosted domain?
Thanks in advance.
Tony

I used the same code but in my st.session_state.edited_rows there are more than one values. Its recording the history edits too in the same session.

Any idea how to resolve that, I am getting an output like this:
{‘edited_rows’: {3: {‘2024-02-25’: 2024, ‘2024-02-04’: 21138, ‘2024-02-11’: 209}, 7: {‘2024-02-11’: 9}}, ‘added_rows’: , ‘deleted_rows’: }

Note: Deleting the state like this is not working as expected:
if “editor” in st.session_state:
del st.session_state.editor[‘edited_rows’]