How to update dataframe from samples in streamlit

I am trying to implement a simple UI. It displays prospect map from a google sheet. And display a sample of uncontacted prospects to call during the day.

The idea would be to display the sample of prospects and allow the user to add prospect contact name, phone number and email in the UI who will then update the dataframe and the google sheet.

I found the way to freeze the sample dataframe with @st.cache.
It works to input the values and they are updated locally, but when I upload the obtained dataframe to google sheets, the changes are not reflected …

I attach here below the last snippet of my code :

import streamlit as st
from streamlit_folium import folium_static
import table_management
import prospect_map as pm
from oauth2client.service_account import ServiceAccountCredentials
import pandas as pd


#Google sheet identification and worksheets:
sheet_id = "XxxxXXXxx"
initial_worksheet="xXxXX"
final_worksheet = "xxXXXxx"
#Scope of acitivty and credentials for authentification :
scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']
# credentials = Credentials.from_service_account_file(key,scopes=scope)
credentials = ServiceAccountCredentials.from_json_keyfile_dict(st.secrets["gcp_service_account"],scope)
    

prospect_df = table_management.load_sheet(sheet_id,credentials,final_worksheet)
   
try:
        prospect_map = pm.create_map(prospect_df)
        folium_static(prospect_map)
except:
        st.write("Couldn't show the map")

load_map(prospect_df)

st.markdown("""---""")
st.header("3 prospect you could contact today :")
 
@st.cache
def create_sample_df(prospect_df):
    not_contacted = prospect_df[prospect_df["contacted"]=="No"]
    not_contacted = not_contacted[["company","city","brut_contact"]]
    sample_df = not_contacted.sample(n=3)
    return sample_df

sample_df = create_sample_df(prospect_df)

st.write(sample_df)

st.markdown("""---""")
st.header("Update the table :")

company1 = (sample_df.iloc[0]["company"],sample_df.index[0])
company2 = (sample_df.iloc[1]["company"],sample_df.index[1])
company3 = (sample_df.iloc[2]["company"],sample_df.index[2])

with st.form("Update prospects"):
    c1,c2,c3 = st.columns(3)
    with c1:
        phone_number_1 = st.text_input(f"{company1[0]} phone number", value=prospect_df.iloc[company1[1]].phone_number)
    with c2:
        phone_number_2 = st.text_input(f"{company2[0]} phone number", value=prospect_df.iloc[company2[1]].phone_number)
    with c3:
        phone_number_3 = st.text_input(f"{company3[0]} phone number", value=prospect_df.iloc[company3[1]].phone_number)
    c4,c5,c6 = st.columns(3)
    with c4:
        contact_person_1 = st.text_input(f"{company1[0]} contact person", value=prospect_df.iloc[company1[1]].contact_name)
    with c5:
        contact_person_2 = st.text_input(f"{company2[0]} contact person", value=prospect_df.iloc[company2[1]].contact_name)
    with c6:
        contact_person_3 = st.text_input(f"{company3[0]} contact person", value=prospect_df.iloc[company3[1]].contact_name)
    c7,c8,c9 = st.columns(3)
    with c7:
        email_1 = st.text_input(f"{company1[0]} email address", value=prospect_df.iloc[company1[1]].email)
    with c8:
        email_2 = st.text_input(f"{company2[0]} email address", value=prospect_df.iloc[company2[1]].email)
    with c9:
        email_3 = st.text_input(f"{company3[0]} email address", value=prospect_df.iloc[company3[1]].email)
    c10,c11,c12 = st.columns(3)
    with c10:
        contacted_1 = st.selectbox("Have been contacted ?",("No","Yes"),key=1)
    with c11:
        contacted_2 = st.selectbox("Have been contacted ?",("No","Yes"),key=2)
    with c12:
        contacted_3 = st.selectbox("Have been contacted ?",("No","Yes"),key=3)
        
    if st.form_submit_button(label="submit updates"):
        prospect_df.iloc[company1[1]]["phone_number"] = phone_number_1
        prospect_df.iloc[company2[1]]["phone_number"] = phone_number_2
        prospect_df.iloc[company3[1]]["phone_number"] = phone_number_3
        prospect_df.iloc[company1[1]]["contact_name"] = contact_person_1
        prospect_df.iloc[company2[1]]["contact_name"] = contact_person_2
        prospect_df.iloc[company3[1]]["contact_name"] = contact_person_3
        prospect_df.iloc[company1[1]]["email"] = email_1
        prospect_df.iloc[company2[1]]["email"] = email_2
        prospect_df.iloc[company3[1]]["email"] = email_3
        prospect_df.iloc[company1[1]]["contacted"]=contacted_1
        prospect_df.iloc[company2[1]]["contacted"]=contacted_2
        prospect_df.iloc[company3[1]]["contacted"]=contacted_3
        table_management.google_sheet_upload(prospect_df=prospect_df,credentials=credentials,sheet_id=sheet_id,wks_name=final_worksheet)

st.markdown("""---""")

I found the solution to my problem.

I am not really sure why but when I add the upload code to the form_submit_button, the dataframe is correctly uploaded in Google Sheets.

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