-
-
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 5 commits
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
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
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 |
|---|---|---|
|
|
@@ -14,11 +14,10 @@ | |
|
|
||
| import plotly | ||
| from plotly import tools, utils | ||
| from plotly.exceptions import PlotlyError | ||
|
|
||
|
|
||
| try: | ||
| import IPython | ||
| from IPython.display import HTML, display | ||
| _ipython_imported = True | ||
| except ImportError: | ||
| _ipython_imported = False | ||
|
|
@@ -30,9 +29,6 @@ | |
| _matplotlib_imported = False | ||
|
|
||
|
|
||
| __PLOTLY_OFFLINE_INITIALIZED = False | ||
|
|
||
|
|
||
| def download_plotlyjs(download_url): | ||
| warnings.warn(''' | ||
| `download_plotlyjs` is deprecated and will be removed in the | ||
|
|
@@ -50,26 +46,36 @@ def get_plotlyjs(): | |
|
|
||
| def init_notebook_mode(): | ||
| """ | ||
| Initialize Plotly Offline mode in an IPython Notebook. | ||
| Run this function at the start of an IPython notebook | ||
| to load the necessary javascript files for creating | ||
| Plotly graphs with plotly.offline.iplot. | ||
| 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. | ||
| """ | ||
| if not tools._ipython_imported: | ||
| warnings.warn(''' | ||
| `init_notebook_mode` is deprecated and will be removed in the | ||
| next release. Notebook mode is now automatically initialized when | ||
| notebook methods are invoked, so it is no | ||
| longer necessary to manually initialize. | ||
| ''', DeprecationWarning) | ||
|
|
||
| if not _ipython_imported: | ||
| raise ImportError('`iplot` can only run inside an IPython Notebook.') | ||
| from IPython.display import HTML, display | ||
|
|
||
| global __PLOTLY_OFFLINE_INITIALIZED | ||
| if not __PLOTLY_OFFLINE_INITIALIZED: | ||
| display(HTML("<script type='text/javascript'>" + | ||
| "define('plotly', function(require, exports, module) {" + | ||
| get_plotlyjs() + | ||
| "});" + | ||
| "require(['plotly'], function(Plotly) {" + | ||
| "window.Plotly = Plotly;" + | ||
| "});" + | ||
| "</script>")) | ||
| __PLOTLY_OFFLINE_INITIALIZED = True | ||
| 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 _plot_html(figure_or_data, show_link, link_text, | ||
|
|
@@ -171,25 +177,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 | ||
| init_notebook_mode() | ||
|
|
||
| plot_html, plotdivid, width, height = _plot_html( | ||
| figure_or_data, show_link, link_text, validate, | ||
|
|
@@ -421,11 +415,9 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False, | |
|
|
||
| Example: | ||
| ``` | ||
| from plotly.offline import init_notebook_mode, iplot_mpl | ||
| from plotly.offline import iplot_mpl | ||
| import matplotlib.pyplot as plt | ||
|
|
||
| init_notebook_mode() | ||
|
|
||
| fig = plt.figure() | ||
| x = [10, 15, 20, 25, 30] | ||
| y = [100, 250, 200, 150, 300] | ||
|
|
@@ -434,6 +426,8 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False, | |
| iplot_mpl(fig) | ||
| ``` | ||
| """ | ||
| init_notebook_mode() | ||
|
|
||
| plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose) | ||
| return iplot(plotly_plot, show_link, link_text, validate) | ||
|
|
||
|
|
@@ -454,10 +448,9 @@ def enable_mpl_offline(resize=False, strip_style=False, | |
|
|
||
| Example: | ||
| ``` | ||
| from plotly.offline import init_notebook_mode, enable_mpl_offline | ||
| from plotly.offline import enable_mpl_offline | ||
| import matplotlib.pyplot as plt | ||
|
|
||
| init_notebook_mode() | ||
| enable_mpl_offline() | ||
|
|
||
| fig = plt.figure() | ||
|
|
@@ -467,8 +460,8 @@ def enable_mpl_offline(resize=False, strip_style=False, | |
| fig | ||
| ``` | ||
| """ | ||
| if not __PLOTLY_OFFLINE_INITIALIZED: | ||
| init_notebook_mode() | ||
| init_notebook_mode() | ||
|
|
||
| ip = IPython.core.getipython.get_ipython() | ||
| formatter = ip.display_formatter.formatters['text/html'] | ||
| formatter.for_type(matplotlib.figure.Figure, | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -27,10 +27,9 @@ | |
|
|
||
| class PlotlyOfflineTestCase(TestCase): | ||
| def setUp(self): | ||
| plotly.offline.offline.__PLOTLY_OFFLINE_INITIALIZED = False | ||
| pass | ||
|
|
||
| @raises(plotly.exceptions.PlotlyError) | ||
| def test_iplot_doesnt_work_before_you_call_init_notebook_mode(self): | ||
| def test_iplot_works_wihout_calling_init_notebook_mode(self): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎉 |
||
| plotly.offline.iplot([{}]) | ||
|
|
||
| def test_iplot_works_after_you_call_init_notebook_mode(self): | ||
|
|
||
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!