mwouts / itables

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

Can you save \ access the filtered tables? #155

Closed Alexfinkelshtein closed 3 months ago

Alexfinkelshtein commented 1 year ago

Hi, I was trying to check in the docs, but I couldn't find a clear answer. Can you save\ access \ interact with subtable generated by some filtering applied on the itable?

thanks!

mwouts commented 1 year ago

Hello @Alexfinkelshtein , unfortunately no, this is a display only plugin (not a widget). The data is exported to Javascript / HTML but there is no way back to Python.

MartinK84 commented 1 year ago

I just want to say that for me having a save/export functionality of a filtered table would be an essential feature. It must not even go back to python, just saving the filtered results as .csv or .xlsx would help a lot. This might not be required for the typical use case when the table is embedded into notebooks, but when using itables to convert large DataFrames to interactive HTML.

My current use case for which I would love to have this feature is the following:

mwouts commented 1 year ago

Oh, I see. Thanks for the clarification!

Well at some point we should be able to provide the excel / csv / html / pdf export indeed, but that requires being able to integrate the button extensions of datatables.net, see https://github.com/mwouts/itables/issues/50

I am not sure whether the export function of datatables takes into account the filter / ordering. Do you want to have a look at their documentation?

Also, since you mention using itables to generate an HTML page, let me point out that maybe providing the export buttons in that context could be easier than providing them in a Jupyter notebook. You could start with one example from the datatables.net documentation, and then we could see how to modify it to include your actual table ?

MartinK84 commented 1 year ago

You are correct, since I'm going to HTML directly it should be possible to just inject the correct code into the html string before writing it to a file. Basically updating the dt_args and adding the buttons parameter as described here: https://datatables.net/extensions/buttons/examples/initialisation/export.html

So I tried changing it to let dt_args = {"dom": "lrtip", "order": [], buttons: ['excel'], "initComplete": function () {, however, since I'm not familiar with java script I can't get the imports to work. All example I can find are using normal <script src=...></script> imports. In the itables generated code you are using <script type="module"> (which I have never seen before). I tried to just import "..."; everything that was given in the link above, but that does not appear to work. But I'm probably doing something stupid here because I have no idea how JavaScript works, sorry :(

mwouts commented 1 year ago

Yes, that's right, we are using the "ESM imports" because that is the only solution that happened to work in all the different contexts... The proper way to use the buttons with module imports is probably through an import map. We started working on that at #121 but at that time the technology was so recent that it was not even working in the latest Firefox on Ubuntu (now it is).

For your use case you could perfectly use standard scripts. The file that you want to edit is this one: datatables_template_connected.html. And you want to edit in a way that this file shows the buttons when you open it.

Once you have a minimal template that works, we'll see how to integrate it with itables. Does that work for you?

mwouts commented 3 months ago

Quick update on this: now the table data can be exported using the CSV and Excel buttons: https://mwouts.github.io/itables/extensions.html#buttons