Abstract
When I executed streamlit run main.py
, I got the following error.
AttributeError: module 'streamlit' has no attribute 'session_state'
Before I got the error…
(1) composed main.py
as follows
import streamlit as st
st.set_page_config(layout="wide")
import select_page as page1
import show_page as page2
import scraping_page as page3
pages = {
"対象サイトの選択": page1.page,
"新着情報一覧": page2.page,
"更新の実行": page3.page,
}
if "news" not in st.session_state:
st.session_state["news"] = {}
if "sites" not in st.session_state:
st.session_state["sites"] = {}
st.sidebar.title('ページの一覧')
selection = st.sidebar.radio("", list(pages.keys()))
if selection == "対象サイトの選択":
is_scraping_list = pages[selection]()
elif selection == "新着情報一覧":
pages[selection](st.session_state["news"])
elif selection == "更新の実行" :
news_dict = pages[selection]()
st.session_state["news"] = news_dict
(2) executed streamlit run main.py
I got the following error message.
AttributeError: module 'streamlit' has no attribute 'session_state'
Traceback:
File "/Users/toshiaki/anaconda3/envs/Campus-Dog/lib/python3.8/site-packages/streamlit/script_runner.py", line 337, in _run_script
exec(code, module.__dict__)
File "/Users/toshiaki/workspace/Campus-Dog/main.py", line 17, in <module>
if "news" not in st.session_state:
I thought empty dictionaries would be in sessions whose keys were news
and sites
.
Because documentation says as follows.
Initialization
if ‘key’ not in st.session_state:
st.session_state[‘key’] = ‘value’
Session State also supports attribute based syntax
if ‘key’ not in st.session_state:
st.session_state.key = ‘value’
I thought I could access to session data using st.session_state
so I mimicked them in my main.py
.
My development environment is like this:
MacOS Big Sur
Streamlit 0.81.1
Python 3.8.8
package management using Anaconda