Trying to map a conditional textcolor pallete returned by Linearcmap to a datatable column gives the following error:
text_color: expected either None or a value of type Color, got Field(field=‘percentgain’, transform=LinearColorMapper
What additional code is required to map datacolumn to linearcmap pallete?
from bokeh.plotting import figure, show, gridplot,row,column
from bokeh.models import ColumnDataSource,TableColumn,DataTable,LinearColorMapper,NumberFormatter
from bokeh.embed import file_html
from bokeh.layouts import layout
from bokeh.plotting import figure, output_file, save
import pandas as pd
from bokeh.transform import factor_cmap,linear_cmap
from bokeh.models import ColumnDataSource, DataTable, TableColumn, HTMLTemplateFormatter
from bokeh.io import show
from bokeh.palettes import RdYlGn9
# Sample data
data = {
'time': ['2024-05-03', '2024-05-04', '2024-05-05'],
'stockname': ['AAPL', 'GOOG', 'MSFT'],
'percentgain': [0.05, -1.2, 1.5] # Example percent gains (positive/negative)
}
data['time'] = pd.to_datetime(data['time'])
# Determine gain status and assign colors
data['gain_status'] = ['Positive' if gain > 0 else 'Negative' for gain in data['percentgain']]
source = ColumnDataSource(data)
# Create a TableColumn with HTMLTemplateFormatter
percentgain_col = TableColumn(field=data['percentgain'], title="PercentGain",
formatter= NumberFormatter(
text_color = linear_cmap(
field_name = 'percentgain',
low=min(data['percentgain']),
high=max(data['percentgain']),
palette=RdYlGn9[::-1])))
columns = [
TableColumn(field="time", title="Time", width=150),
TableColumn(field="stockname", title="Stock Name"),
percentgain_col
]
data_table = DataTable(source=source, columns=columns)
output_file(filename="tableCss_2.html", title="LinearCmap")
save(data_table)