Change background color of a specific character in multiselect widget

I am working on a Data Profiling project.
I want to change the background color of empty white spaces in options of the multiselect widget.

When the user chooses a certain option from a radio widget, the code gets the unique values of a pandas column and shows them in a multiselect widget.
I want the change the background color of the spaces because sometimes the same values have leading/trailing white spaces, and I want the user to be able to see them in order to choose the values without the trailing/leading whitespaces.

Example of what I need:

Is that, or any other approach to handle leading/trailing whitespaces, achievable in any way?

Is there a reason why you can’t trim the offerings and/or selections within Python and eliminate the issue?

yes actually, I want to show the quality of the data in columns.
So in case of categorical data, a list of the values existing in the column must be shown, in order to choose the correct values that we want to compare the column’s values against.

In the example above, we would select “Value A” and “Value B” that are without the trailing/leading spaces (as the only values that should exist in this column). Then the algorithm would check this rule, and get back with the rate of correct values against wrong ones.
Th wrong ones are the values with spaces, or are different values than the chosen.

If the solution as in the above photo is not possible, then maybe changing the color of the values with the spaces to red? Is that possible?

I hope I’m making sense.

Thank you!

I think I’m understanding, but I might be conceptualizing something different. From your explanation, I would construct something like:

  • Get all unique values in a column
  • Display the list in html where it’s much easier to format the display. This would give a clear picture of what exists.
  • Provide a selection of trimmed values for the user to select
    • Can also check if trimming some element results in a new element not originally seen

Is there a reason you want to provide them the option of selecting a “wrong” choice?

(Off the top of my head, I don’t know how to format individual options in the a selection widget like that. It might be possible, but probably a little messy. I will play around.)

One option, though it’s not a general-purpose solution to formatting:

def formatter (x):
        return str(x).replace(' ','⬛')
        return x

with st.form('my_form'):
    st.selectbox('Choose',[' This  is an option','This is an option','This  is an option  ', 'This is an option   '], index=0, format_func=formatter)

Logic could be changed if you only want to highlight leading, trailing and double spaces.

1 Like

Thank you so much for this, it worked and this is exactly what I wanted.
Sorry for the late reply, it was midnight here when you got back to me.