Update cell in dataframe and color background of cell

How do I update a cell in a table coming from a cell in a dataframe and and update color of cell red for 2 seconds?

I have tried doing:

t = st.empty()
t.dataframe(data)
while True:
data.iat[0,2] += 1
t.dataframe(data)
time.sleep(5)

which displays the update - but there is no way of knowing that its the cell [0,2] that is updated.

Hi @endrem,

Welcome to Streamlit. Have you tried using pandas styler to change the background color of cell [0,2]? Specifically, you can use set_td_classes as described in Table Visualization — pandas 1.3.3 documentation

Dinesh

1 Like

Im running one of the demo apps - i cant find the link to it but it is “Gross Agricultural Product ($B)” for China and USA. I try to add some styling to the dataframe but nothing is displayed on the webpage:

import streamlit as st
import pandas as pd
import altair as alt
import time

from urllib.error import URLError

@st.cache
def get_UN_data():
AWS_BUCKET_URL = “http://streamlit-demo-data.s3-us-west-2.amazonaws.com
df = pd.read_csv(AWS_BUCKET_URL + “/agri.csv.gz”)
return df.set_index(“Region”)

try:
df = get_UN_data()
countries = st.multiselect(
“Choose countries”, list(df.index), [“China”, “United States of America”]
)
if not countries:
st.error(“Please select at least one country.”)
else:
data = df.loc[countries]
data /= 1000000.0
data = data.sort_index()

    #data.style.set_properties(**{'background-color': 'black',
    #                   'color': 'green'})
    data.style.set_table_styles(
        [{'selector': 'tr:hover',
        'props': [('background-color', 'green')]}] )

    t = st.empty()
    t.dataframe(data)
    #st.write(data.sort_index())

    data = data.T.reset_index()
    data = pd.melt(data, id_vars=["index"]).rename(
        columns={"index": "year", "value": "Gross Agricultural Product ($B)"}
    )
    chart = (
        alt.Chart(data)
        .mark_area(opacity=0.3)
        .encode(
            x="year:T",
            y=alt.Y("Gross Agricultural Product ($B):Q", stack=None),
            color="Region:N",
        )
    )
    st.altair_chart(chart, use_container_width=True)

    data.style.set_properties(**{'background-color': 'black',
                       'color': 'green'})
    t.dataframe(data)

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