I am creating a dashboard for some metrics using the python panel library in Jupyter Labs. I am using python 3.8.3, pandas 1.0.5 and panel 0.9.7.
I am displaying a panel depending on a value chosen in a panel widget. I would like to change the font color of the numbers in the panel depending on its value, that is, any number below a threshold should be green and any number above should be red.
The below code is an example of the code I am using. In this example I would like to change the font color to red in the displaying panel for any number above 5 and change to green any number below 5.
Any suggestions will be highly appreciated
#imports
import pandas as pd
import panel as pn
pn.extension()
# initialize list of lists
data = [['A','January',10],['A','February',7],['A','March',5],['B','January',4],['B','February',8],['B','March',12] ]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['Type', 'Month','Metric'])
#lists creation
type_list=['A','B']
#widget creation
dd_types = pn.widgets.Select(name='Select the type to display report:', options=type_list)
#filter df function
def display_panel(value):
table = df[df.Type==value].pivot_table(values='Metric',index='Type',columns='Month')
return pn.panel(table)
# create relation between widgets
@pn.depends(dd_types)
def get_parameters(value):
return display_panel(value)
# arrange and correlate widgets and functions
report_area = pn.Row(dd_types, get_parameters)
report_area.show()
Thanks a lot for your response. I was able to set the df with the format I wanted and it worked perfectly when I execute the code directly. But when I put it into a function and return the styled dataframe I keep the color but lose some other formatting (spacing, alignment, the shadows every row, the line below the the column names)