I have the following script that turns a pandas dataframe into an interactive html table with Plotly:
goals_output_df = pd.read_csv(os.path.join(question_dir, "data/output.csv"))
fig = go.Figure(data=[go.Table(
columnwidth = [15,170,15,35,35],
header=dict(values=['<b>' + x + '</b>' for x in list(goals_output_df.columns)],
# fill_color='#b9e2ff',
line_color='darkslategray',
align='center',
font=dict(color='black', family="Lato", size=20),
height=30
),
cells=dict(values=[goals_output_df[column] for column in goals_output_df.columns],
# fill_color='#e6f2fd',
line_color='darkslategray',
align='left',
font=dict(color='black', family="Lato", size=20),
height=30
))
])
fig.update_layout(
title="<b>Output summary for %s</b>"%question.strip('question'),
font=dict(
family="Lato",
size=18,
color="#000000"))
fig.write_html(os.path.join(question_dir, "results/output.html"))
The table contains a column named "Output" which can have one of three values for each row: "YES", "NO" and "BORDERLINE".
I want to be able to change the color of the text in the "Output" column such that "YES" is green, "NO" is red and "BORDERLINE" is blue.
Any idea how I could do that? Plotly documentation doesn't seem to help.
It's kind of implicit on this section cell-color-based-on-variable. Here what I'll do in your case.
import pandas as pd
import plotly.graph_objects as go
df = pd.DataFrame({"name":["a", "b", "c", "d"],
"value":[100,20,30,40],
"output":["YES", "NO", "BORDERLINE", "NO"]})
map_color = {"YES":"green", "NO":"red", "BORDERLINE":"blue"}
df["color"] = df["output"].map(map_color)
cols_to_show = ["name", "value", "output"]
Where cols_to_show
are the only columns you want to show in your table.
Here you want to have a standard cell background in all columns but the output
one
fill_color = []
n = len(df)
for col in cols_to_show:
if col!='output':
fill_color.append(['#e6f2fd']*n)
else:
fill_color.append(df["color"].to_list())
data=[go.Table(
# columnwidth = [15,20,30],
header=dict(values=[f"<b>{col}</b>" for col in cols_to_show],
# fill_color='#b9e2ff',
line_color='darkslategray',
align='center',
font=dict(color='black', family="Lato", size=20),
height=30
),
cells=dict(values=df[cols_to_show].values.T,
fill_color=fill_color,
line_color='darkslategray',
align='left',
font=dict(color='black', family="Lato", size=20),
height=30
))
]
fig = go.Figure(data=data)
fig.show()
UPDATE
In case you want to change text color
text_color = []
n = len(df)
for col in cols_to_show:
if col!='output':
text_color.append(["black"] * n)
else:
text_color.append(df["color"].to_list())
and
data=[go.Table(
# columnwidth = [15,20,30],
header=dict(values=[f"<b>{col}</b>" for col in cols_to_show],
# fill_color='#b9e2ff',
line_color='darkslategray',
align='center',
font=dict(color='black', family="Lato", size=20),
height=30
),
cells=dict(values=df[cols_to_show].values.T,
line_color='darkslategray',
align='left',
font=dict(color=text_color, family="Lato", size=20),
height=30
))
]
fig = go.Figure(data=data)
fig.show()
You could add an if statement in the font_color section:
import pandas as pd
import plotly
import plotly.graph_objs as go
df = pd.DataFrame({"name":["a", "b", "c", "d"],
"value":[100,20,30,40],
"output":["YES", "NO", "BORDERLINE", "NO"]})
fig = go.Figure(data=[go.Table(
columnwidth = [30,30,30],
header=dict(values=["Name","Value", "Output"],
# fill_color='#b9e2ff',
line_color='darkslategray',
align='center',
font=dict(color='black', family="Lato", size=20),
height=30
),
cells=dict(values=[df.name, df.value, df.output],
# fill_color='#e6f2fd',
line_color='darkslategray',
align='left',
font_color=['darkslategray','darkslategray',['green' if x == "YES" else
"red" if x == "NO" else "blue" if x == "BORDERLINE" else 'darkslategray'
for x in list(df.output)]],
height=30
))
])
fig.show()
This will produce the table:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With