mwouts / itables

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

fix error "initializeDataTables is not a function" when a notebook is reloaded #160

Closed mwouts closed 1 year ago

mwouts commented 1 year ago

Sometimes when a notebook is reloaded from disk, the tables don't load, and in the console I find a message window.initializeDataTables is not a function.

This PR intends to postpone the table initialization when this occur, to let some time for the init script to load (that script is located in another section of the HTML document).

@fwouts , do you see a better approach? Ideally I would declare a dependency of (the rendered version of) datatables_template.html on (the rendered version of) initialize_offline_datatable.html but I have not idea how to do that...

fwouts commented 1 year ago

I suspect the problem is that <script type="module"> code isn't evaluated immediately like <script>, it's automatically deferred (like <script type="module" defer>). It appears that cannot be disabled.

You can see an example with the following file:

<!DOCTYPE html>
<html>
  <body>
    <script>
      console.log("Hello from regular script 1");
    </script>
    <script type="module">
      console.log("Hello from module");
    </script>
    <script>
      console.log("Hello from regular script 2");
    </script>
  </body>
</html>

The output here (in Chrome at least) is:

Hello from regular script 1
Hello from regular script 2
Hello from module

However as far as I understand, a deferred script is still guaranteed to run before $(document).ready() (which I believe is equivalent to the browser's native DOMContentLoaded event), so I'm not sure why it's possible to get this bug in the first place!

I've attempted to fix this in https://github.com/mwouts/itables/compare/catch_retry...fwouts:itables:catch_retry by using a promise. I haven't tested this code though, as I'm not sure how to do that :) There could be a bug or two in there.

Is there a contributing guide somewhere?

mwouts commented 1 year ago

Yes - I am not sure how this happens but it does really happen (Jupyter Lab in Chrome under Windows). I have given a quick try at the promise approach and it seems to work really well! Thanks Francois.

Is there a contributing guide somewhere?

You mean a guide on how to setup a dev environment and do some tests? Not at the moment but that is certainly a great idea. I'll take a note of the tests I will be doing, this way we can draft an initial guide...

codecov-commenter commented 1 year ago

Codecov Report

Merging #160 (05111c2) into main (c6e7388) will increase coverage by 0.04%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #160      +/-   ##
==========================================
+ Coverage   96.44%   96.49%   +0.04%     
==========================================
  Files          23       23              
  Lines         732      742      +10     
==========================================
+ Hits          706      716      +10     
  Misses         26       26              
Impacted Files Coverage Δ
itables/sample_dfs.py 100.00% <100.00%> (ø)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

fwouts commented 1 year ago

Perfect, the contributing guide is typically named CONTRIBUTING.md. Looking forward to it!

mwouts commented 1 year ago

Perfect, the contributing guide is typically named CONTRIBUTING.md. Looking forward to it!

Thanks! I have a draft at docs/contributing.md - this way it gets included in the documentation as well. Let me know what you think.

fwouts commented 1 year ago

It looks good! I hadn't seen docs/developing.md, that was basically what I needed :)