Jupyter Notebook: Fix window.Plotly not defined error when running all cells #420
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In Dynamic Dashboards (jupyter/dashboards_server#108) we need to be able to execute all code cells of a Jupyter Notebook file. Currently Plotly does not work, with the error that
window.Plotly
is not defined. This problem can also be observed in a notebook when performing a "Run All" of all cells.@jdfreder's fix in #412 preserves requirejs when plotting in offline mode in the notebook, but causes an error if doing a "Run All" cells. The reason for this is the initialization of
window.Plotly
occurs in a requirejs function and is immediately followed by an attempt to access it outside of a requirejs function. At this pointwindow.Plotly
won't yet be defined since the definition will execute asynchronously after the attempt to access it.To fix, the call to
Plotly.newPlot()
could happen inside its own requrejs function, making use of the 'plotly' requirejs module. I have made the minimal change in this PR to demonstrate. (I kept the requirejs function that defineswindow.Plotly
, but it could potentially be removed in favor of using the requirejs module only.) It works for me when doing a "Run All" in the notebook and also works when deploying a dashboard read from the notebook file.This may also be related to #394 if the conversion of notebook to slides is doing a "Run All".