根据单元格数据绘制 table 单元格颜色

Plotly table cell color depending on cell data

大家好,我能得到任何可能的帮助吗?我正在尝试根据对我的功能的回答对单元格颜色进行编码。

b = df['Supertrend']

a = b[-1]

def main(a):
   if a !=True:
   return 'Bearish'
   else:
   return 'Bullish'

poes = main(a)

j = d1['Supertrend']

k = j[-1]

def main(k):
   if k!= True:
   return 'Bearish'
   else:
   return 'Bullish'

doos = main(k)

l = d15['Supertrend']

m = l[-1]

def main(m):
  if m!= True:
  return 'Bearish'
  else:
  return 'Bullish'

naai = main(m)

n = d1h['Supertrend']

o = n[-1]

 def main(o):
  if o!= True:
  return 'Bearish'
  else:
  return 'Bullish'

tiet = main(o)

q = d1d['Supertrend']

p = q[-1]

 def main(p):
  if p!= True:
  return 'Bearish'
 else:
  return 'Bullish'

kak = main(p)

TF = ['1 Minute', '5 Minutes', '15 Minutes', '1 Hour', 'Daily']
BB = [doos, poes, naai, tiet, kak]

fig = go.Figure(data=[go.Table(
header=dict(values=[['<b>Timeframe</b>'], ['<b>Bullish/Bearish</b>']],
            line_color='black',
            fill_color='black',
            align='center',
            height= 40,
            font=dict(color='white', size=15)),

所以我想这是我需要帮助的地方。规定如果函数输出看跌,则单元格必须为红色,如果输出看涨,则应为绿色

cells=dict(values=[TF,BB],
           line_color='darkslategray',
           fill = dict(color=['black', 

我尝试了下面的第一个,但没有成功

          ['#ff2d5d' if BB == str('Bearish') else '#04b29b']]),
           font=dict(color='white', size=12),
           height = 30,
           align='center'))
           ])

          fig.update_layout(width=500, height=450)
          fig.show()

由于无法在列表中写入if语句,所以可以预先创建一个颜色列表并将其指定给color-code个单元格。

TF = ['1 Minute', '5 Minutes', '15 Minutes', '1 Hour', 'Daily']
BB = [doos, poes, naai, tiet, kak]

colors = ['#ff2d5d' if c == 'Bearish' else '#04b29b' for c in BB]
print(colors)
fig = go.Figure(data=[go.Table(
    header=dict(values=[['<b>Timeframe</b>'], ['<b>Bullish/Bearish</b>']],
                line_color='black',
                fill_color='black',
                align='center',
                height= 40,
                font=dict(color='white', size=15)),

   cells=dict(values=[TF,BB],
           line_color='darkslategray',
           fill = dict(color=['black',colors]),
           font=dict(color='white', size=12),
           height = 30,
           align='center'))
           ])

fig.update_layout(width=500, height=450)
fig.show()