Hi all,
I’m relatively new to this awesome tool and would like to understand if it is possible to update the cache value with the new value.
Background:
I am developing a tool where data is loaded from the database, displayed using Ag-Grid, where the user can manipulate the data on the grid, and upon changes, the data should then overwrite the cached data with the new data so that whenever the page is refreshed, “address_frame” takes the last data updated (from the grid) rather than the raw data from the database. Lastly, user has the option to store the data to the database once he’s done with the manipulation.
Where I need support!
I would like to understand how to update the cache once I have the updated dataframe.
Right now, even after I assign the updated dataframe to the variable address_frame, it will still contain the cached data that was loaded from the database
Code
Code snippet:
@st.experimental_memo(show_spinner=True,suppress_st_warning=True)
def load_Raw_Address():
with st.spinner('Loading Cleansed Address Data From Database. This may take a while...'):
query_raw='SELECT * FROM [dbo].[DQ_Raw_Address]'
address_frame_raw = pd.read_sql(query_raw,con)
return address_frame_raw
#Assign raw data to address_frame
address_frame = load_Raw_Address()
#Show on AgGrid, user can manipulate the data on the grid
grid_return = AgGrid(address_frame, gridoptions ,editable=True ,allow_unsafe_jscode=True,theme = 'balham' , width = "100%", height = "800px")
#manipulated data is ultimately assigned to the address_frame again.
#NOTE: THIS NEEDS TO REPLACE PREVIOUS CACHED VALUE WITH NEW ONE
address_frame = grid_return['data']
Actual behavior:
Currently, If i manipulate the data on the dataframe and refresh the page, the cached data is still stored as raw data from DB and is loaded.
This sounds like a good use of st.session_state, where you could check to see if “edited_dataframe” exists in the session state, and if it doesn’t, load the data from the cached function.
Something like this
import pandas as pd
import streamlit as st
@st.experimental_memo(show_spinner=True, suppress_st_warning=True)
def load_Raw_Address():
with st.spinner(
"Loading Cleansed Address Data From Database. This may take a while..."
):
query_raw = "SELECT * FROM [dbo].[DQ_Raw_Address]"
address_frame_raw = pd.read_sql(query_raw, con)
return address_frame_raw
# Assign raw data to address_frame
if "address_frame" not in st.session_state:
address_frame = load_Raw_Address()
st.session_state["address_frame"] = address_frame
address_frame = st.session_state["address_frame"]
# Show on AgGrid, user can manipulate the data on the grid
grid_return = AgGrid(
address_frame,
gridoptions,
editable=True,
allow_unsafe_jscode=True,
theme="balham",
width="100%",
height="800px",
)
# manipulated data is ultimately assigned to the address_frame again.
# NOTE: THIS NEEDS TO REPLACE PREVIOUS CACHED VALUE WITH NEW ONE
st.session_state["address_frame"] = grid_return["data"]
Thankyou for the quick reply. The solution was very helpful in understanding the whole concept.
I implemented your code but now face an issue that needs support.
This is what i observe when the loop runs for the first time. (Pretty good so far)
Observe after changing the value, the session state is updated with the new value in the grid below, but not in the grid above (For the same code that you provided). If I change the value again now, the code reruns again and the following output can be seen.
Is there any onChange() method implementable for Aggrid that can update the value of the Session state before rerunning?
I believe this is the missing piece to this issue.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.