Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError (only in terminal) when accessing session_state key in a multipage app #8700

Closed
3 of 4 tasks
luxiaolei opened this issue May 17, 2024 · 4 comments
Closed
3 of 4 tasks
Labels
feature:multipage-apps feature:st.session_state status:cannot-reproduce Bug cannot be reproduced by the Streamlit team type:bug Something isn't working

Comments

@luxiaolei
Copy link

luxiaolei commented May 17, 2024

Checklist

  • I have searched the existing issues for similar issues.
  • I added a very descriptive title to this issue.
  • I have provided sufficient information below to help reproduce this issue.

Summary

I have encountered an issue when using st.session_state in a multipage Streamlit app. The app runs without any errors in the browser, and I can successfully access and modify the st.session_state values. However, in the terminal where I run the app, I get a KeyError indicating that the key is missing from st.session_state.

Reproducible Code Example

No response

Steps To Reproduce

  1. Create a Streamlit app with the following directory structure
    ├── app.py
    └── pages
    └── mypage.py

  2. In app.py, add the following code:

import streamlit as st

if 'my_val' not in st.session_state:
    st.session_state.my_val = 1
  1. In mypage.py , add the following code:
import streamlit as st

st.session_state.my_val = st.session_state.my_val + 1
st.write(st.session_state.my_val)
  1. Start the Streamlit app using the command: streamlit run app.py
  2. Navigate between the main page and mypage.py in the browser.
  3. After a while, check the error message in the terminal.

Expected Behavior

The terminal should not display any KeyError

Current Behavior

The app runs fine in the browser, and my_val increments as expected when navigating between pages.
However, the terminal displays a KeyError indicating that the key "my_val" is missing from st.session_state.

Is this a regression?

  • Yes, this used to work in a previous version.

Debug info

  • Streamlit version: 1.34.0
  • Python version: 3.11
  • Operating System: macOS
  • Browser: Safari

Additional Information

No response

@luxiaolei luxiaolei added status:needs-triage Has not been triaged by the Streamlit team type:bug Something isn't working labels May 17, 2024
Copy link

If this issue affects you, please react with a 👍 (thumbs up emoji) to the initial post.

Your feedback helps us prioritize which bugs to investigate and address first.

Visits

@Asaurus1
Copy link
Contributor

I suspect this will go away if you add a if "my_val" in st.session_state guard to your subpage, but agree that doesn't explain why you would get an issue only in your terminal.

@Asaurus1
Copy link
Contributor

I'm not able to reproduce this error with the latest version of streamlit. I am able to produce key errors if I refresh and go directly to the "mypage.py" page (e.g. start a session on that page) which makes sense, since my_val doesn't exist yet. But those key errors also show up in the app.

@raethlein raethlein added status:cannot-reproduce Bug cannot be reproduced by the Streamlit team and removed status:needs-triage Has not been triaged by the Streamlit team labels May 21, 2024
@raethlein
Copy link
Collaborator

Thanks @Asaurus1 for responding to this! I agree with your assessment that in general if "my_val" in st.session_state should guard against this behavior. Indeed, if you start the app and go directly to /mypage, I get a key error, which is expected as explained above. As I am also not able to reproduce any other behavior, I am going to close the issue. @luxiaolei feel free to re-open the issue if you have more details on how this can be reproduced or feel the original issue was misunderstood 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature:multipage-apps feature:st.session_state status:cannot-reproduce Bug cannot be reproduced by the Streamlit team type:bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants