Update the Values in FORMS object and Save to Database

Dear all,

I am facing the following issue with Streamlit and would really appreciate some help.

I am creating a CRM system with Streamlit in order to SAVE, READ and UPDATE user data.
In order to achieve this, I created an FORMS object with multiple TEXT_INPUT objects.
In the Sidepanel, I have created a TEXT_INPUT field where the user can insert a Number and press a button. After pressing the button, the user is then shown a FORMS object with several TEXT_INPUT objects that are prefilled with the latest values from the database.

Now, I would like to update the existing values and press the SUBMIT_BUTTON to store the new values in the database. And here is where i am having problems with. As I update the TEXT INPUT objects in the FORMS Object, the new values are not being saved. The System just saves the old values into the Database when pressing the submit button.

Some pictures

Here i fill in the Account Number and Press “Get KYC” Button. I am then presented with a pre filled forms object with the latest values.

Now I have updated the Values in the Forms object and Would like to Save this to the Database

Here is my code:

def test_ui():
	st.sidebar.markdown("# KYC")
	# account number text input
	account_number = st.sidebar.text_input("Rekeningnummer:")
	get_kyc_button = st.sidebar.button("Get KYC")

	if get_kyc_button:
		ui_KYC_new(account_number)

def ui_KYC_new(account_number):

	st.header('Know Your Customer')
	st.write('v 2021.01')
	st.markdown('____')
	with st.form(key="myform"):
		st.subheader('Personalia')
# Get Existing Value form the database and present them in the Forms object
		Rekeninghouder_1 = st.text_input('Rekeninghouder 1:', get_kyc(account_number)['Rekeninghouder_1'][0], key="Rekeninghouder_1")	

		Rekeninghouder_2 = st.text_input('Rekeninghouder 2:', get_kyc(account_number)['Rekeninghouder_2'][0], key="Rekeninghouder_2")
		
		Vertegenwoordiger_Gevolmachtigde = st.text_input('Vertegenwoordiger / Gevolmachtigde:', get_kyc(account_number)['Vertegenwoordiger_Gevolmachtigde'][0], key="Vertegenwoordiger_Gevolmachtigde")
		
		st.form_submit_button(label='Submit', on_click=writekyc(account_number,Rekeninghouder_1,Rekeninghouder_2, Vertegenwoordiger_Gevolmachtigde))

# Function to get Existing KYC information from DB
def get_kyc(accountnumber):
	con = sqlite3.connect('example4.db')
	global engine

	df = pd.read_sql(f''' SELECT * FROM kyc1 WHERE account_number = '{accountnumber}' order by Datum desc limit 1;''', con = engine)

	return df
# Function to write new Values to Database
def writekyc(account_number,Rekeninghouder_1,Rekeninghouder_2, Vertegenwoordiger_Gevolmachtigde):
	con = sqlite3.connect('example4.db')
	today = date.today()
	# dd/mm/YY
	d1 = today.strftime("%d/%m/%Y")

	# create cursor object
	cur = con.cursor()

	cur.execute(f'''
			INSERT INTO kyc1 (datum, account_number, Rekeninghouder_1, Rekeninghouder_2, Vertegenwoordiger_Gevolmachtigde) values ('{d1}', '{account_number}', '{Rekeninghouder_1}', '{Rekeninghouder_2}','{Vertegenwoordiger_Gevolmachtigde}')
		''')
	con.commit()
	con.close() paste code here
1 Like

I am having a similar problem. Does anyone have a solution?

I think the parameters of the form submit buttons callback function (writekyc(account_number,Rekeninghouder_1) do not contain the values of the text inputs.
To get those values, you probably need to fetch them, using session_state with the keys of the inputs.
Something like this:

if 'Rekeninghouder_1' not in st.session_state:
    st.session_state.Rekeninghouder_1=' '
...
st.text_input('Rekeninghouder 1:', key='Rekeninghouder_1')
st.form_submit_button(label='Submit', on_click=writekyc)
...
def writekyc():
Rekh1 = st.session_state.Rekeninghouder_1   #Get value from text input
....
1 Like

That works for me. Thank you so much

1 Like

use st.session_state to save data

code example

import streamlit as st 
from datetime import datetime

def my_callback():
    print(f"curdate: {datetime.now()} first: {st.session_state.value1} sec: {st.session_state.value2}")

with st.form('my_form',clear_on_submit=True):
    st.radio(label='🙆‍♀️ part1:', options=[
                        '解决', '未解决', '未完全解决'], horizontal=True,
                        key='value1')

    error_list = ['答非所问', '推荐错误', '推荐不准确', '回答不详细', '信息更新不及时']
    not_saf_reason = st.radio(
        label='🤦‍♀️ part2:', options=error_list, horizontal=True,key='value2')

    submit_button = st.form_submit_button(
                    label='📥 点我提交', on_click=my_callback
                    )