Summary
KeyError: ‘st.session_state has no key “username”. Did you forget to initialize it? More info: Add statefulness to apps - Streamlit Docs’
Steps to reproduce
i create a mutipage with treamlit_option_menu. it have three pages is as follow:tax,test,xm
i succed to login the main page,but when i swith the other page ,for exampe tax page,it error
st.session_state has no key “username”.
Code snippet:
import streamlit as st
from streamlit_option_menu import option_menu
from menuPages import tax,test,xm
st.set_page_config(
page_title="菜单与登陆相结合",
layout="wide",)
st.markdown(
"""
<style>
.css-1oe5cao {display: none;}
</style>
""", unsafe_allow_html=True
)
def menu():
#主程序-带菜单
v_menu=["tax","test","xm"]
with st.sidebar:
st.header("main form")
selected=option_menu(
menu_title=None,
options=v_menu,
icons=None,menu_icon="menu-down",default_index=0,)
if selected=="tax":
tax.createPage()
if selected=="test":
test.createPage()
if selected=="xm":
xm.createPage()
def check_password()->tuple:
"""Returns `True` if the user had a correct password."""
def password_entered()->tuple:
"""Checks whether a password entered by the user is correct."""
if (
st.session_state["username"] in st.secrets["passwords"]
and st.session_state["password"]
== st.secrets["passwords"][st.session_state["username"]]
):
# del st.session_state["password"] # don't store username + password
# del st.session_state["username"]
return True,st.session_state["username"]
else:
return False
st.title("登陆")
st.text_input("用户名:", key="username") #相当于将输入的用户名,存到st.session_state["password"]=
st.text_input("密码:", type="password", key="password")
if st.button("登陆"):
if password_entered():
st.session_state.password_correct = True
st.experimental_rerun()
else:
st.error("😕 错误的用户名或密码")
return True,st.session_state["username"]
# Streamlit application
# Streamlit application
def main():
if "password_correct" not in st.session_state:
st.session_state.password_correct = False
if st.session_state["password_correct"]==False:
status,username=check_password()
else:
st.title("main page")
col1,col2=st.sidebar.columns(2)
with col1:
st.write("当前用户:"+st.session_state["username"])
with col2:
st.button("注销")
menu()
if __name__ == "__main__":
main()
If applicable, please provide the steps we should take to reproduce the error or specified behavior.
Expected behavior:
File "D:\python\web\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 552, in _run_script
exec(code, module.__dict__)File "D:\python\web\b.py", line 76, in <module>
main()File "D:\python\web\b.py", line 70, in main
st.write("当前用户:"+st.session_state["username"])File "D:\python\web\lib\site-packages\streamlit\runtime\state\session_state_proxy.py", line 90, in __getitem__
return get_session_state()[key]File "D:\python\web\lib\site-packages\streamlit\runtime\state\safe_session_state.py", line 113, in __getitem__
return self._state[key]File "D:\python\web\lib\site-packages\streamlit\runtime\state\session_state.py", line 378, in __getitem__
raise KeyError(_missing_key_error_message(key))