-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Changes from 1 commit
d2b9880
89c53db
c769ad6
555d89a
6ff77d6
78fc1d7
341e87f
17485bc
8eae6bd
ad728a3
b2f9db9
bdb4b85
0effa54
3172843
151f23e
cae796e
8b39241
e20da3e
6abf3f9
cac15c5
a49d903
f7d972c
4256891
d447ae4
bd46038
1c7d517
f5baa3e
dbbad22
5bf4dd5
1892ab6
94a55f9
6ea4e6d
e60a5dd
f44ad45
e943120
7b4681a
0582283
8689112
cf90fba
4235cd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
import uuid | ||
import warnings | ||
from pkg_resources import resource_string | ||
import time | ||
import webbrowser | ||
|
||
import plotly | ||
|
@@ -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): | ||
''' | ||
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 | ||
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. 🐐 |
||
page reloads. | ||
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. Nice 🎎! |
||
''' | ||
|
||
if caller == 'iplot': | ||
check_start = 'if(document.readyState == \'complete\') {{' | ||
check_end = '}}' | ||
elif type == 'plot': | ||
elif caller == 'plot': | ||
check_start = '' | ||
check_end = '' | ||
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. 🐄
? |
||
else: | ||
raise ValueError('caller should only be one of `iplot` or `plot`') | ||
|
||
return( | ||
('<script>' | ||
|
@@ -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') | ||
``` | ||
""" | ||
|
@@ -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)] | ||
) | ||
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. ;__; this will just break if you don't have enough Can you:
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. ahh I see. I think I could put the 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.
|
||
) | ||
# 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) | ||
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. Will this work for plots that take > 1 second to 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. Right so this is a bit of a concern. The most complicated plot I tried was one with 300 boxplots, I also dropped |
||
# inject code to download an image of the plot | ||
display(HTML(script)) | ||
|
@@ -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: | ||
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. |
||
script = image_download_script('plot') | ||
|
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.
🐄 We typically use
"""
, not'''
for docstrings.