Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c6bbb96

Browse files
committed
Merge remote-tracking branch 'plotly/master'
2 parents ef9f4ba + a1c0a9c commit c6bbb96

File tree

12 files changed

+2914
-565
lines changed

12 files changed

+2914
-565
lines changed

CHANGELOG.md

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,37 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5-
## [Unreleased]
5+
## [1.12.2] - 2016-06-20
6+
### Updated
7+
- Updated plotly.min.js so the offline mode is using plotly.js v1.13.0
8+
- Fix `Plotly.toImage` and `Plotly.downloadImage` bug specific to Chrome 51 on OSX
9+
- Beta version of the scattermapbox trace type - which allows users to create mapbox-gl maps using the plotly.js API. Note that scattermapbox is only available through custom bundling in this release.
10+
- See [the plotly.js CHANGELOG](https://github.com/plotly/plotly.js/blob/master/CHANGELOG.md#1130----2016-05-26) for additional additions and updates.
11+
12+
### Added
13+
- The FigureFactory can now create gantt charts with `.create_gantt`. Check it out with:
14+
```
15+
import plotly.tools as tls
16+
help(tls.FigureFactory.create_gantt)
17+
```
18+
- Ability to download images in offline mode. By providing an extra keyword `image` to the existing plot calls, you can now download the images of the plots you make in offline mode.
19+
20+
### Fixed
21+
- Fixed check for the height parameter passed to `_plot_html`, and now sets the correct `link text` for plots
22+
generated in offline mode.
23+
24+
25+
## [1.12.1] - 2016-06-19
26+
### Added
27+
- The FigureFactory can now create violin plots with `.create_violin`. Check it out with:
28+
```
29+
import plotly.tools as tls
30+
help(tls.FigureFactory.create_violin)
31+
```
32+
33+
## [1.12.0] - 2016-06-06
34+
### Added
35+
- Added ability to enable/disable SSL certificate verification for streaming. Disabling SSL certification verification requires Python v2.7.9 / v3.4.3 (or above). This feature can be toggled via the `plotly_ssl_verification` configuration setting.
636

737
## [1.11.0] - 2016-05-27
838
### Updated
@@ -22,12 +52,18 @@ Note: This is a backwards incompatible change.
2252

2353
- In `create_trisurf`, the parameter `dist_func` has been replaced by `color_func`. Users can now:
2454
- Input a list of different color types (hex, tuple, rgb) to `colormap` to map colors divergently
25-
- Input a list|array of hex and rgb colors to `color_func` to assign each simplex to a color
55+
- Input a list|array of hex and rgb colors to `color_func` to assign each simplex to a color
2656

2757
### Added
2858
- Added the option to load plotly.js from a CDN by setting the parameter `connected=True`
2959
in the `init_notebook_mode()` function call
30-
60+
- The FigureFactory can now create trisurf plots with `.create_trisurf`. Check it out with:
61+
```
62+
import plotly.tools as tls
63+
help(tls.FigureFactory.create_trisurf)
64+
```
65+
66+
3167

3268
## [1.10.0] - 2016-05-19
3369
### Fixed

plotly/graph_reference/default-schema.json

Lines changed: 92 additions & 92 deletions
Large diffs are not rendered by default.

plotly/offline/offline.py

Lines changed: 144 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import uuid
1111
import warnings
1212
from pkg_resources import resource_string
13+
import time
1314
import webbrowser
1415

1516
import plotly
@@ -31,6 +32,8 @@
3132

3233
__PLOTLY_OFFLINE_INITIALIZED = False
3334

35+
__IMAGE_FORMATS = ['jpeg', 'png', 'webp', 'svg']
36+
3437

3538
def download_plotlyjs(download_url):
3639
warnings.warn('''
@@ -46,6 +49,49 @@ def get_plotlyjs():
4649
plotlyjs = resource_string('plotly', path).decode('utf-8')
4750
return plotlyjs
4851

52+
def get_image_download_script(caller):
53+
"""
54+
This function will return a script that will download an image of a Plotly
55+
plot.
56+
57+
Keyword Arguments:
58+
caller ('plot', 'iplot') -- specifies which function made the call for the
59+
download script. If `iplot`, then an extra condition is added into the
60+
download script to ensure that download prompts aren't initiated on
61+
page reloads.
62+
"""
63+
64+
if caller == 'iplot':
65+
check_start = 'if(document.readyState == \'complete\') {{'
66+
check_end = '}}'
67+
elif caller == 'plot':
68+
check_start = ''
69+
check_end = ''
70+
else:
71+
raise ValueError('caller should only be one of `iplot` or `plot`')
72+
73+
return(
74+
('<script>'
75+
'function downloadimage(format, height, width,'
76+
' filename) {{'
77+
'var p = document.getElementById(\'{plot_id}\');'
78+
'Plotly.downloadImage(p, {{format: format, height: height, '
79+
'width: width, filename: filename}});'
80+
'}};' +
81+
check_start +
82+
'if(confirm(\'Do you want to save this image as '
83+
'{filename}.{format}?\\n\\n'
84+
'For higher resolution images and more export options, '
85+
'consider making requests to our image servers. Type: '
86+
'help(py.image) for more details.'
87+
'\')) {{'
88+
'downloadimage(\'{format}\', {height}, {width}, '
89+
'\'{filename}\');}}' +
90+
check_end +
91+
'</script>'
92+
)
93+
)
94+
4995

5096
def init_notebook_mode(connected=False):
5197
"""
@@ -114,8 +160,8 @@ def init_notebook_mode(connected=False):
114160
__PLOTLY_OFFLINE_INITIALIZED = True
115161

116162

117-
def _plot_html(figure_or_data, show_link, link_text,
118-
validate, default_width, default_height, global_requirejs):
163+
def _plot_html(figure_or_data, show_link, link_text, validate,
164+
default_width, default_height, global_requirejs):
119165

120166
figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
121167

@@ -130,11 +176,11 @@ def _plot_html(figure_or_data, show_link, link_text,
130176
width = str(width) + 'px'
131177

132178
try:
133-
float(width)
179+
float(height)
134180
except (ValueError, TypeError):
135181
pass
136182
else:
137-
width = str(width) + 'px'
183+
height = str(height) + 'px'
138184

139185
plotdivid = uuid.uuid4()
140186
jdata = json.dumps(figure.get('data', []), cls=utils.PlotlyJSONEncoder)
@@ -156,6 +202,7 @@ def _plot_html(figure_or_data, show_link, link_text,
156202
.replace('https://', '')\
157203
.replace('http://', '')
158204
link_text = link_text.replace('plot.ly', link_domain)
205+
config['linkText'] = link_text
159206

160207
script = 'Plotly.newPlot("{id}", {data}, {layout}, {config})'.format(
161208
id=plotdivid,
@@ -185,9 +232,9 @@ def _plot_html(figure_or_data, show_link, link_text,
185232

186233
return plotly_html_div, plotdivid, width, height
187234

188-
189235
def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
190-
validate=True):
236+
validate=True, image=None, filename='plot_image', image_width=800,
237+
image_height=600):
191238
"""
192239
Draw plotly graphs inside an IPython notebook without
193240
connecting to an external server.
@@ -210,12 +257,23 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
210257
has become outdated with your version of
211258
graph_reference.json or if you need to include
212259
extra, unnecessary keys in your figure.
260+
image (default=None |'png' |'jpeg' |'svg' |'webp') -- This parameter sets
261+
the format of the image to be downloaded, if we choose to download an
262+
image. This parameter has a default value of None indicating that no
263+
image should be downloaded.
264+
filename (default='plot') -- Sets the name of the file your image
265+
will be saved to. The extension should not be included.
266+
image_height (default=600) -- Specifies the height of the image in `px`.
267+
image_width (default=800) -- Specifies the width of the image in `px`.
213268
214269
Example:
215270
```
216271
from plotly.offline import init_notebook_mode, iplot
217272
init_notebook_mode()
218273
iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}])
274+
# We can also download an image of the plot by setting the image to the
275+
format you want. e.g. `image='png'`
276+
iplot([{'x': [1, 2, 3], 'y': [5, 2, 7]}], image='png')
219277
```
220278
"""
221279
if not __PLOTLY_OFFLINE_INITIALIZED:
@@ -236,13 +294,30 @@ def iplot(figure_or_data, show_link=True, link_text='Export to plot.ly',
236294

237295
display(HTML(plot_html))
238296

297+
if image:
298+
if image not in __IMAGE_FORMATS:
299+
raise ValueError('The image parameter must be one of the following'
300+
': {}'.format(__IMAGE_FORMATS)
301+
)
302+
# if image is given, and is a valid format, we will download the image
303+
script = get_image_download_script('iplot').format(format=image,
304+
width=image_width,
305+
height=image_height,
306+
filename=filename,
307+
plot_id=plotdivid)
308+
# allow time for the plot to draw
309+
time.sleep(1)
310+
# inject code to download an image of the plot
311+
display(HTML(script))
312+
239313

240314
def plot(figure_or_data,
241315
show_link=True, link_text='Export to plot.ly',
242316
validate=True, output_type='file',
243317
include_plotlyjs=True,
244-
filename='temp-plot.html',
245-
auto_open=True):
318+
filename='temp-plot.html', auto_open=True,
319+
image=None, image_filename='plot_image',
320+
image_width=800, image_height=600):
246321
""" Create a plotly graph locally as an HTML document or string.
247322
248323
Example:
@@ -251,6 +326,10 @@ def plot(figure_or_data,
251326
import plotly.graph_objs as go
252327
253328
plot([go.Scatter(x=[1, 2, 3], y=[3, 2, 6])], filename='my-graph.html')
329+
# We can also download an image of the plot by setting the image parameter
330+
# to the image format we want
331+
plot([go.Scatter(x=[1, 2, 3], y=[3, 2, 6])], filename='my-graph.html'
332+
image='jpeg')
254333
```
255334
More examples below.
256335
@@ -288,6 +367,14 @@ def plot(figure_or_data,
288367
auto_open (default=True) -- If True, open the saved file in a
289368
web browser after saving.
290369
This argument only applies if `output_type` is 'file'.
370+
image (default=None |'png' |'jpeg' |'svg' |'webp') -- This parameter sets
371+
the format of the image to be downloaded, if we choose to download an
372+
image. This parameter has a default value of None indicating that no
373+
image should be downloaded.
374+
image_filename (default='plot_image') -- Sets the name of the file your image
375+
will be saved to. The extension should not be included.
376+
image_height (default=600) -- Specifies the height of the image in `px`.
377+
image_width (default=800) -- Specifies the width of the image in `px`.
291378
"""
292379
if output_type not in ['div', 'file']:
293380
raise ValueError(
@@ -325,13 +412,30 @@ def plot(figure_or_data,
325412
else:
326413
plotly_js_script = ''
327414

415+
if image:
416+
if image not in __IMAGE_FORMATS:
417+
raise ValueError('The image parameter must be one of the '
418+
'following: {}'.format(__IMAGE_FORMATS)
419+
)
420+
# if the check passes then download script is injected.
421+
# write the download script:
422+
script = get_image_download_script('plot')
423+
script = script.format(format=image,
424+
width=image_width,
425+
height=image_height,
426+
filename=image_filename,
427+
plot_id=plotdivid)
428+
else:
429+
script = ''
430+
328431
f.write(''.join([
329432
'<html>',
330433
'<head><meta charset="utf-8" /></head>',
331434
'<body>',
332435
plotly_js_script,
333436
plot_html,
334437
resize_script,
438+
script,
335439
'</body>',
336440
'</html>']))
337441

@@ -358,7 +462,9 @@ def plot(figure_or_data,
358462
def plot_mpl(mpl_fig, resize=False, strip_style=False,
359463
verbose=False, show_link=True, link_text='Export to plot.ly',
360464
validate=True, output_type='file', include_plotlyjs=True,
361-
filename='temp-plot.html', auto_open=True):
465+
filename='temp-plot.html', auto_open=True,
466+
image=None, image_filename='plot_image',
467+
image_height=600, image_width=800):
362468
"""
363469
Convert a matplotlib figure to a Plotly graph stored locally as HTML.
364470
@@ -402,6 +508,14 @@ def plot_mpl(mpl_fig, resize=False, strip_style=False,
402508
auto_open (default=True) -- If True, open the saved file in a
403509
web browser after saving.
404510
This argument only applies if `output_type` is 'file'.
511+
image (default=None |'png' |'jpeg' |'svg' |'webp') -- This parameter sets
512+
the format of the image to be downloaded, if we choose to download an
513+
image. This parameter has a default value of None indicating that no
514+
image should be downloaded.
515+
image_filename (default='plot_image') -- Sets the name of the file your
516+
image will be saved to. The extension should not be included.
517+
image_height (default=600) -- Specifies the height of the image in `px`.
518+
image_width (default=800) -- Specifies the width of the image in `px`.
405519
406520
Example:
407521
```
@@ -416,16 +530,22 @@ def plot_mpl(mpl_fig, resize=False, strip_style=False,
416530
plt.plot(x, y, "o")
417531
418532
plot_mpl(fig)
533+
# If you want to to download an image of the figure as well
534+
plot_mpl(fig, image='png')
419535
```
420536
"""
421537
plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose)
422538
return plot(plotly_plot, show_link, link_text, validate, output_type,
423-
include_plotlyjs, filename, auto_open)
539+
include_plotlyjs, filename, auto_open,
540+
image=image, image_filename=image_filename,
541+
image_height=image_height, image_width=image_width)
424542

425543

426544
def iplot_mpl(mpl_fig, resize=False, strip_style=False,
427545
verbose=False, show_link=True,
428-
link_text='Export to plot.ly', validate=True):
546+
link_text='Export to plot.ly', validate=True,
547+
image=None, image_filename='plot_image',
548+
image_height=600, image_width=800):
429549
"""
430550
Convert a matplotlib figure to a plotly graph and plot inside an IPython
431551
notebook without connecting to an external server.
@@ -454,6 +574,14 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False,
454574
has become outdated with your version of
455575
graph_reference.json or if you need to include
456576
extra, unnecessary keys in your figure.
577+
image (default=None |'png' |'jpeg' |'svg' |'webp') -- This parameter sets
578+
the format of the image to be downloaded, if we choose to download an
579+
image. This parameter has a default value of None indicating that no
580+
image should be downloaded.
581+
image_filename (default='plot_image') -- Sets the name of the file your
582+
image will be saved to. The extension should not be included.
583+
image_height (default=600) -- Specifies the height of the image in `px`.
584+
image_width (default=800) -- Specifies the width of the image in `px`.
457585
458586
Example:
459587
```
@@ -467,10 +595,14 @@ def iplot_mpl(mpl_fig, resize=False, strip_style=False,
467595
468596
init_notebook_mode()
469597
iplot_mpl(fig)
598+
# and if you want to download an image of the figure as well
599+
iplot_mpl(fig, image='jpeg')
470600
```
471601
"""
472602
plotly_plot = tools.mpl_to_plotly(mpl_fig, resize, strip_style, verbose)
473-
return iplot(plotly_plot, show_link, link_text, validate)
603+
return iplot(plotly_plot, show_link, link_text, validate,
604+
image=image, filename=image_filename,
605+
image_height=image_height, image_width=image_width)
474606

475607

476608
def enable_mpl_offline(resize=False, strip_style=False,

plotly/offline/plotly.min.js

Lines changed: 23 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)