itables
itables copied to clipboard
Exporting table to pdf with columnDefs styling applied
-
Are buttons supported to be able to export the table to a pdf? I see an open issue but no instructions on how to enable via the docs. #121
-
Would it be possible to include the custom styling applied to the table when exporting to a pdf?
Hey @sandsmichael , sorry the buttons are not yet available. They require a very significant change in how we implement itables and at the moment I don't see when I might be able to work on this.
Can you tell us a bit more about the context in which you would like to use the pdf export? Thanks
Thanks for the quick response. For starters, the package is amazing so thanks for all of your hard work.
The use case is really for greater accessibility/sharing.
itables provides excellent functionality for presenting one's work with in Jupyter. But this assumes only other developers will be interested in the resulting output.
the interactive tables and customized styling makes for a better user experience when exploring data within the notebook, but it precludes the ability to be able to leverage that effort outside of the notebook.
If a user want's to utilize python to conduct their analysis, for whatever reason, knowing that time spent on building out itables will always be self-contained within the notebook itself might discourage a user from working with the tool in the first place....and that's a shame.
The same user may pivot to just writing and styling excel files directly to get to a pdf's, while either still maintaining code for the itables or forgoes using itables altogether. Either way, it's extra work for the same result and the developer is forced to take a suboptimal route.
itables provides a better experience for users exploring data with in Jupyter, but in my opinion, would be missing out not to provide the same users the ability to leverage/share their work with a non-technical audience. I would point to functionality such as "ReportBuilder" within PowerBI as one example of this use case.
That being said, I really enjoy using this tool and appreciate your consideration for this enhancement.
Hey @sandsmichael , thank you for providing more information about how you use/ would like to use the package.
Well personally I do care a lot about making sure that the notebook can be turned into a report.
However itables is based on the JavaScript library datatables and hence it makes it far more natural to support HTML output documents rather than PDF documents.
You can currently convert your notebooks to HTML documents using either
jupyter nbconvert --to html- and Jupyter Book (this is what we use to build the docs at https://mwouts.github.io/itables/ )
Currently the conversion to PDF using jupyter nbconvert --to pdf is not very interesting (it just prints the text representation of the dataframe).
Even with the default Pandas representation it does not seem that obvious to convert a dataframe to a PDF - see this SO question. Maybe we could try to combine that with the to_html_datatable function and see what this gives.
I have two additional questions for you:
- are you thinking of export only one table, or rather one notebook, as a PDF document?
- you would you like to deal with the tables that have many rows/many columns? I don't think we can have interactivity in PDF documents like we have in HTML documents thanks to
datatables, so would you like to display all the columns/all the rows and possibly overflow on multiple pages?
Fair point, supporting html output is still helpful, as the file, or even a screenshot of it could ultimately be saved as a pdf.
Enabling toolbar buttons to be able to both export the raw data to an excel file, as well as export the html styled tabled to an html file would both be big strides in the right direction.
The issue with converting the entire notebook is that I may be only interested in a specific section at a time for a given report.
Response to questions:
-
Ideally, there would be a toolbar button (similar to the pagination or column search features) that enables me to click and export either the underlying data within the table to an excel file, or the entire html styled dataframe to an html file (or pdf). The important part for the html/pdf export would be that the pagination and html styling appears neatly in the final result.
-
Having the ability to choose to export either a) only what is visible on the page or b) all records in the dataset would be helpful. As a bare minimum, I would suggest focusing on the ability to export all data and all columns contained within the table. A user can always just reshape the data from python prior to sending it to the itable so that the resulting export is as desired.
Hey @sandsmichael , sorry for the long delay in answering.
So, if you are willing to export a table to an HTML document, you can use to_html_datatable, there is a short example of how to do that here. Then you need to save the HTML to a file. If you want to export the full data you probably want maxBytes=0. So in the end you could use this function:
from itables import to_html_datatable
def export_to_html(df, html_filename, **kwargs):
with open(html_filename, 'w') as fp:
fp.write(to_html_datatable(df, maxBytes=0, **kwargs))
For an example:
from itables.sample_dfs import get_countries
df = get_countries()
export_to_html(df, 'countries.html')
Hey @sandsmichael , the DataTables extensions are now supported in ITables v2!
The PDF button is not included by default, because the required PDF library makes the bundle much larger, however I have documented at https://mwouts.github.io/itables/custom_extensions.html#creating-a-custom-datatables-bundle how to create a bundle with the PDF export. Do you want to give it a try?