Global variables don't get affected in Streamlit blocks

I have a code like this

import streamlit as st
db = {}
menu = st.sidebar.radio(...)
if menu == ... :
    db['...'] = '.....'
else:
    ...

Now if I inspect that db variable in the global scope or any other local scope it comes empty, any modification that happened inside of a streamlit blocked is like it never happened. Can someone help me with this?

Hi @Kareem_Amr,

I think the db variable is getting reset to the empty dict every time script runner is re-running your script on change. If you are looking to keep state in a session, have a look at this link, Multi-page app with session state

1 Like