Use session state in debugger

Has anyone found that they can’t use the debugger in their streamlit code anymore once they use the native state management? Now whenever I use the debugger, it’s as if it can’t store any data in the st.session_state object.

When I run the app, the code runs fine. However when I use my debugger in pycharm, I can’t get past the first line that accesses the st.session_state call because the “dictionary” is empty.

See this example:

print(st.session_state)
{}
st.session_state['foo'] = 'bar'
st.session_state['foo']
Traceback (most recent call last):
  File "/opt/anaconda3/envs/yogen2/lib/python3.7/site-packages/streamlit/state/session_state.py", line 381, in __getitem__
    return self._getitem(widget_id, key)
  File "/opt/anaconda3/envs/yogen2/lib/python3.7/site-packages/streamlit/state/session_state.py", line 424, in _getitem
    raise KeyError
KeyError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/opt/anaconda3/envs/yogen2/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3457, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-4-9404e5a9ae15>", line 1, in <module>
    st.session_state['foo']
  File "/opt/anaconda3/envs/yogen2/lib/python3.7/site-packages/streamlit/state/session_state.py", line 666, in __getitem__
    return state[key]
  File "/opt/anaconda3/envs/yogen2/lib/python3.7/site-packages/streamlit/state/session_state.py", line 383, in __getitem__
    raise KeyError(_missing_key_error_message(key))
KeyError: 'st.session_state has no key "foo". Did you forget to initialize it? More info: https://docs.streamlit.io/library/advanced-features/session-state#initialization'
print(st.session_state)
{}

How are we supposed to develop applications if we can’t use the debugger mode in our IDEs?

1 Like

anyone know the answer to this or have workarounds? surely I’m not the only one

I’m running into the same problem, have you found any workarounds?

Yes. Create a file called dev.py and then setup your application to run that.

from streamlit import bootstrap


def main():
    real_script = 'app.py'

    bootstrap.run(real_script, f'run.py {real_script}', [], {})


if __name__ == "__main__":
    main()

Configure your IDE to run dev.py instead of app.py, and then the bugger works because the actual streamlit server is kick started and paused in real time on your browser. st.session_state works then