Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting Great Tables #220

Open
machow opened this issue Feb 2, 2024 · 2 comments
Open

Supporting Great Tables #220

machow opened this issue Feb 2, 2024 · 2 comments

Comments

@machow
Copy link

machow commented Feb 2, 2024

Hello, I'm really loving the support in itables for both pandas and polars!

While looking through the code for itables, I noticed it's able to use the .to_html() method of DataFrames, so should also be able to display a Great Tables GT object.

However, I wasn't quite able to get it to work. I could produce the necessary HTML, but ran into a javascript error. Any chance you might be interested in adding Great Tables support? We'd love to be able to provide an interactive table with pagination! I'm happy to dig more, but wanted to check here in case you have a better sense of how Great Tables could be plugged in.

Here's the javascript error, and the code I used to produce it.

Error:

 "Cannot read properties of undefined (reading 'mData')".

Python code

from itables.javascript import set_default_options, to_html_datatable_using_to_html
from great_tables import exibble, GT
from IPython.display import HTML, Javascript, display


# Modify GT class to work with what itables expects ---------
# this section adds .to_html() and .__len__ methods to GT

def to_html(self: GT, table_id: str, classes:str = "") -> str:
    rendered = self.render("html")

    # rough hack to insert id and classes itables expects
    return (
        rendered
            .replace("<table ", f'<table id="{table_id}" ')
            .replace('class="gt_table" ', f'class="gt_table {classes}" ')
    )

GT.to_html = to_html
GT.__len__ = lambda self: len(self._tbl_data)


# define a GT table ----------
#

gt = GT(exibble).tab_spanner("SPANNER", ["date", "time"])
gt


# attempt to display using itables -----------
#

opts = {"css": "", "id": "T_abcd", "pre_dt_code": ""}
set_default_options(opts, use_to_html=True)

display(HTML(to_html_datatable_using_to_html(gt, tableId="T_abcd")))
@mwouts
Copy link
Owner

mwouts commented Feb 3, 2024

Hey @machow , thanks for reaching out! Yes certainly I would love to add support for Great Tables. Recently we added support for Pandas Style objects so I imagine we should be able to do GT as well.

Thanks for the example above, I will have a look when time permits.

@mwouts
Copy link
Owner

mwouts commented Feb 10, 2024

Hey @machow , I've had a look at your example.

I am wondering if the problem might be that there is no <thead> in your table? That seems to be a requirement for using datatables-net, cf. https://stackoverflow.com/questions/25377637/datatables-cannot-read-property-mdata-of-undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants