Add sorting to this st.markdown

Is it possible to add sorting to this dataframe generated using below code please:

import pandas as pd

# Sample DataFrame
df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [24, 30, 22],
    'Score': [88, 92, 85]
})

# Function to style DataFrame cells with HTML based on the 'Score' value
def apply_callout_html(val):
    if val > 90:
        return f'<div style="background-color:#e8f9ee;color:#177233;padding:5px;border-radius:5px;">{val}</div>'
    elif val < 85:
        return f'<div style="background-color:#ffecec;color:#7d353b;padding:5px;border-radius:5px;">{val}</div>'
    else:
        return f'<div>{val}</div>'

# Apply callouts to the 'Score' column
df['Score'] = df['Score'].apply(apply_callout_html)

# Convert DataFrame to HTML with escape=False to allow HTML elements
df_html = df.to_html(escape=False)

# Display the styled DataFrame in Streamlit
st.markdown(df_html, unsafe_allow_html=True)```

Hello,
I don’t understand what are you trying to achieve here. What do you want to sort , the value, the color ?

sort by the value. i know u may ask me to use st.dataframe but i cannot use callouts inside it so looking into this.

You can sort with

        # Sort the DataFrame by 'Score' in ascending order (you can use ascending=False for descending order)
        df = df.sort_values(by='Score', ascending=True)

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