Update st.bar_chart using st.sessionstate and multiprocessing

Greetings,
I am having an issue with session_state update. The community help on topics related to multiprocessing helped me understand a lot about data sharing across processes.
I am trying to use a session_state to update a st.bar_chart.
but I keep getting this error 'AttributeError: st.session_state has no attribute "data". Did you forget to initialize it? and the app will spin indefinitely until I shut it down.
If I comment out the st.bar_chart line, then everything under the __main__ block will work just fine in seconds.
my ultimate intend is to use the multiprocessing step to frequently pull the data and pass it to the char.
Not sure of why I am getting this error and how I can fix it.

any help is greatly appreciated. below is the code I am running locally on streamlit 1.29, python 3.10.13

def open_file(file_path, out_name): #read data
    
    out_name.df=pd.read_csv(file_path)


if ('data' not in st.session_state):

    st.session_state['data']=pd.DataFrame() #empty data frame
    
st.write(' Bar Char ')
st.bar_chart(st.session_state.data, x='x_label',y='y_label') 
	



if __name__=="__main__":

    manager=multiprocessing.Manager()
    out_data=manager.Namespace()  # namespace used to save new dataset
    
    p1=multiprocessing.Process(target=open_file,args=(data_path ,out_data) )
    p1.start()
    p1.join()

Hi @jeanoscar

How many threads or cores does your app have access to, if it’s running on a single core or thread then it could be that clearing up st.bar_chart frees up a core/thread that could then be used for processing the app.

Instead of using multiprocessing to update the bar chart, you could create a placeholder using st.empty() and subsequently update the placeholder with an updated bar chart.

Here’s some info from the Docs:

Hope this helps!

the st.empty does the trick. Thanks a lot

1 Like

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