I have a data frame which contains 3 columns. I’d set this up to filter using st.selectbox (x3) so the user can:
identify the row identifier column
identify the postcode column
optionally, select a column to filter on
conditional on 3 having a selection, a multiselect appears to allow the user to select the criteria / features to filter the dataframe on.
This is all working fine, though I wondered if there is a feature I’m missing of the data frame that would avoid having several selectboxes (?). My main issue is I’ve added a check box colum using st.data_editor as I’d also like the user to be able to de-select rows in addition to the above filtering.
This is working, but, is rendering a dataframe version with the checkbox column, in addition to the second dataframe which has the st.selectbox filters. I’d like a way to combine this functionality, so I display just one df, with filters as well as the check box. is this possible? TIA(!).
Hi @Mr.E - This isn’t a helpful response to your inquiry but instead a question about how you’re doing this. I’m fairly new to Streamlit and have just started using the data_editor but I’m not sure I saw a feature to provide a filter to the user. Would you mind sharing a snippet of your code? Would this be using the column_config? I think I need to find a few examples to figure this out more.
Hi @bruestle - thanks for coming back to me on this thread. I’m new to streamlit too (1 week in ) and first experimented with the data_editor yesterday so you’re in good company.
Happy to share the code snippet - please see below, hope this is useful though any questions fire away and I’ll try to clarify. This code is my work-around to provide a user with a means of filtering their dataset. The approach below requires a certain format of dataset currently (mainly to simplify the problem while getting started with this project), but the intention would be to adjust this so that it scales to more or less any df (with the requirement that this contains at least a column for unique row ID, and the another containing UK postcode).
#dummy data
current_site_names = ['site_1', 'site_2', 'site_3', 'site_4', 'site_5']
current_site_postcodes = ['S40 3HW', 'S45 9LX', 'DE56 1WH', 'S40 1QN', 'DE55 7BD']
current_site_some_classifier = [1, 2, 3, 1, 3]
#dict as intermediary step to creating dataframe to display to the user (hardcoded approach to create a df to build around, would need replacing with an alternative method to ingest actual site information)
data_dict = {'Site': current_site_names, 'Post_code': current_site_postcodes, 'Some_Classifier': current_site_some_classifier}
df_sites = pd.DataFrame(data_dict)
#add bool column to exclude specific sites from the model
#purpose: to enable scenario modelling of a change to the configuration of sites (e.g. removing *and* adding, not just removing, or adding, or as-is)
list_bool_include = [True for x in range(len(current_site_names))]
df_sites['Include?'] = list_bool_include
#With the below I wanted to overwrite the original variable df_sites with the dynamic df but this doesnt seem to replace it as expected, and instead, renders a version with the data_editor (check box) feature in - this is what prompted my question in this thread
df_sites = st.data_editor(df_sites, use_container_width=True, num_rows="dynamic") #dynamic = allows new rows to be added in-browser
#list to subsequently filter the df on - when replaced with the uploaded df, this could be redirected to the df.columns, which will dynamically show the user the column labels in the selectboxes that are present in whatever df they upload, meaning filtering is possible for any df being used.
col1, col2, col3, col4 = st.columns(4)
with col1:
site_name_col = st.selectbox(label='(Core) select site name/identifier field:', options=list(df_sites.columns))
with col2:
postcode_col = st.selectbox(label=f'(Core) select site postcode field:', options=list(set(df_sites.columns)))
with col3:
filter_column = selectbox(label='(Optional) select filter column', options=list(df_sites.columns), )
with col4:
#try block which handles KeyError that otherwise occurs until the user selects a col to filter on
try:
data_choices = st.multiselect(label=f'(Optional) select filter criteria for: {filter_column}', options=list(set(df_sites[filter_column])))
except KeyError:
pass
#try block which handles NameError that otherwise occurs at first load of the app
try:
st.session_state['data_choices'] = data_choices
if len(st.session_state['data_choices']) > 0:
df_sites = df_sites[df_sites[filter_column].isin(data_choices)]
else:
pass
except NameError:
pass
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.