As discussed in #879, sometimes (particularly with big notebooks, slow connections, etc), nbextensions get loaded before the notebook has finished loading. In other cases, the notebook may have fully loaded before the nbextension gets loaded, so the nbextension will miss the notebook_loaded.Notebook event. Finally, the event is fired again as part of loading checkpoints, so it's not necessarily a one-time-only event! Nbextension code should be able to deal with any of these three scenarios.
The best way to approach this and still get any initialization which needs to be applied to the whole notebook performed correctly seems to be:
- make sure any whole-notebook initialization function can be called multiple times without causing problems like duplicating html elements, etc.
- bind the initialization function to the
notebook_loaded.Notebook event, so that it gets re-initialized correctly whenever the notebook is (re)loaded
- As part of the
load_ipython_extension function used to load the nbextension, check if Jupyter.notebook._fully_loaded === true, which indicates we missed the first notebook_loaded.Notebook event. If this is true, call the initialization function(s) mentioned in 1 directly
I've had a quick skim through our nbextensions looking for ones which may need their loading behaviour altered, and come up with the following lists:
The following need updates to handle loading correctly:
These I'm uncertain about:
These seem to run at least some code without load_ipython_extension
being called, which I don't think is supposed to happen, so perhaps need a bit
of reworking:
These haven't even been updated to notebook 4.x, so should either be updated or removed:
These already use the correct approach:
autoscroll
collapsible_headings
init_cell
nbTranslate
toc2
And these don't need notebook loaded to initialize:
addbefore
autosavetime
chrome-clipboard
code_font_size
code_prettify
comment-uncomment
contrib_nbextensions_help_item
css_selector
datestamper
dragdrop
equation-numbering
gist_it
help_panel
hide_header
hinterland
keyboard_shortcut_editor
limit_output
navigation-hotkeys
nbTranslate
nbviewer_theme
no_exec_dunder
notify
printview
qtconsole
rubberband
runtools
scratchpad
search-replace
select_keymap
skill
snippets_menu
snippets
spellchecker
toc2
toggle_all_line_numbers
tree-filter
zenmode
As discussed in #879, sometimes (particularly with big notebooks, slow connections, etc), nbextensions get loaded before the notebook has finished loading. In other cases, the notebook may have fully loaded before the nbextension gets loaded, so the nbextension will miss the
notebook_loaded.Notebookevent. Finally, the event is fired again as part of loading checkpoints, so it's not necessarily a one-time-only event! Nbextension code should be able to deal with any of these three scenarios.The best way to approach this and still get any initialization which needs to be applied to the whole notebook performed correctly seems to be:
notebook_loaded.Notebookevent, so that it gets re-initialized correctly whenever the notebook is (re)loadedload_ipython_extensionfunction used to load the nbextension, check ifJupyter.notebook._fully_loaded === true, which indicates we missed the firstnotebook_loaded.Notebookevent. If this is true, call the initialization function(s) mentioned in 1 directlyI've had a quick skim through our nbextensions looking for ones which may need their loading behaviour altered, and come up with the following lists:
The following need updates to handle loading correctly:
execute_time- handled in make execute_time robust to loading before notebook finishes loading #883freeze- handled in Freeze Update #884hide_inputFix/timings #886hide_input_allFix/timings #886rulerFix/timings #886scroll_down-ok but needs update to handler for new '.output' instancessplitcellFix/timings #886These I'm uncertain about:
python-markdown- does some running of things onkernel_ready.Kernel, but I'm not sure whether this needs special handling Test if notebook is loaded during initialization. #898runtools- has some odd dummy require thing going on to combat race conditions. I'm not immediately sure what's going on there Fix runtools initialization #897table_beautifier- probably ok (relies on catchingkernel_ready.Kernel), but I'm not certainhighlighter/highlighter.old.js- do we even need to keep this file?! [highlighter] Minor typo update + cleaning #899These seem to run at least some code without
load_ipython_extensionbeing called, which I don't think is supposed to happen, so perhaps need a bit
of reworking:
codefoldinghandle notebook_loaded for codefolding #888exerciseModularity - don't run things beforeload_ipython_extensionis called #895exercise2Modularity - don't run things beforeload_ipython_extensionis called #895highlighter/highlighter.jsModularity - don't run things beforeload_ipython_extensionis called #895move_selected_cellsModularity - don't run things beforeload_ipython_extensionis called #895skip-tracebackModularity - don't run things beforeload_ipython_extensionis called #895splitcellFix/timings #886These haven't even been updated to notebook
4.x, so should either be updated or removed:read-onlyRemove stale read-only extensions. #890historyRemove stale history extensions #889cellstateRemove stale extension #978searchremove stale nbextensions #891slidemode/slidemode2remove stale nbextensions #891swcremove stale nbextensions #891These already use the correct approach:
autoscrollcollapsible_headingsinit_cellnbTranslatetoc2And these don't need notebook loaded to initialize:
addbeforeautosavetimechrome-clipboardcode_font_sizecode_prettifycomment-uncommentcontrib_nbextensions_help_itemcss_selectordatestamperdragdropequation-numberinggist_ithelp_panelhide_headerhinterlandkeyboard_shortcut_editorlimit_outputnavigation-hotkeysnbTranslatenbviewer_themeno_exec_dundernotifyprintviewqtconsolerubberbandruntoolsscratchpadsearch-replaceselect_keymapskillsnippets_menusnippetsspellcheckertoc2toggle_all_line_numberstree-filterzenmode