KeyError: 'st.session_state has no key

Running app locally

Error Message:
KeyError: ‘st.session_state has no key “slider_range”. Did you forget to initialize it? More info: Add statefulness to apps - Streamlit Docs

Traceback:

File "C:\Users\hp\AppData\Local\Programs\Python\Python312\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 531, in _run_script
    self._session_state.on_script_will_rerun(rerun_data.widget_states)File "C:\Users\hp\AppData\Local\Programs\Python\Python312\Lib\site-packages\streamlit\runtime\state\safe_session_state.py", line 63, in on_script_will_rerun
    self._state.on_script_will_rerun(latest_widget_states)File "C:\Users\hp\AppData\Local\Programs\Python\Python312\Lib\site-packages\streamlit\runtime\state\session_state.py", line 504, in on_script_will_rerun
    self._call_callbacks()File "C:\Users\hp\AppData\Local\Programs\Python\Python312\Lib\site-packages\streamlit\runtime\state\session_state.py", line 517, in _call_callbacks
    self._new_widget_state.call_callback(wid)File "C:\Users\hp\AppData\Local\Programs\Python\Python312\Lib\site-packages\streamlit\runtime\state\session_state.py", line 261, in call_callback
    callback(*args, **kwargs)File "C:\Users\hp\Desktop\streamlit\slider.py", line 14, in update_input_boxes
    min_val, max_val = st.session_state['slider_range']
                       ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^File "C:\Users\hp\AppData\Local\Programs\Python\Python312\Lib\site-packages\streamlit\runtime\state\session_state_proxy.py", line 90, in __getitem__
    return get_session_state()[key]
           ~~~~~~~~~~~~~~~~~~~^^^^^File "C:\Users\hp\AppData\Local\Programs\Python\Python312\Lib\site-packages\streamlit\runtime\state\safe_session_state.py", line 91, in __getitem__
    return self._state[key]
           ~~~~~~~~~~~^^^^^File "C:\Users\hp\AppData\Local\Programs\Python\Python312\Lib\site-packages\streamlit\runtime\state\session_state.py", line 400, in __getitem__
    raise KeyError(_missing_key_error_message(key))

Streamlit version - 1.30.0
Python Version - 3.12.1

I have created a slider in a different file called slider.py

The code is as follows:-

import streamlit as st

def interactive_range_slider(title, min_value_key, max_value_key):
    st.text(title)

    # Initialize session state for min and max values
    if min_value_key not in st.session_state:
        st.session_state[min_value_key] = 0.8
    if max_value_key not in st.session_state:
        st.session_state[max_value_key] = 1.2

    # Function to update input boxes from slider
    def update_input_boxes():
        min_val, max_val = st.session_state['slider_range']
        if min_val > 1.0:
            min_val = 1.0
        if max_val < 1.0:
            max_val = 1.0
        st.session_state[min_value_key] = min_val
        st.session_state[max_value_key] = max_val

    # Create two columns for min and max inputs in the sidebar
    col1, col2 = st.columns(2)

    # Min value input
    with col1:
        min_value_str = st.text_input("Min", value=str(st.session_state[min_value_key]), key=f"min_{min_value_key}")

    # Max value input
    with col2:
        max_value_str = st.text_input("Max", value=str(st.session_state[max_value_key]), key=f"max_{max_value_key}")

    # Manual validation and applying constraints
    try:
        min_value = float(min_value_str)
        max_value = float(max_value_str)
        if min_value < 0.0 or max_value > 3.0 or min_value > max_value or min_value > 1.0 or max_value < 1.0:
            raise ValueError
        st.session_state[min_value_key] = min_value
        st.session_state[max_value_key] = max_value
    except ValueError:
        st.error("Please enter valid min and max values (0.0 <= min <= 1, 1 <= max <= 3.0, min <= max).")

    # Range slider in the sidebar
    slider_range = st.slider(
        "Select Range",
        0.0, 
        3.0, 
        (st.session_state[min_value_key], st.session_state[max_value_key]), 
        step= 0.1, 
        on_change=update_input_boxes, 
        key=f'slider_range_{title}'
    )
    
    return slider_range

I am trying to use the slider like this in my main file:-

    interactive_range_slider("Slider 1:", "min_value_1", "max_value_1")
    interactive_range_slider("Slider 2", "min_value_2", "max_value_2")
    interactive_range_slider("Slider 3", "min_value_3", "max_value_3")
    interactive_range_slider("Slider 4", "min_value_4", "max_value_4")

But I am getting the above error.

Is there any way I can make a reusable interacive slider like the one I have made and use it the way as shown here?

Any help is appreciated, thank you!!

Use the following to differentiate which key to use.

min_val, max_val = st.session_state[f'slider_range_{title}']