-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
offline.plot/iplot improvements #1234
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
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d486d8e
Support auto-resize in offline.plot with output_type='div' and includ…
jonmmease 1494538
Support auto-resize for classic notebook iplot
jonmmease 0cbcce7
Add 'cdn' and 'directory' include_plotlyjs options in offline.plot
jonmmease f4f17ff
Merge branch 'master' into enh_1043
jonmmease 69dc7ba
Add option to specify include_plotlyjs as a path/url to a *.js file
jonmmease 1d4d090
Small review updates
jonmmease 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 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 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 |
---|---|---|
|
@@ -21,14 +21,29 @@ | |
) | ||
} | ||
|
||
|
||
resize_code_strings = [ | ||
'window.addEventListener("resize", ', | ||
'Plotly.Plots.resize(' | ||
] | ||
|
||
|
||
PLOTLYJS = plotly.offline.offline.get_plotlyjs() | ||
|
||
cdn_script = ('<script src="https://cdn.plot.ly/plotly-latest.min.js">' | ||
'</script>') | ||
|
||
directory_script = '<script src="plotly.min.js"></script>' | ||
|
||
|
||
class PlotlyOfflineBaseTestCase(TestCase): | ||
def tearDown(self): | ||
# Some offline tests produce an html file. Make sure we clean up :) | ||
try: | ||
os.remove('temp-plot.html') | ||
# Some tests that produce temp-plot.html] | ||
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. remove ']' character |
||
# also produce plotly.min.js | ||
os.remove('plotly.min.js') | ||
except OSError: | ||
pass | ||
|
||
|
@@ -64,10 +79,142 @@ def test_default_plot_generates_expected_html(self): | |
# and it's an <html> doc | ||
self.assertTrue(html.startswith('<html>') and html.endswith('</html>')) | ||
|
||
def test_including_plotlyjs(self): | ||
html = self._read_html(plotly.offline.plot(fig, include_plotlyjs=False, | ||
auto_open=False)) | ||
self.assertNotIn(PLOTLYJS, html) | ||
def test_including_plotlyjs_truthy_html(self): | ||
# For backwards compatibility all truthy values that aren't otherwise | ||
# recognized are considered true | ||
for include_plotlyjs in [True, 34, 'non-empty-str']: | ||
html = self._read_html(plotly.offline.plot( | ||
fig, | ||
include_plotlyjs=include_plotlyjs, | ||
output_type='file', | ||
auto_open=False)) | ||
self.assertIn(PLOTLYJS, html) | ||
self.assertNotIn(cdn_script, html) | ||
self.assertNotIn(directory_script, html) | ||
|
||
def test_including_plotlyjs_truthy_div(self): | ||
# For backwards compatibility all truthy values that aren't otherwise | ||
# recognized are considered true | ||
for include_plotlyjs in [True, 34, 'non-empty-str']: | ||
html = plotly.offline.plot( | ||
fig, | ||
include_plotlyjs=include_plotlyjs, | ||
output_type='div') | ||
self.assertIn(PLOTLYJS, html) | ||
self.assertNotIn(cdn_script, html) | ||
self.assertNotIn(directory_script, html) | ||
|
||
def test_including_plotlyjs_false_html(self): | ||
# For backwards compatibility all truthy values that aren't otherwise | ||
# recognized are considered true | ||
for include_plotlyjs in [False, 0, '']: | ||
html = self._read_html(plotly.offline.plot( | ||
fig, | ||
include_plotlyjs=include_plotlyjs, | ||
output_type='file', | ||
auto_open=False)) | ||
self.assertNotIn(PLOTLYJS, html) | ||
self.assertNotIn(cdn_script, html) | ||
self.assertNotIn(directory_script, html) | ||
|
||
def test_including_plotlyjs_false_div(self): | ||
for include_plotlyjs in [False, 0, '']: | ||
html = plotly.offline.plot( | ||
fig, | ||
include_plotlyjs=include_plotlyjs, | ||
output_type='div') | ||
self.assertNotIn(PLOTLYJS, html) | ||
self.assertNotIn(cdn_script, html) | ||
self.assertNotIn(directory_script, html) | ||
|
||
def test_including_plotlyjs_cdn_html(self): | ||
for include_plotlyjs in ['cdn', 'CDN', 'Cdn']: | ||
html = self._read_html(plotly.offline.plot( | ||
fig, | ||
include_plotlyjs=include_plotlyjs, | ||
output_type='file', | ||
auto_open=False)) | ||
self.assertNotIn(PLOTLYJS, html) | ||
self.assertIn(cdn_script, html) | ||
self.assertNotIn(directory_script, html) | ||
|
||
def test_including_plotlyjs_cdn_div(self): | ||
for include_plotlyjs in ['cdn', 'CDN', 'Cdn']: | ||
html = plotly.offline.plot( | ||
fig, | ||
include_plotlyjs=include_plotlyjs, | ||
output_type='div') | ||
self.assertNotIn(PLOTLYJS, html) | ||
self.assertIn(cdn_script, html) | ||
self.assertNotIn(directory_script, html) | ||
|
||
def test_including_plotlyjs_directory_html(self): | ||
self.assertFalse(os.path.exists('plotly.min.js')) | ||
|
||
for include_plotlyjs in ['directory', 'Directory', 'DIRECTORY']: | ||
html = self._read_html(plotly.offline.plot( | ||
fig, | ||
include_plotlyjs=include_plotlyjs, | ||
auto_open=False)) | ||
self.assertNotIn(PLOTLYJS, html) | ||
self.assertNotIn(cdn_script, html) | ||
self.assertIn(directory_script, html) | ||
|
||
# plot creates plotly.min.js in the output directory | ||
self.assertTrue(os.path.exists('plotly.min.js')) | ||
with open('plotly.min.js', 'r') as f: | ||
self.assertEqual(f.read(), PLOTLYJS) | ||
|
||
def test_including_plotlyjs_directory_div(self): | ||
self.assertFalse(os.path.exists('plotly.min.js')) | ||
|
||
for include_plotlyjs in ['directory', 'Directory', 'DIRECTORY']: | ||
html = plotly.offline.plot( | ||
fig, | ||
include_plotlyjs=include_plotlyjs, | ||
output_type='div', | ||
auto_open=False) | ||
|
||
self.assertNotIn(PLOTLYJS, html) | ||
self.assertNotIn(cdn_script, html) | ||
self.assertIn(directory_script, html) | ||
|
||
# plot does NOT create a plotly.min.js file in the output directory | ||
# when output_type is div | ||
self.assertFalse(os.path.exists('plotly.min.js')) | ||
|
||
def test_including_plotlyjs_path_html(self): | ||
for include_plotlyjs in [ | ||
('https://cdnjs.cloudflare.com/ajax/libs/plotly.js/1.40.1/' | ||
'plotly.min.js'), | ||
'subpath/to/plotly.min.js', | ||
'something.js']: | ||
|
||
html = self._read_html(plotly.offline.plot( | ||
fig, | ||
include_plotlyjs=include_plotlyjs, | ||
output_type='file', | ||
auto_open=False)) | ||
self.assertNotIn(PLOTLYJS, html) | ||
self.assertNotIn(cdn_script, html) | ||
self.assertNotIn(directory_script, html) | ||
self.assertIn(include_plotlyjs, html) | ||
|
||
def test_including_plotlyjs_path_div(self): | ||
for include_plotlyjs in [ | ||
('https://cdnjs.cloudflare.com/ajax/libs/plotly.js/1.40.1/' | ||
'plotly.min.js'), | ||
'subpath/to/plotly.min.js', | ||
'something.js']: | ||
|
||
html = plotly.offline.plot( | ||
fig, | ||
include_plotlyjs=include_plotlyjs, | ||
output_type='div') | ||
self.assertNotIn(PLOTLYJS, html) | ||
self.assertNotIn(cdn_script, html) | ||
self.assertNotIn(directory_script, html) | ||
self.assertIn(include_plotlyjs, html) | ||
|
||
def test_div_output(self): | ||
html = plotly.offline.plot(fig, output_type='div', auto_open=False) | ||
|
@@ -77,10 +224,7 @@ def test_div_output(self): | |
self.assertTrue(html.startswith('<div>') and html.endswith('</div>')) | ||
|
||
def test_autoresizing(self): | ||
resize_code_strings = [ | ||
'window.addEventListener("resize", ', | ||
'Plotly.Plots.resize(' | ||
] | ||
|
||
# If width or height wasn't specified, then we add a window resizer | ||
html = self._read_html(plotly.offline.plot(fig, auto_open=False)) | ||
for resize_code_string in resize_code_strings: | ||
|
@@ -96,6 +240,28 @@ def test_autoresizing(self): | |
for resize_code_string in resize_code_strings: | ||
self.assertNotIn(resize_code_string, html) | ||
|
||
def test_autoresizing_div(self): | ||
|
||
# If width or height wasn't specified, then we add a window resizer | ||
for include_plotlyjs in [True, False, 'cdn', 'directory']: | ||
html = plotly.offline.plot(fig, | ||
output_type='div', | ||
include_plotlyjs=include_plotlyjs) | ||
|
||
for resize_code_string in resize_code_strings: | ||
self.assertIn(resize_code_string, html) | ||
|
||
# If width or height was specified, then we don't resize | ||
html = plotly.offline.plot({ | ||
'data': fig['data'], | ||
'layout': { | ||
'width': 500, 'height': 500 | ||
} | ||
}, output_type='div') | ||
|
||
for resize_code_string in resize_code_strings: | ||
self.assertNotIn(resize_code_string, html) | ||
|
||
def test_config(self): | ||
config = dict(linkText='Plotly rocks!', | ||
editable=True) | ||
|
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.
Remove empty string here