Mapping LineraCmap to TableColumn

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)

This feature was only added in version 3.4 you will need to update your installed version of Bokeh to 3.4 or newer.

Also, your code has an error, the field value should be the name of the column, not the actual column of data:

field='percentgain'

@Bryan

bokeh v3.4.1 got installed in python-scripts dir as ‘bokeh.exe’ ,however ‘bokeh’ command is not recognized when run from the terminal(Windows10-Pycharm). .

Also tried changed the filename -‘bokeh.py’ but the issue persists.

Under ENVIRONMENT VARIABLES the python-scripts + site-packages directory path is already provided.

Bokeh files do run on using show(plot) in pythonscript but when the same is run using bokeh serve ,the terminal does not recognize bokeh .

   Directory: C:\Users\User_0\AppData\Roaming\Python\Python312\Scripts

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----    Mon 19-02-2024     10:15                __pycache__
-a----    Sun 05-05-2024     07:40         108390 bokeh.exe

PS C:\Users\User_0\AppData\Roaming\Python\Python312\Scripts> bokeh serve --show filter_1.py
bokeh : The term 'bokeh' is not recognized as the name of a cmdlet, function, script file, or operable program.

Your code (with the fix described above) works fine with Bokeh 3.4.1:

It sounds like you have some sort of path or environment issue. Unfortunately I am not a regular Windows user so I can’t really advise.

1 Like

This issue is preventing the use of bokeh server .

Can I post it as a new issue here or Bokeh-github?

As I said you almost certainly have some local path or environment configuration problem with your individual system. That’s a support question, so that would not be appropriate for GitHub at all.

Hopefully someone with more Windows expertise than me can chime in here.

Alternatively, “I’m on windows and installed package X but am not seeing it” is a fairly regular Python question IME. You might find more windows Python users who can help on a more general forum such as https://discuss.python.org/

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.