I am trying to use conditional formatting to set colors of a datatable cell. I have tried going thru forum questions and the official documentation but could not get to my specific requirement. I am new to the Dash World and was looking for some suggestions from the experts here.
I have picked the sample program from Dash Documentation to work upon but could not understand how.
I need to updated the last cell of column ‘Humidity’ with color “Green” or “Red” depending on the value of the first cell of the same column. If the value of second cell has increased (meaning greater than) it gets the color green and if the value of second cell has decreased ( meaning lesser than) it gets the color red.
Note: The actual dataframe has thousands of rows but I just need to look at the last 2 rows as new data is added every 5 minutes.
Any Input/Suggestion will be of great help.
from dash import Dash, dash_table
import pandas as pd
from collections import OrderedDict
data = OrderedDict(
[
("Date", ["2015-01-01", "2015-01-02"]),
("Region", ["Montreal", "Montreal"]),
("Temperature", [1, -20]),
("Humidity", [14, 12]),
("Pressure", [2, 10924]),
]
)
df = pd.DataFrame(data)
app = Dash(__name__)
app.layout = dash_table.DataTable(
data=df.to_dict('records'),
sort_action='native',
columns=[{'name': i, 'id': i} for i in df.columns],
style_data_conditional=[
{
'if': {
'filter_query': '{{{}}} >= {}'.format(col, value),
'column_id': col
},
'backgroundColor': '#B10DC9',
'color': 'white'
} for (col, value) in df.quantile(0.9).iteritems()
]
)
if __name__ == '__main__':
app.run_server(debug=True)