Hi Streamlit community,
I’m currently working on a Streamlit application where I’m encountering an issue with dropdown behavior that I’m struggling to resolve. Here’s a brief overview of the problem:
Issue: When selecting a circulariteitscode value, the dropdown should only display the corresponding materials for that specific code in that row. However, it currently displays materials from all circulariteitscode values that are available in the dataframe.
Background: In my MySQL database, I have three circulariteitscode values: 603, 409, and 407. Each circulariteitscode should have its own set of materials. For example, circulariteitscode 603 should only display three materials, but it’s displaying all nine materials from 603, 409, and 407 combined.
Approach: I’ve attempted to fetch data from the database for each circulariteitscode value and update the DataFrame accordingly. However, despite my efforts, the dropdown still shows all materials.
Code: Here’s a simplified version of my code:
import streamlit as st
from modules.db_con import db
import pandas as pd
st.title("🧪 Test Dropdown")
# Define DataFrame with dropdown options (simplified)
df_dropdown = pd.DataFrame([
{"circulariteitscode": 603, "materiaal": "beton", "productnaam": ""},
{"circulariteitscode": 409, "materiaal": "hout", "productnaam": ""},
{"circulariteitscode": 407, "materiaal": "staal", "productnaam": ""}
])
def fetch_data_from_mysql(abc_value):
"""
Fetch data from MySQL database based on the given 'abc' value.
Args:
abc_value (int): The 'abc' value to fetch data for.
Returns:
list: A list of 'productnaam' values fetched from the database.
"""
cursor = db.cursor()
cursor.execute("SELECT productnaam FROM materiaal WHERE circulariteitscode = %s", (int(abc_value),))
results = cursor.fetchall()
cursor.close()
return [result[0] for result in results]
# Iterate through unique 'abc' values in the DataFrame
unique_abc_values = df_dropdown["circulariteitscode"].unique()
# Fetch data from MySQL for each 'abc' value
all_custom_cat = []
for abc_value in unique_abc_values:
custom_cat = fetch_data_from_mysql(abc_value)
all_custom_cat.extend(custom_cat)
# Remove duplicates and None values using set and list comprehension
unique_custom_cat = list(set(x for x in all_custom_cat if x is not None))
# Update the DataFrame with the fetched data
df_dropdown["productnaam"] = (
df_dropdown["materiaal"]
.astype("category")
.cat.remove_categories(df_dropdown["materiaal"])
.cat.add_categories(unique_custom_cat)
)
# If the session state does not have 'edited_df_dropdown', add it with the initial DataFrame
if "edited_df_dropdown" not in st.session_state:
st.session_state.edited_df_dropdown = df_dropdown
# Display the DataFrame with the data editor
edited_df_dropdown = st.data_editor(st.session_state.edited_df_dropdown, hide_index=True)
# Update the session state with the edited DataFrame
st.session_state.edited_df_dropdown = edited_df_dropdown
Request for Help: I’m seeking assistance to resolve this issue. How can I ensure that the dropdown only displays materials corresponding to the selected circulariteitscode in that row?
Any insights, suggestions, or guidance would be greatly appreciated. Thank you in advance for your help!
Regards,
Adam