# Trigger function from an st.data\_editor

**URL:** <https://discuss.streamlit.io/t/trigger-function-from-an-st-data-editor/57610>\
**Category:** Using Streamlit\
**Created:** [December 18, 2023, 11:30am UTC](https://discuss.streamlit.io/t/trigger-function-from-an-st-data-editor/57610 "2023-12-18T11:30:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![yoshco](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/yoshco/32/31966_2.png) [@yoshco](https://discuss.streamlit.io/u/yoshco)\
**Post date:** [December 18, 2023, 11:30am UTC](https://discuss.streamlit.io/t/trigger-function-from-an-st-data-editor/57610/1 "2023-12-18T11:30:16Z")

</div>

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.

```python
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)

```

---

<div class="post-metadata">

**Author:** ![dataprofessor](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/dataprofessor/32/44163_2.png) [@dataprofessor](https://discuss.streamlit.io/u/dataprofessor)\
**Post date:** [December 18, 2023, 1:41pm UTC](https://discuss.streamlit.io/t/trigger-function-from-an-st-data-editor/57610/2 "2023-12-18T13:41:38Z")

</div>

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:

> **[st.data\_editor - Streamlit Docs](https://docs.streamlit.io/library/api-reference/data/st.data_editor)**
>
> st.data\_editor display a data editor widget that allows you to edit dataframes and many other data structures in a table-like UI.

More info on Session states also in the Docs:

> **[Session State - Streamlit Docs](https://docs.streamlit.io/library/api-reference/session-state)**
>
> st.session\_state is a way to share variables between reruns, for each user session.

> **[Add statefulness to apps - Streamlit Docs](https://docs.streamlit.io/library/advanced-features/session-state)**

---

<div class="post-metadata">

**Author:** ![yoshco](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/yoshco/32/31966_2.png) [@yoshco](https://discuss.streamlit.io/u/yoshco)\
**Post date:** [December 18, 2023, 4:35pm UTC](https://discuss.streamlit.io/t/trigger-function-from-an-st-data-editor/57610/3 "2023-12-18T16:35:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tonyz80](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/tonyz80/32/47858_2.png) [@tonyz80](https://discuss.streamlit.io/u/tonyz80)\
**Post date:** [March 17, 2024, 12:13am UTC](https://discuss.streamlit.io/t/trigger-function-from-an-st-data-editor/57610/4 "2024-03-17T00:13:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ferdy](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/ferdy/32/29957_2.png) [@ferdy](https://discuss.streamlit.io/u/ferdy)\
**Post date:** [March 17, 2024, 9:10am UTC](https://discuss.streamlit.io/t/trigger-function-from-an-st-data-editor/57610/5 "2024-03-17T09:10:23Z")

</div>

> [@tonyz80](#):
>
> 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

![image](https://global.discourse-cdn.com/streamlit/original/3X/b/2/b2f7a192d1982a17619acc1509b704da37d35f83.png)

Here is an example code.

```auto
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()

```

---

<div class="post-metadata">

**Author:** ![tonyz80](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/tonyz80/32/47858_2.png) [@tonyz80](https://discuss.streamlit.io/u/tonyz80)\
**Post date:** [March 17, 2024, 3:37pm UTC](https://discuss.streamlit.io/t/trigger-function-from-an-st-data-editor/57610/6 "2024-03-17T15:37:26Z")

</div>

Thank you @ferdy.  
It worked perfectly as expected!

---

<div class="post-metadata">

**Author:** ![tonyz80](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/tonyz80/32/47858_2.png) [@tonyz80](https://discuss.streamlit.io/u/tonyz80)\
**Post date:** [March 17, 2024, 3:52pm UTC](https://discuss.streamlit.io/t/trigger-function-from-an-st-data-editor/57610/7 "2024-03-17T15:52:29Z")

</div>

@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](https://discuss.streamlit.io/c/deployment/14), 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](http://A2hosting.com) and I have a domain I own that I would like to have my Streamlit app be [myapp.mydomain.com](http://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

---

<div class="post-metadata">

**Author:** ![keshav\_garg](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/keshav_garg/32/28387_2.png) [@keshav\_garg](https://discuss.streamlit.io/u/keshav_garg)\
**Post date:** [March 26, 2024, 7:42am UTC](https://discuss.streamlit.io/t/trigger-function-from-an-st-data-editor/57610/8 "2024-03-26T07:42:22Z")

</div>

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’]

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/streamlit/original/3X/c/f/cf4c2ee0d938358f82448c5fe4b9c3c91d7ce19c.svg) [@system](https://discuss.streamlit.io/u/system)\
**Post date:** [September 22, 2024, 7:43am UTC](https://discuss.streamlit.io/t/trigger-function-from-an-st-data-editor/57610/9 "2024-09-22T07:43:02Z")

</div>

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