-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Ensure plotlyjs loaded #466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
91e74fd
Offline: Add `load_plotlyjs` method to ensure it is always available
mdtusz 2de17c3
Offline: Cleanup unused code
mdtusz 593e84c
Offline: Add back try/except block for IPython
mdtusz 9126d58
Update plot schema
mdtusz 87217ab
Update test for plotly.js script loading changes
mdtusz abd4169
Bow to the :cow2:
mdtusz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Offline: Add
load_plotlyjs method to ensure it is always available
- Loading branch information
commit 91e74fddd4c9059ee05bdfd9221d9fde9d455252
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,30 @@ def get_plotlyjs(): | |
| return plotlyjs | ||
|
|
||
|
|
||
| def load_plotlyjs(): | ||
| """ | ||
| Initialize plotly.js in the browser if it hasn't been loaded into the DOM | ||
| yet. This is an idempotent method and can and should be called from any | ||
| offline methods that require plotly.js to be loaded into the notebook dom. | ||
| """ | ||
| script_inject = ( | ||
| '' | ||
| '<script type=\'text/javascript\'>' | ||
| 'if(!window.Plotly){{' | ||
| 'define(\'plotly\', function(require, exports, module) {{' | ||
| '{script}' | ||
| '}});' | ||
| 'require([\'plotly\'], function(Plotly) {{' | ||
| 'console.log(Plotly);' | ||
| 'window.Plotly = Plotly;' | ||
| '}});' | ||
| '}}' | ||
| '</script>' | ||
| '').format(script=get_plotlyjs()) | ||
|
|
||
| display(HTML(script_inject)) | ||
|
|
||
|
|
||
| def init_notebook_mode(): | ||
| """ | ||
| Initialize Plotly Offline mode in an IPython Notebook. | ||
|
|
@@ -171,25 +195,13 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly', | |
|
|
||
| Example: | ||
| ``` | ||
| from plotly.offline import init_notebook_mode, iplot | ||
| init_notebook_mode() | ||
| from plotly.offline import, iplot | ||
|
||
|
|
||
| iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}]) | ||
| ``` | ||
| """ | ||
| if not __PLOTLY_OFFLINE_INITIALIZED: | ||
| raise PlotlyError('\n'.join([ | ||
| 'Plotly Offline mode has not been initialized in this notebook. ' | ||
| 'Run: ', | ||
| '', | ||
| 'import plotly', | ||
| 'plotly.offline.init_notebook_mode() ' | ||
| '# run at the start of every ipython notebook', | ||
| ])) | ||
| if not tools._ipython_imported: | ||
| raise ImportError('`iplot` can only run inside an IPython Notebook.') | ||
|
|
||
| from IPython.display import HTML, display | ||
| load_plotlyjs() | ||
|
|
||
| plot_html, plotdivid, width, height = _plot_html( | ||
| figure_or_data, show_link, link_text, validate, | ||
|
|
@@ -434,6 +446,8 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False, | |
| iplot_mpl(fig) | ||
| ``` | ||
| """ | ||
| load_plotlyjs() | ||
|
|
||
| plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose) | ||
| return iplot(plotly_plot, show_link, link_text, validate) | ||
|
|
||
|
|
@@ -467,8 +481,8 @@ def enable_mpl_offline(resize=False, strip_style=False, | |
| fig | ||
| ``` | ||
| """ | ||
| if not __PLOTLY_OFFLINE_INITIALIZED: | ||
| init_notebook_mode() | ||
| load_plotlyjs() | ||
|
|
||
| ip = IPython.core.getipython.get_ipython() | ||
| formatter = ip.display_formatter.formatters['text/html'] | ||
| formatter.for_type(matplotlib.figure.Figure, | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐄 could use less escaping by following original formatting, but not needed!