Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught app exception when using st_folium with hmac authentication and requests #233

Open
amedyukhina opened this issue Oct 18, 2024 · 0 comments

Comments

@amedyukhina
Copy link

First of all, thanks for the great tool!

I ran into an error when using the following three components in the same streamlit app:

  • authentication with hmac
  • a st_folium map component
  • an api call with requests that returns json

Plotting the map and making the api call in that sequence seems to be clearing the session_state and results in failed authentication. The error does not occur when using folium_static, so I assume the issue is with streamlit-folium, but apologies if my judgement is wrong.

The full error:

2024-10-18 14:55:31.107 Uncaught app exception
Traceback (most recent call last):
  File "/home/anna/test/.env/lib/python3.12/site-packages/streamlit/runtime/state/session_state.py", line 455, in __getitem__
    return self._getitem(widget_id, key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/anna/test/.env/lib/python3.12/site-packages/streamlit/runtime/state/session_state.py", line 500, in _getitem
    raise KeyError
KeyError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/anna/test/.env/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/exec_code.py", line 88, in exec_func_with_error_handling
    result = func()
             ^^^^^^
  File "/home/anna/test/.env/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 541, in code_to_exec
    self._session_state.on_script_will_rerun(
  File "/home/anna/test/.env/lib/python3.12/site-packages/streamlit/runtime/state/safe_session_state.py", line 66, in on_script_will_rerun
    self._state.on_script_will_rerun(latest_widget_states)
  File "/home/anna/test/.env/lib/python3.12/site-packages/streamlit/runtime/state/session_state.py", line 559, in on_script_will_rerun
    self._call_callbacks()
  File "/home/anna/test/.env/lib/python3.12/site-packages/streamlit/runtime/state/session_state.py", line 572, in _call_callbacks
    self._new_widget_state.call_callback(wid)
  File "/home/anna/test/.env/lib/python3.12/site-packages/streamlit/runtime/state/session_state.py", line 273, in call_callback
    callback(*args, **kwargs)
  File "/home/anna/test/app.py", line 14, in password_entered
    if hmac.compare_digest(st.session_state["password"], st.secrets["password"]):
                           ~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/home/anna/test/.env/lib/python3.12/site-packages/streamlit/runtime/state/session_state_proxy.py", line 100, in __getitem__
    return get_session_state()[key]
           ~~~~~~~~~~~~~~~~~~~^^^^^
  File "/home/anna/test/.env/lib/python3.12/site-packages/streamlit/runtime/state/safe_session_state.py", line 94, in __getitem__
    return self._state[key]
           ~~~~~~~~~~~^^^^^
  File "/home/anna/test/.env/lib/python3.12/site-packages/streamlit/runtime/state/session_state.py", line 457, in __getitem__
    raise KeyError(_missing_key_error_message(key))
KeyError: 'st.session_state has no key "password". Did you forget to initialize it? More info: https://docs.streamlit.io/develop/concepts/architecture/session-state#initialization'

Minimum example to reproduce:

import hmac

import folium
import requests
import streamlit as st
from streamlit_folium import folium_static, st_folium


def check_password():
    """Returns `True` if the user had the correct password."""

    def password_entered():
        """Checks whether a password entered by the user is correct."""
        if hmac.compare_digest(st.session_state["password"], st.secrets["password"]):
            st.session_state["password_correct"] = True
            del st.session_state["password"]  # Don't store the password.
        else:
            st.session_state["password_correct"] = False

    # Return True if the password is validated.
    if st.session_state.get("password_correct", False):
        return True

    # Show input for password.
    st.text_input(
        "Password", type="password", on_change=password_entered, key="password"
    )
    if "password_correct" in st.session_state:
        st.error("😕 Password incorrect")
    return False


if not check_password():
    st.stop()  # Do not continue if check_password is not True.


map = folium.Map()
st_folium(map)
# folium_static(map)


# url = "http://example.com"
url = "https://api.sampleapis.com/coffee/hot"
response = requests.get(url)
st.write("Response:", response.json())

The error does not occur if st_folium is replaced with folium_static or "https://api.sampleapis.com/coffee/hot" is replaced with "http://example.com" (json vs plain text) or if the api request is made before plotting the map.

Python environment:

altair==5.4.1
attrs==24.2.0
blinker==1.8.2
branca==0.8.0
cachetools==5.5.0
certifi==2024.8.30
charset-normalizer==3.4.0
click==8.1.7
folium==0.17.0
gitdb==4.0.11
GitPython==3.1.43
idna==3.10
Jinja2==3.1.4
jsonschema==4.23.0
jsonschema-specifications==2024.10.1
markdown-it-py==3.0.0
MarkupSafe==3.0.2
mdurl==0.1.2
narwhals==1.9.4
numpy==2.1.2
packaging==24.1
pandas==2.2.3
pillow==10.4.0
protobuf==5.28.2
pyarrow==17.0.0
pydeck==0.9.1
Pygments==2.18.0
python-dateutil==2.9.0.post0
pytz==2024.2
referencing==0.35.1
requests==2.32.3
rich==13.9.2
rpds-py==0.20.0
six==1.16.0
smmap==5.0.1
streamlit==1.39.0
streamlit_folium==0.23.1
tenacity==9.0.0
toml==0.10.2
tornado==6.4.1
typing_extensions==4.12.2
tzdata==2024.2
urllib3==2.2.3
watchdog==5.0.3
xyzservices==2024.9.0

python3.12

Other details:

  • OS: Ubuntu 24.04.1
  • browser: Firefox 131.0.3 (64-bit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant