mwouts / itables

Pandas DataFrames as Interactive DataTables
https://mwouts.github.io/itables/
MIT License
767 stars 56 forks source link

Full table is briefly displayed when using a pandas.style table #236

Closed tfmark closed 6 months ago

tfmark commented 6 months ago

Firstly, thank you very much for the work you've done on this project!

I've been playing around with itables on an 2,000 row table (dataset is much larger but am truncating to 2k rows so the user experience is still snappy enough), and now I'm at a stage where I'd like to apply formatting to cells (e.g. format some floats to 2 decimals, others to e.g. 5 decimals), so wanted to use pandas.style - however when the table is loaded, all of the rows are briefly displayed (for a couple of seconds) before the table is displayed correctly.

Example using pandas table:

pandas_table

Example using a styled table (i.e. pandas.style):

pandas_style

Is this a known issue with passing a styled table to itables? Is there a workaround?

mwouts commented 6 months ago

Hey @tfmark , what happens here is that for Pandas style object we have no other choice than to use their .to_html() method. This creates an HTML table that indeed might get displayed before it is loaded by datatables-net.

It is more efficient to directly display the Pandas dataframe, in which case the data is passed directly to datatables-net (throught Javascript rather than through HTML).

You can still use either Pandas formatting or Javascript formatting. The Javascript formatting works slightly better (i.e. if you require three decimals then 1 will be shown as 1.000, which with the Pandas formatting you would get 1.0 as the formatting happens before the data is passed on to datatables-net).