I have registered an atexit function like this:
import streamlit as st
from azure.search.documents.indexes import SearchIndexClient
from azure.core.credentials import AzureKeyCredential
@atexit.register
def delete_vector_index():
admin_client = SearchIndexClient(endpoint=st.session_state.vector_store_address,
index_name=st.session_state.index_name,
credential=AzureKeyCredential(st.session_state.vector_store_password))
admin_client.delete_index(st.session_state.index_name)
if "initialized" not in st.session_state:
st.session_state.initialized = True
st.session_state.vector_store_address: str = "<my endpoint>"
st.session_state.vector_store_password: str = "<admin key>"
st.session_state.index_name: str = "<index name>"
But everytime the interpreter finishes, this is the error message I get "AttributeError: st.session_state has no attribute "vector_store_address". Did you forget to initialize it? More info: https://docs.streamlit.io/library/advanced-features/session-state#initialization"
Why is the st.session_state variable inaccessible from the atexit function when it can be easily accessed from my other functions? Does anyone know what to do about this?
The modified code below includes your code that accesses the Streamlit session state variable with the atexit function.
Code :
Output :
It ran successfully as shown below:
I got the below output in the browser.
Next, the index was deleted while stopping Streamlit, as shown below.