I am using Python 3.11 with Streamlit 1.28.0 locally on a Mac. I am trying to append to a text_area
using session_state
:
import time
import streamlit as st
st.text_area("Logs", key="logs_text_area")
for i in range(4):
st.session_state.logs_text_area += f"Log {i}"
time.sleep(1)
When I run this, I just get:
2023-11-06 12:18:25.199 Uncaught app exception
Traceback (most recent call last):
File "/Users/user/code/repo/venv/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 534, in _run_script
exec(code, module.__dict__)
File "/Users/user/code/repo/web.py", line 8, in <module>
st.session_state.logs_text_area += f"Log {i}"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/code/repo/venv/lib/python3.11/site-packages/streamlit/runtime/metrics_util.py", line 396, in wrapped_func
result = non_optional_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/code/repo/venv/lib/python3.11/site-packages/streamlit/runtime/state/session_state_proxy.py", line 125, in __setattr__
self[key] = value
~~~~^^^^^
File "/Users/user/code/repo/venv/lib/python3.11/site-packages/streamlit/runtime/metrics_util.py", line 396, in wrapped_func
result = non_optional_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/code/repo/venv/lib/python3.11/site-packages/streamlit/runtime/state/session_state_proxy.py", line 103, in __setitem__
get_session_state()[key] = value
~~~~~~~~~~~~~~~~~~~^^^^^
File "/Users/user/code/repo/venv/lib/python3.11/site-packages/streamlit/runtime/state/safe_session_state.py", line 94, in __setitem__
self._state[key] = value
~~~~~~~~~~~^^^^^
File "/Users/user/code/repo/venv/lib/python3.11/site-packages/streamlit/runtime/state/session_state.py", line 457, in __setitem__
raise StreamlitAPIException(
streamlit.errors.StreamlitAPIException: `st.session_state.logs_text_area` cannot be modified after the widget with key `logs_text_area` is instantiated.
What am I doing wrong? How can one continually append to a text_area
using session_state
?