Apply dataframe style base on multiselect columns

Context: I’m trying to apply string and background gradient formatting to a dataframe as such:

    st.dataframe((dataframe.style
        .format(
            {
                'A': '{:.0%}', 
                'B': '{:.0%}',
                'C': '{:.0%}',
            }
        )
        .background_gradient(
            cmap=cmap_red_green,
            axis=None
        )
    ))

The multi-select only shows “A”,“B” and “C” as options:

choices = st.multiselect('Choose the date filters to apply',['A','B','C'])

However, my dataframe is actually multi-index and so I can grab the columns based on the choices from the multiselect doing this:

chosen_cols = [col for col in list(dataframe.columns) for choice in choices if choice in col]

Now, how can I apply the .format dynamically based on the options a user selected? Using apply? How could I do that?

I was trying to do this:

st.dataframe(dataframe[chosen_cols]) and this works if the dataframe doesn’t have style applied to it (based on the options seleted by the user), but if I apply the style it says “Styler” object is not subscriptable (since it’s no longer a dataframe so I can’t subset the columns based on the options chosen)

How could I overcome this issue?

Thanks!

For an overview, the dataframe looks more or like this:

image