Skip to content

added option to download an image of the most recent plot #494

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 40 commits into from
Jun 15, 2016
Merged
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
d2b9880
added option to download an image of the most recent plot
yankev Jun 2, 2016
89c53db
remove an alert, change name of function
yankev Jun 2, 2016
c769ad6
updated docstring
yankev Jun 2, 2016
555d89a
removed unused variable `selected`
yankev Jun 2, 2016
6ff77d6
more docstring updates
yankev Jun 2, 2016
78fc1d7
added the ability to download plots when calling iplot
yankev Jun 3, 2016
341e87f
fixed the inline download method
yankev Jun 3, 2016
17485bc
renamed variables so tests will pass
yankev Jun 3, 2016
8eae6bd
attempt at stopping image download on future notebook openings
yankev Jun 4, 2016
ad728a3
fixed the download on refresh
yankev Jun 4, 2016
b2f9db9
fixing code to fix tests
yankev Jun 4, 2016
bdb4b85
Revert "fixing code to fix tests"
yankev Jun 4, 2016
0effa54
removed redundant return from _plot_html
yankev Jun 4, 2016
3172843
removed unused download arguments
yankev Jun 4, 2016
151f23e
providing functionality to all offline plot calls
yankev Jun 5, 2016
cae796e
pep8 and edit message in confirmation to download image
yankev Jun 5, 2016
8b39241
fixed typo
yankev Jun 5, 2016
e20da3e
ensuring the use of the correct argument names, and proper assignment…
yankev Jun 5, 2016
6abf3f9
added new arguments to docstrings
yankev Jun 5, 2016
cac15c5
changed `download_image` parameter to `image`
yankev Jun 7, 2016
a49d903
changed default value for image_filename
yankev Jun 7, 2016
f7d972c
added warning messages indicating the option to use `py.image`
yankev Jun 7, 2016
4256891
changed default filename for `download_notebook_image`
yankev Jun 7, 2016
d447ae4
add download image examples, update more default values
yankev Jun 7, 2016
bd46038
more docstring fixes and amending examples
yankev Jun 7, 2016
1c7d517
removed `download_notebook_image()` function
yankev Jun 7, 2016
f5baa3e
removed `download_notebook_image` from module init
yankev Jun 7, 2016
dbbad22
merged image and format into one parameter: image
yankev Jun 8, 2016
5bf4dd5
added new logic to deal with the new image parameter (iplot, plot)
yankev Jun 10, 2016
1892ab6
rename some arguments, typos, and formatting
yankev Jun 10, 2016
94a55f9
added confirm message to iplot mode
yankev Jun 10, 2016
6ea4e6d
edit value error
yankev Jun 10, 2016
e60a5dd
put download scripts into a function
yankev Jun 10, 2016
f44ad45
remove old scripts replaced by function calls
yankev Jun 10, 2016
e943120
remove image server warnings
yankev Jun 10, 2016
7b4681a
address PR comments
yankev Jun 14, 2016
0582283
fixed function names, as well as added a better check for the caller …
yankev Jun 14, 2016
8689112
Revert "fixed function names, as well as added a better check for the…
yankev Jun 14, 2016
cf90fba
renamed calls to `get_image_download_script` and fixed docstring
yankev Jun 14, 2016
4235cd4
fix ValueError message
yankev Jun 14, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions plotly/offline/offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import uuid
import warnings
from pkg_resources import resource_string
import time
import webbrowser

import plotly
Expand Down Expand Up @@ -48,14 +49,26 @@ def get_plotlyjs():
plotlyjs = resource_string('plotly', path).decode('utf-8')
return plotlyjs

def image_download_script(type):
def get_image_download_script(caller):
'''
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐄 We typically use """, not ''' for docstrings.

This function will return a script that will download an image of a Plotly
plot.

if type == 'iplot':
Keyword Arguments:
caller ('plot', 'iplot') -- specifies which function made the call for the
download script. If `iplot`, then an extra condition is added into the
download script to ensure that download prompts aren't iniitated on
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐐 iniitated --> initiated.

page reloads.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 🎎!

'''

if caller == 'iplot':
check_start = 'if(document.readyState == \'complete\') {{'
check_end = '}}'
elif type == 'plot':
elif caller == 'plot':
check_start = ''
check_end = ''
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐄

else:
    raise SomeException( .. )

?

else:
raise ValueError('caller should only be one of `iplot` or `plot`')

return(
('<script>'
Expand Down Expand Up @@ -258,7 +271,7 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
init_notebook_mode()
iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}])
# We can also download an image of the plot by setting the image to the
format you want ie: `image='png'`
format you want. e.g. `image='png'`
iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}], image='png')
```
"""
Expand All @@ -282,18 +295,20 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',

if image:
if image not in __IMAGE_FORMATS:
img_n = len(__IMAGE_FORMATS)
raise ValueError('The image parameter takes only the '
'following format types: `png`, `jpeg`, '
'`webp`, `svg`')
# if the check passes then download script injection will commence.
# Write script to download image of the plot
'following format types: `{}`, `{}`, '
'`{}`, `{}`'.format(*[__IMAGE_FORMATS[i]
for i in range(img_n)]
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

;__; this will just break if you don't have enough {}, right?

Can you:

'This image parameter must be one of {}.'.format(__IMAGE_FORMATS)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh I see. I think I could put the {} in a list comprehension to fix this. But I think I'll just implement your suggestion as it appears to be the cleaner option.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple is better than complex.

Zen of Python

)
# if image is given, and is a valid format, we will download the image
script = image_download_script('iplot').format(format=image,
width=image_width,
height=image_height,
filename=filename,
plot_id=plotdivid)
# allow time for the plot to draw
import time
time.sleep(1)
Copy link
Contributor

@theengineear theengineear Jun 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work for plots that take > 1 second to download draw?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right so this is a bit of a concern. The most complicated plot I tried was one with 300 boxplots, I also dropped time.sleep to 0.1 and there was no issue. I'm not really sure of a way to guarantee this success though.

# inject code to download an image of the plot
display(HTML(script))
Expand Down Expand Up @@ -402,9 +417,13 @@ def plot(figure_or_data,

if image:
if image not in __IMAGE_FORMATS:
img_n = len(__IMAGE_FORMATS)
raise ValueError('The image parameter takes only the '
'following format types: `png`, `jpeg`, '
'`webp`, `svg`')
'following format types: `{}`, `{}`, '
'`{}`, `{}`'.format(*[__IMAGE_FORMATS[i]
for i in range(img_n)]
)
)
# if the check passes then download script is injected.
# write the download script:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

script = image_download_script('plot')
Expand Down