Finding the sum of cells in a dataframe and showing that total in the last cell using calculate and reset buttons

Hi everyone, I have my editable dataframe created but I want to be able to calculate the total user input from each cell and show it in the rightmost cell as โ€œtotalโ€. I want a reset button to make everyone 0 when pressed. Calculate should calculate total. Thanks in advance.

add code here
# Import python packages
import streamlit as st
from snowflake.snowpark.context import get_active_session

#calculate button
if  st.button("Calculate"):
    df['Total'] = df['DOI'] + df['DOI+1']+ df['DOI+2']+df['DOI+3']

if st.button("Reset"):
    df['Total'] = df['DOI'] + df['DOI+1']+ df['DOI+2']+df['DOI+3*']
st.subheader("Current Liquidity Need")

#create dataframe with input data and input columns
dataframe_three = session.create_dataframe(
    [["ROW 1","","","","",""],
    ["ROW 2","","","","",""]],
    schema= [" ", "COLUMN1","COLUMN2","COLUMN3","COLUMN4","Total"]
)

# Execute the query and convert it into a Pandas dataframe
queried_data_three = dataframe_three.to_pandas()
styled_data = queried_data_three.style.set_properties(**{'border':'none','border-collapse':'collapse'})

#display dataframe for editable columns
st.experimental_data_editor(styled_data)


If applicable, please provide the steps we should take to reproduce the error or specified behavior.

**Expected behavior:**

Explain what you expect to happen when you run the code above.
I expect the user to add in values they want to in row 1 and 2 under the column names and those two totals get calculated in the right most box in each row. The reset button should reset everything to 0.
**Actual behavior:**
Buttons aren't working. Sum formula doesn't work.

Hi @st.jfern

Perhaps you can look into using Session State in a Streamlit app, more details here.

Thereโ€™s an example code for implementing a counter, which you may adapt for your use case:

import streamlit as st

st.title('Counter Example')
count = 0

increment = st.button('Increment')
if increment:
    count += 1

st.write('Count = ', count)

Hope this helps!

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