Skip to content

Commit 78fc1d7

Browse files
committed
added the ability to download plots when calling iplot
1 parent 6ff77d6 commit 78fc1d7

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

plotly/offline/offline.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ def init_notebook_mode(connected=False):
114114
__PLOTLY_OFFLINE_INITIALIZED = True
115115

116116

117-
def _plot_html(figure_or_data, show_link, link_text,
118-
validate, default_width, default_height, global_requirejs):
117+
def _plot_html(figure_or_data, show_link, link_text, validate,
118+
default_width, default_height, global_requirejs,
119+
download=False):
119120

120121
figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
121122

@@ -183,11 +184,12 @@ def _plot_html(figure_or_data, show_link, link_text,
183184
id=plotdivid, script=script,
184185
height=height, width=width)
185186

186-
return plotly_html_div, plotdivid, width, height
187+
return plotly_html_div, plotdivid, width, height, plotdivid
187188

188189

189190
def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
190-
validate=True):
191+
validate=True, download_image=False, format='png',
192+
width=800, height=600, filename='newplot'):
191193
"""
192194
Draw plotly graphs inside an IPython notebook without
193195
connecting to an external server.
@@ -230,12 +232,22 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
230232
if not tools._ipython_imported:
231233
raise ImportError('`iplot` can only run inside an IPython Notebook.')
232234

233-
plot_html, plotdivid, width, height = _plot_html(
235+
plot_html, plotdivid, width, height, plot_id = _plot_html(
234236
figure_or_data, show_link, link_text, validate,
235-
'100%', 525, global_requirejs=True)
237+
'100%', 525, global_requirejs=True, download=download_image)
236238

237239
display(HTML(plot_html))
238240

241+
# Use the plot id to download the image now:
242+
243+
script = ('<script>'
244+
'Plotly.downloadImage(\'{plot_id}\', {{format: \'{format}\', '
245+
'height: {height}, width: {width}, filename: \'{filename}\'}});'
246+
'</script>'
247+
).format(format=format, width=width, height=height,
248+
filename=filename, plot_id=plot_id)
249+
250+
display(HTML(script))
239251

240252
def plot(figure_or_data,
241253
show_link=True, link_text='Export to plot.ly',

0 commit comments

Comments
 (0)