Issue with displaying success/error message on button click

I am trying to achieve the below functionality in SiS(streamlit in snowflake) but facing issues:

Streamlit in Snowflake (SiS) :

  • python=3.8.*
  • streamlit=1.26

Requirement:

  1. display table data as dataframe
  2. Take user input as text_input
  3. display ADD button,
    on click of ADD button
  • i) user input should be inserted to the table and display updated data of table in first point 1)
  • ii) Success or error message should be displayed below the button
  • iii) user input columns should be cleared (reset the input text values)
  1. Edit functionality on click of button on getting user input(text_input) of edited details

Solutions tried

Option1 : using If st.button(β€œADD”) : completed code below:
Issues faced :
The below code is displaying the success/error message below the button as per requirement, but facing below issues

  1. When clearing user input columns using reset function , getting error : β€œStreamlitAPIException: st.session_state.input_name cannot be modified after the widget with key input_name is instantiated.”
  2. Displayed dataframe on top is not being refreshed or added record is not showing in the table dataframe.

create table TEST_DB.PUBLIC.student
(
  student_name string,
  branch string,
  email string
);
insert into student values ('Dave','Computers','dave@gmail.com') ;
insert into student values ('Christi','Economics','Christi@gmail.com') ;
#**Streamlit Code:**

import streamlit as st
from snowflake.snowpark.context import get_active_session

session = get_active_session()
st.set_page_config(layout="wide")
st.title("Manage Student details :")

def reset():
   st.session_state.input_name =''
   st.session_state.input_branch =''
   st.session_state.input_email ='' 

def main():
   st.subheader("Student details")
   try:
       sql = "select student_name,branch,email from TEST_DB.PUBLIC.STUDENT "
       data = session.sql(sql).collect()
       st.dataframe(data)      
   except Exception as e:   
       st.error("Error while fectching snowflake account details: ")
       st.exception(e)   

   st.write('**:blue[Enter Student details to add:]**')
   dfColumns = st.columns(3)

   with dfColumns[0]:
       name = st.text_input(':blue[Student Name:]', key='input_name')
   with dfColumns[1]:
       branch = st.text_input(':blue[Branch:]', key='input_branch')
   with dfColumns[2]:
       mail = st.text_input(':blue[Email:]', key='input_email')

   if st.button("ADD"):
       if name !='' and branch != '' and mail != '':
           try:
               sql = "insert into TEST_DB.PUBLIC.STUDENT values ('"+name+"','"+branch+"','"+mail+"')"
               session.sql(sql).collect()
               st.success("details added successfully")
           except Exception as e:
               st.error("Error while inserting data")
               st.exception(e)
           finally:
               reset()

if __name__ == "__main__":
   main()

screenshot of the issued faced using option1 code:

Option 2 : using st.button(β€œADD” , on_click = add_button, args)

Issue : Success/error message is being displayed on top of the page

Code :


import streamlit as st
from snowflake.snowpark.context import get_active_session

session = get_active_session()
st.title("Manage Student details :")

def reset():
   st.session_state.input_name =''
   st.session_state.input_branch =''
   st.session_state.input_email =''

def add(name,branch,mail):
   if name !='' and branch != '' and mail != '':

       try:
           sql = "insert into TEST_DB.PUBLIC.STUDENT values (TEST_DB.PUBLIC.STUDENT_ID_SEQ.nextval,'"+name+"','"+branch+"','"+mail+"')"
           session.sql(sql).collect()
           st.success("details added successfully")

       except Exception as e:
           st.error("Error while inserting data")
           st.exception(e)
       finally:
           reset()

def main():
   st.subheader("Student details")

   try:
       sql = "select student_name,branch,email from TEST_DB.PUBLIC.STUDENT "
       data = session.sql(sql).collect()
       st.dataframe(data) 

   except Exception as e:   
       st.error("Error while fectching snowflake account details: ")
       st.exception(e)   
   st.write('**:blue[Enter Student details to add:]**')

   dfColumns = st.columns(3)
   with dfColumns[0]:
       name = st.text_input(':blue[Student Name:]', key='input_name')
   with dfColumns[1]:
       branch = st.text_input(':blue[Branch:]', key='input_branch')
   with dfColumns[2]:
       mail = st.text_input(':blue[Email:]', key='input_email')

   st.button("ADD", on_click=add,args=[name,branch,mail],key='add_button')

if __name__ == "__main__":
   main()

screenshot of the issued faced using option2 code:

Please let me know if there is any work around for this.