Hi, I am having problems with using the st.form and st.data_editor components. I am developing a small application for financial record keeping. For this I use the st.data_editor component for the input of new information such as incoming or outgoing money. Once modified, I proceed to update an excel hosted in my google drive, which contains the complete information of the financial record, which allows me a daily update.
The following code represents part of my application and is also where the problem is located.
import streamlit as st
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
if 'credentials' not in st.session_state:
gauth = GoogleAuth()
directorio_credenciales = 'credentials_module.json'
gauth.LoadCredentialsFile(directorio_credenciales)
if gauth.access_token_expired:
gauth.Refresh()
gauth.SaveCredentialsFile(directorio_credenciales)
else:
gauth.Authorize()
st.session_state.credentials = GoogleDrive(gauth)
with st.form('form1'):
data = st.data_editor(pd.read_excel(pd.ExcelFile(f"https://docs.google.com/spreadsheets/d/1-qpdnIsc6rPTLULLAzg34Bl_pIF9F5XAHrPtVln3Ca4/export?format=xlsx"), sheet_name = "Sheet1"), num_rows = "dynamic", use_container_width = True)
submitted1 = st.form_submit_button("Actualizar", use_container_width = True)
if submitted1:
st.dataframe(data)
data.to_excel("Contabilidad.xlsx", index = None)
idexe = '1-qpdnIsc6rPTLULLAzg34Bl_pIF9F5XAHrPtVln3Ca4'
gfile = st.session_state.credentials.CreateFile({'id': idexe})
gfile.SetContentFile(r'Contabilidad.xlsx')
gfile.Upload()
The problem I have is that when the excel update depends on the intent. I explain it below:
Attempt 1: The excel is updated according to what is registered in the st.data_editor.
Attempt 2: The excel is not updated and the modified in the st.data_editor is lost.
Attempt 3: The excel is updated according to what is registered in the st.data_editor.
Attempt 4: The excel is not updated and the modified in the st.data_editor is lost.
Attempt 5: The excel is updated according to what is registered in the st.data_editor.
.
.
.
I did not understand why this happened, so I tried with the following code, to see the behavior of the table without the update of the excel, and it works normal, what I modified was reflected correctly when I press the button. This left me more confused.
import streamlit as st
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
if 'credentials' not in st.session_state:
gauth = GoogleAuth()
directorio_credenciales = 'credentials_module.json'
gauth.LoadCredentialsFile(directorio_credenciales)
if gauth.access_token_expired:
gauth.Refresh()
gauth.SaveCredentialsFile(directorio_credenciales)
else:
gauth.Authorize()
st.session_state.credentials = GoogleDrive(gauth)
with st.form('form1'):
data = st.data_editor(pd.read_excel(pd.ExcelFile(f"https://docs.google.com/spreadsheets/d/1-qpdnIsc6rPTLULLAzg34Bl_pIF9F5XAHrPtVln3Ca4/export?format=xlsx"), sheet_name = "Sheet1"), num_rows = "dynamic", use_container_width = True)
submitted1 = st.form_submit_button("Actualizar", use_container_width = True)
if submitted1:
st.dataframe(data)
I attach the following video to make it clearer what I describe.
I hope you can help me, because for the moment, the solution I found is to use st.experimental_rerun, but it does not seem to me the optimal solution. Thank you.