How to highlight words in st.table or st.dataframe?

I have a dataframe with html code to bold certain words. How do I display the bold formatting in Streamlit? st.table and st.dataframe dont seem to do the job.

Code:

df = pd.DataFrame({'item_name': ['Chocolate is the best', 'We love Chocolate',
                                        'I would pay money for Chocolate', 'Biscuit',
                                        'Biscuit', 'Biscuit',
                                        'IceCream', 'Dont love IceCream',
                                        'IceCream'],
                          'value': [90, 50, 86, 87, 42, 48,
                                    68, 92, 102],
                          'weight': [4, 2, 3, 5, 6, 5, 3, 7,
                                     5]})
def highlight_selected_text(row):
    text = row["item_name"]
    bold =['Chocolate']
    for k in bold:
        text = text.replace(k, f'<span style="font-weight: bold">{k}</span>')
    return text
df["highlighted"] = df.apply(highlight_selected_text, axis=1)
df

This works st.markdown(df.to_html(escape=False),unsafe_allow_html=True)

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.