Force Reload Eliminating Session Variable

Hello, I saw this post:

https://discuss.streamlit.io/t/force-reload-of-webpage/43793/1

which solved half of my problem. Specifically, I am in a situation where I need to get info from user, send it to chatGPT, wait like 15 minutes, and then, when the reponses come through, I display an st.download button and the user can download the responses. The way I am doing this is as follows (only showing relevant code):

if "xlsx_downloaded" not in st.session_state:
    st.session_state.xlsx_downloaded = False

...
with row6_col3:
    if st.session_state.xlsx_downloaded:
        st.write("Placeholder :popcorn: :balloon: :popcorn: :balloon:")
        with open('./outputs/summary.xlsx') as f:
            st.download_button('Download', f)
    else:
        st.write(" ")
...

Write Response to Output Folder
try:
        CreateWorkbook.create_workbook(response_map)
        st.session_state.xlsx_downloaded = True streamlit_js_eval(js_expressions="parent.window.location.reload()")

except Exception as e:
        st.write("Error in downloading: " + e)

Everything works fine and if the file is appropriately downloaded, the page refreshes . . . BUT . . . my session state variable appears to default back again to False. Is there something I am doing wrong or is there a way around this? Basically I just need the user to sit tight (I show a progress bar while they way) and when the doc is ready, I refresh page and allow them to download the file. I don’t want to show the download button before that b/c there is no file there to be downloaded until the download completes.

1 Like

Reloading the page kills the session and creates a new one. Why are you doing that?

1 Like

What i want to do is have a download button that only appears upon the occurrence of an event occurring on the backend - that event being the successful receipt of info from chatgpt. So i was hoping to set a session boolean have it be false initially, get the response from chatgpt, set the session boolean to True, and force a page refresh so the button would now show up. If u have a better suggestion for accomplishing the task i am open to that as well. I was hoping the above code would refresh WITHOUT clearing the session variable.

1 Like

I don’t think I understand what you mean. A common way of getting info is calling a function, when the function returns you have the info in the return value. That doesn’t seem to be what you are doing, but then I don’t know what it is instead.

Anyway, maybe you want st.rerun() instead of a reload?

1 Like

Would that re-run the script but keep the st.session_state?

1 Like

In terms of what I am trying to do, I have a page where a user enters info. I take that info. I send it to an API. I get back a response and put the response in a properly formatted .xlsx. That all works, but it does not occur instantaneously. It takes like 10-15 minutes. So . . . when the file is ready for download, I set a boolean - “download_complete = True” and when download_complete = True, I would like to render a download button to the user. It’s pretty common. You typically don’t give the user a button to download something unless that thing exists and is ready to be downloaded, right?

1 Like

Yes.

1 Like

So what does this mean in terms of python code?

I send it to an API. I get back a response

Something like this?

response = get_api_response(user_info)
1 Like

xlsx_downloaded = False
file_path = "./src/something/something/my_file.xlsx"

# Once the file is Saved, The Download Button Needs to Appear
if xlsx_downloaded:
    # Need to Redraw Page so either the javascript I am using or st.rerun()
   with open filepath as f:
        st.download_button(label="Downloading", data = f)
)

response = function # function that calls API and gets a complex data structure

file_path = "some file path"

wb.save(response, file_path)

xlsx_downloaded = True

I will try the st.rerun() once i get to my work computer. I haven’t tried it yet. If it re-renders the page without resetting session variables, that should do it.

1 Like

The logic in your description is simple, but you are complicating it in the code for no apparent reason. I suggest you do stuff in code in the same order as in your description.

response = function # function that calls API and gets a complex data structure

file_path = "some file path"
wb.save(response, file_path)

# Once the file is Saved, The Download Button Needs to Appear
# (at this point we DO know that it is saved)
file_path = "./src/something/something/my_file.xlsx"
with open filepath as f:
    st.download_button(label="Downloading", data = f)
)
1 Like

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