1

I'm trying to apply one or more styles to a pandas Dataframe, but I can't get to display the applied style in a pandastable Table. Here's an example:

from tkinter import *
from pandastable import Table, TableModel
class TestApp(Frame):
    def __init__(self, parent=None):
        self.parent = parent
        Frame.__init__(self)
        self.main = self.master
        self.main.geometry('600x400+200+100')
        self.main.title('Table app')
        f = Frame(self.main)
        f.pack(fill=BOTH,expand=1)
        df = TableModel.getSampleData()
        df.style.highlight_max(color='lightgreen') #HERE'S THE STYLE I WANT TO APPLY
        pt = Table(f, dataframe=df, showtoolbar=True, showstatusbar=True)
        pt.show()
        return

app = TestApp()
app.mainloop()

This will display the usual Table with no styles applied.

Does pandastable process the html/css style created by the pandas Styler or is there any way to apply styles to a pandastable Table object?

Any suggestions would be appreciated.

Subbu VidyaSekar
  • 2,503
  • 3
  • 21
  • 39
enri_c_m
  • 11
  • 3
  • Please Check this https://blog.furas.pl/python-tkinter-pandastable-examples-gb.html for Color Columns part – Karthik Sep 21 '20 at 05:29
  • Thanks. That goes as far as table coloring, but what if I want to change just the text color in a specific row based on a mask instead? – enri_c_m Sep 22 '20 at 01:16

1 Answers1

0

You could use function setColorByMask():

from tkinter import *
from pandastable import Table, TableModel

class TestApp(Frame):
    def __init__(self, parent=None):
        self.parent = parent
        Frame.__init__(self)
        self.main = self.master
        self.main.geometry('600x400+200+100')
        self.main.title('Table app')
        f = Frame(self.main)
        f.pack(fill=BOTH,expand=1)
        df = TableModel.getSampleData()
        pt = Table(f, dataframe=df, showtoolbar=True, showstatusbar=True)
        for col_idx in range(len(df.columns)):
            col_name = pt.model.df.iloc[:, col_idx].name
            if pt.model.df[col_name].dtypes != 'object':
                pt.setColorByMask(
                    col_name, 
                    pt.model.df.iloc[:, col_idx] == pt.model.df.iloc[:, col_idx].max(), 
                    'lightgreen'
                )
        pt.show()
        return

app = TestApp()
app.mainloop()

Some details are also provided in this answer.

Alexandra Dudkina
  • 4,302
  • 3
  • 15
  • 27
  • I've got the setColorByMask figured out, except for pt.model.df, that will not be executed even if I call pt.updateModel(TableModel(df)). So my final string is: self.pt.setColorByMask(col='Fee', clr='#FFD700', mask=(self.df['Fee']==self.df['Fee'].max())). Just to clarify, pandastable can stretch as far as table coloring, but it won't process a pd.DataFrame Styler if I apply any style as df.style.applymap(some style) for example? – enri_c_m Sep 23 '20 at 15:21
  • As far as I know pandastable is not so advanced yet. – Alexandra Dudkina Sep 23 '20 at 18:46