Hello,
I have a shared function that is used by multiple pages. When the widget doesn’t have key defined, Streamlit generates error about multiple widgets using the same autogenerated key and when I add the key parameter to widget, it stops working.
Has anyone has idea how to solve this issue?
Below is the code for the function.
import streamlit as st
import pandas as pd
from dblib import split_frame
import random
def sort_df_top_menu(df):
select_rand_key = random.random()
if len(df) > 0:
top_menu = st.columns([.11,.78,.11])
with top_menu[0]:
sort = st.radio("Sort Data", options=["Yes", "No"], horizontal=1, index=1)
if sort == "Yes":
with top_menu[1]:
sort_field = st.multiselect("Sort By", options=df.columns)
with top_menu[2]:
sort_direction = st.radio(
"Sort Direction", options=["⬆️", "⬇️"], horizontal=True
)
df = df.sort_values(
by=sort_field, ascending=sort_direction == "⬆️", ignore_index=True
)
return df
The call to this function is below from multiple pages.
data_df = sort_df_top_menu(data_df)
This is how the widget after adding the key parameter.
This is how the widget is when key parameter is removed.