mwouts / itables

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

Is it possible to load datatables.net without using require? #40

Closed mwouts closed 2 years ago

mwouts commented 2 years ago

The require library is not available in JupyterLab, thus I'd like to remove this dependency (see also #3 and #39).

@fwouts suggested that we could load jQuery and DataTables.net using the new import keyword in Javascript and contributed the example below:

from IPython.display import HTML

HTML("""
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css">
<script type="module">
    import $ from "https://esm.sh/jquery@3.5.0";
    import initDataTables from "https://esm.sh/datatables.net@1.11.3?deps=jquery@3.5.0";

    initDataTables();

    $(document).ready(function () {
        $('#example').DataTable();
    });
</script>
<table id="example">
    <thead>
        <tr>
            <th>A</th>
            <th>B</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>3</td>
            <td>1</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
        </tr>
        </tfoot>
</table>""")

This seems to work pretty well: image

I'll see how to apply this methodology to replace require in the itables package.

mwouts commented 2 years ago

A first implementation of the above is available at https://github.com/mwouts/itables/pull/41/commits/d83b6abe9b4aeafc10b3e440ae351bd2dd15d135, however it only works in Jupyter Lab (and not always, for instance when the notebook is reloaded the tables are not shown).

The problems seems to be that the initDataTables function is not working properly when requirejs is loaded, for instance in the context of a notebook exported to html with nbconvert.

image image

I'll see if we can get help on this on the datatables.net forum

mwouts commented 2 years ago

As a workaround for the problem reported above, we will use import only when require.js is not available, cf #43