444B / streamlit-analytics2

👀 Track & visualize user interactions with your streamlit app
MIT License
14 stars 2 forks source link

[BUG] - streamlit crashed when running from python directly #11

Closed 444B closed 4 months ago

444B commented 4 months ago

Describe the bug When running streamlit run main.py the files runs without issue However, when running python3 main.py it will crash and attribute it to streamlit-analytics2

To Reproduce Steps to reproduce the behavior:

  1. write boilerplate streamlit file as main.py
  2. install streamlit and streamlit-analytics2
  3. enter python3 main.py

Expected behavior streamlit should run as expected, as seen with streamlit run main.py Confirmed that this has no correlation to the streamlit-analytics2 migration as it appears on the upstream as well

Error text

python3 main.py 
2024-03-02 08:46:19.021 WARNING streamlit.runtime.state.session_state_proxy: Session state does not function when running a script without `streamlit run`
Traceback (most recent call last):
  File "/home/codespace/.python/current/lib/python3.10/site-packages/streamlit/runtime/state/session_state.py", line 398, in __getitem__
    return self._getitem(widget_id, key)
  File "/home/codespace/.python/current/lib/python3.10/site-packages/streamlit/runtime/state/session_state.py", line 443, in _getitem
    raise KeyError
KeyError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/codespace/.python/current/lib/python3.10/site-packages/streamlit/runtime/state/session_state_proxy.py", line 119, in __getattr__
    return self[key]
  File "/home/codespace/.python/current/lib/python3.10/site-packages/streamlit/runtime/state/session_state_proxy.py", line 90, in __getitem__
    return get_session_state()[key]
  File "/home/codespace/.python/current/lib/python3.10/site-packages/streamlit/runtime/state/safe_session_state.py", line 91, in __getitem__
    return self._state[key]
  File "/home/codespace/.python/current/lib/python3.10/site-packages/streamlit/runtime/state/session_state.py", line 400, in __getitem__
    raise KeyError(_missing_key_error_message(key))
KeyError: 'st.session_state has no key "last_time". Did you forget to initialize it? More info: https://docs.streamlit.io/library/advanced-features/session-state#initialization'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/workspaces/codespaces-blank/main.py", line 4, in <module>
    with streamlit_analytics.track():
  File "/home/codespace/.python/current/lib/python3.10/contextlib.py", line 135, in __enter__
    return next(self.gen)
  File "/home/codespace/.python/current/lib/python3.10/site-packages/streamlit_analytics2/main.py", line 425, in track
    start_tracking(
  File "/home/codespace/.python/current/lib/python3.10/site-packages/streamlit_analytics2/main.py", line 276, in start_tracking
    _track_user()
  File "/home/codespace/.python/current/lib/python3.10/site-packages/streamlit_analytics2/main.py", line 78, in _track_user
    counts["total_time_seconds"] += (now - st.session_state.last_time).total_seconds()
  File "/home/codespace/.python/current/lib/python3.10/site-packages/streamlit/runtime/state/session_state_proxy.py", line 121, in __getattr__
    raise AttributeError(_missing_attr_error_message(key))
AttributeError: st.session_state has no attribute "last_time". Did you forget to initialize it? More info: https://docs.streamlit.io/library/advanced-features/session-state#initialization
444B commented 4 months ago

Closing this as it seems to WAI. proof: https://docs.streamlit.io/knowledge-base/using-streamlit/how-do-i-run-my-streamlit-script One should run streamlit via streamlit run xyz.py and not python3 xyz.py