Streamlit append color changes

I’m new to streamlit and running locally. I need to change the output text color of a row based on a record Yes or No. How can I do that. Thank you.

`

import os
import pandas as pd
import streamlit as st

Function to list all saved persons in a table

def list_saved_persons(SAVE_DIR, ENTRY_LOGS_DIR, filter_type=None):
# Initialize an empty list to store person details
person_details =

# Iterate over each file in the save directory
for file_name in os.listdir(SAVE_DIR):
    try:
        # Extract name, type, date, and blacklisted status from the file name
        parts = os.path.splitext(file_name)[0].split('_')
        name = parts[0].capitalize()
        file_type = parts[1]
        date = parts[3]
        blacklisted = parts[2]
        uid = parts[4].upper()
    except ValueError:
        # Handle cases where the file name structure is different
        name = os.path.splitext(file_name)[0]
        file_type = "Unknown"
        date = "Unknown"
        blacklisted = "No"

    if blacklisted == "notblacklisted":
        blacklisted_stat = " No"
    else:
        blacklisted_stat = " Yes"

    # Check if filter_type is None or matches the file_type
    if filter_type is None or filter_type == file_type:
        # Append person details to the list
        person_details.append({'ID': uid,
                               'Name': name,
                               'Type': file_type,
                               'Date Added': date,
                               'Blacklisted': blacklisted_stat})

 # Convert the list of dictionaries to a DataFrame
df = pd.DataFrame(person_details)

return df

`

1 Like

Hi @Chamod_Abeyrathne, welcome to Streamlit!

One suggestion for future posts – please use triple backticks (```) to format your code as a code block, so that it’s all formatted properly.

Essentially, the easiest way to change the color of a row is by creating a styled dataframe.

Here’s an example that’s simpler than your example:

import streamlit as st
import numpy as np
import pandas as pd

df = pd.DataFrame(
    {"blacklisted": np.random.choice([True, False], 10), "x": np.random.randn(10)}
)

styled_df = df.style.apply(
    lambda row: [
        "background-color: red" if row["blacklisted"] == True else "" for _ in row
    ],
    axis=1,
)
st.write(styled_df)

That outputs something like this

2 Likes

Thank you!

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