Unchecked runtime.lastError: The message port closed before a response was received

The app is running locally. Streamlit version is 1.38.0. Python version is 3.12.4. I have a simple page that shows a selectbox of two values. I’m printing the value selected. It always shows the default value. When I select the second value, it’s not selected. When I checked the chrome → inspect → console it showed this message “Unchecked runtime.lastError: The message port closed before a response was received.”

Here’s the code:

import streamlit as st
import os
import uuid
from datetime import datetime

# Function to handle file uploads and save with specific filenames
def save_uploaded_file(uploaded_file, data_type, site):
    save_dir = ''
    filename = ''

    # Generate the timestamp
    timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')

    if data_type == 'rv_tools':
        save_dir = f'static/customer_data/{site}'
        filename = f"{site}_RVTools_{timestamp}.xlsx"
    elif data_type == 'app_vm_mapping':
        save_dir = f'static/customer_data/'
        filename = f"App_VM_Mapping_{timestamp}.xlsx"

    # Create the directory if it doesn't exist
    os.makedirs(save_dir, exist_ok=True)

    # Save the file
    save_path = os.path.join(save_dir, filename)
    with open(save_path, 'wb') as f:
        f.write(uploaded_file.getbuffer())   

    st.success(f"File saved as {filename} in {save_dir}")


def upload_env_data():
    st.write("Upload environment data for main and disaster recovery sites.")
    st.markdown("<h3 style='text-align: left; background-color: #0D5265; color: white; padding: 10px 20px'>Environment Data Uploads</h3>", unsafe_allow_html=True)

    # Generate a unique key using UUID
    unique_key = str(uuid.uuid4())

    # Option to select between Main Site and DR Site
    site = st.selectbox("Select Site for RVTools Upload", ("Main", "DR"), key=f"site_selectbox_key_{unique_key}")

    # Upload Main Site RVTools
    site_rv_tools_file = st.file_uploader(f"Upload RVTools for {site} site", type=["xlsx"], key=f"{site}_rvtools_key_{unique_key}")
    if site_rv_tools_file is not None:
        save_uploaded_file(site_rv_tools_file, 'rv_tools', site)

    # Upload Application VM Mapping
    vm_mapping_file = st.file_uploader("Upload Application VM Mapping", type=["xlsx"], key=f"app_vm_mapping_key_{unique_key}")
    if vm_mapping_file is not None:
        save_uploaded_file(vm_mapping_file, 'app_vm_mapping', None)

# Call the function in your Streamlit app
if __name__ == "__main__":
    upload_env_data()

Hey @Ahmad3! I am not sure whether this is the root cause of the issue, but one problem with your selectbox is that you pass a new key on every re-run because unique_key is regenerated every time you interact with the app. You have to make sure that on every re-run the key of widget stays stable, otherwise the Streamlit app removes the widgets and replaces it with a new one. This is the reason why your selection is reset and you cannot change it.

@raethlein Thanks a lot. It’s resolved after I removed the unique id.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.