m-wrzr / streamlit-searchbox

Streamlit searchbox that dynamically updates and provides a list of suggestions based on a provided function
MIT License
253 stars 33 forks source link

`KeyError` in streamlit 1.20.0 #11

Closed rami3l closed 1 year ago

rami3l commented 1 year ago

The following snippet:

from time import sleep
from streamlit_searchbox import st_seachbox

def f(s):
    sleep(1) # Some time-consuming operation
    return [s + "1"]

selected_value = st_searchbox(f, key="theme_searchbox")
st.write(selected_value)

... generates the following error (some paths are redacted, but it should not matter):

Uncaught app exception
Traceback (most recent call last):
  File ".venv\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)
  in <module>
    main()
  in main
    theme = st_searchbox(ent_search, key="theme_searchbox")
  File ".venv\lib\site-packages\streamlit_searchbox\__init__.py", line 110, in st_searchbox
    return _process_search(search_function, key, value, rerun)
  File ".venv\lib\site-packages\streamlit_searchbox\__init__.py", line 40, in _process_search
    st.session_state[key]["options"] = [
  File ".venv\lib\site-packages\streamlit\runtime\state\session_state_proxy.py", line 90, in __getitem__
    return get_session_state()[key]
  File ".venv\lib\site-packages\streamlit\runtime\state\safe_session_state.py", line 104, in __getitem__
    raise KeyError(key)
KeyError: 'theme_searchbox'
m-wrzr commented 1 year ago

Thanks for the example, I've tested it locally and it seems to run fine for me.

Can you maybe provide some additional information? Are there any updates to the session state outside the provided function?

rami3l commented 1 year ago

@m-wrzr Thanks for your reply!

I apologize for forgetting to mention that this error is caught when I try to type something into the search box.

Reinstalling the venv doesn't work (I use pdm to manage my venv, if that helps), besides I can reproduce the error on my Mac as well (I used Windows in my previous example), so I guess this is a real issue instead of a misconfiguration.

I guess when streamlit is trying to refresh the page, the callback f is supposed to return immediately but sometimes it just can't (my point of using sleep in my example was to show this delay), causing this error.

Is it working alright on your machine? 🤔

m-wrzr commented 1 year ago

Hey, so I was able to reproduce the issue. It worked fine for me testing the example running the local module at first, so thanks again for the detailed error info.

It seems that due to calling the experimental_rerun function, thesession_state proxy becomes unavailable. This can happen anywhere within the function / script and lead to the key errors. The script execution is only stopped later and the previous thread still runs for a bit without proper context. I've added an early return and a guard for missing the specific streamlit key within the session_state proxy.

Can you try to update to 0.1.2? The error should be fixed now.


Below are some streamlit logs for reruns without any available data causing these issues.

023-03-26 09:52:39.370 Running script RerunData(query_string='', widget_states=None, page_script_hash='a0105f2cfe95c1a1489e17db1fec396a', page_name='')
2023-03-26 09:52:39.371 Ignoring event from non-current ScriptRunner: ScriptRunnerEvent.SCRIPT_STARTED
2023-03-26 09:52:39.345 Uncaught app exception
Traceback (most recent call last):
  File "/Users//dev/streamlit-searchbox/venv/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)
  File "/Users//dev/streamlit-searchbox/tests/example_f.py", line 16, in <module>
    selected_value = st_searchbox(f, key="theme_searchbox")
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users//dev/streamlit-searchbox/venv/lib/python3.11/site-packages/streamlit_searchbox-0.1.1-py3.11.egg/streamlit_searchbox/__init__.py", line 100, in st_searchbox
    options=st.session_state[key]["options"],
            ~~~~~~~~~~~~~~~~^^^^^
  File "/Users//dev/streamlit-searchbox/venv/lib/python3.11/site-packages/streamlit/runtime/state/session_state_proxy.py", line 90, in __getitem__
    return get_session_state()[key]
           ~~~~~~~~~~~~~~~~~~~^^^^^
  File "/Users//dev/streamlit-searchbox/venv/lib/python3.11/site-packages/streamlit/runtime/state/safe_session_state.py", line 104, in __getitem__
    raise KeyError(key)
KeyError: 'theme_searchbox'

Compared to the normal rerun data.

2023-03-26 09:52:39.343 Running script RerunData(query_string='', widget_states=widgets {
  id: "$$GENERATED_WIDGET_ID-fea4ab9838d973e8d35e09429fc8f134-theme_searchbox_react"
  json_value: "{\"interaction\":\"search\",\"value\":\"sdkijfoidsjgdfvmdofmvd\303\266ofijd<iorgjmjxi\303\266o, mjcoietc<wiulekyxzjsehtxjkywtneslivu<hgydmkvgznrdgihlcumkdzetigmxuyncditzkhdckucjzcnygmxaizxkyungli<s<iscgm4to<isc\"}"
}
rami3l commented 1 year ago

@m-wrzr The problem is indeed fixed on my side as well. Thanks a lot!