Skip to content

Commit 7aa5561

Browse files
committed
add scale and the associated tests
1 parent 90fec76 commit 7aa5561

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

plotly/plotly/plotly.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ class image:
545545
546546
"""
547547
@staticmethod
548-
def get(figure_or_data, format='png', width=None, height=None):
548+
def get(figure_or_data, format='png', width=None, height=None, scale=None):
549549
"""
550550
Return a static image of the plot described by `figure`.
551551
@@ -570,6 +570,13 @@ def get(figure_or_data, format='png', width=None, height=None):
570570
"supported file types here: "
571571
"https://plot.ly/python/static-image-export/"
572572
)
573+
if scale is not None:
574+
try:
575+
scale = float(scale)
576+
except:
577+
raise exceptions.PlotlyError(
578+
"Invalid scale parameter. Scale must be a number."
579+
)
573580

574581
headers = _api_v2.headers()
575582
headers['plotly_version'] = version.__version__
@@ -580,7 +587,8 @@ def get(figure_or_data, format='png', width=None, height=None):
580587
payload['width'] = width
581588
if height is not None:
582589
payload['height'] = height
583-
590+
if scale is not None:
591+
payload['scale'] = scale
584592
url = _api_v2.api_url('images/')
585593

586594
res = requests.post(
@@ -614,21 +622,22 @@ def get(figure_or_data, format='png', width=None, height=None):
614622
"not be translated.")
615623
raise exceptions.PlotlyError(return_data['error'])
616624

617-
618625
@classmethod
619-
def ishow(cls, figure_or_data, format='png', width=None, height=None):
626+
def ishow(cls, figure_or_data, format='png', width=None, height=None,
627+
scale=None):
620628
"""
621629
Display a static image of the plot described by `figure`
622630
in an IPython Notebook.
623631
624632
"""
625633
if format == 'pdf':
626-
raise exceptions.PlotlyError("Aw, snap! "
634+
raise exceptions.PlotlyError(
635+
"Aw, snap! "
627636
"It's not currently possible to embed a pdf into "
628637
"an IPython notebook. You can save the pdf "
629638
"with the `image.save_as` or you can "
630639
"embed an png, jpeg, or svg.")
631-
img = cls.get(figure_or_data, format, width, height)
640+
img = cls.get(figure_or_data, format, width, height, scale)
632641
from IPython.display import display, Image, SVG
633642
if format == 'svg':
634643
display(SVG(img))
@@ -637,7 +646,7 @@ def ishow(cls, figure_or_data, format='png', width=None, height=None):
637646

638647
@classmethod
639648
def save_as(cls, figure_or_data, filename, format=None, width=None,
640-
height=None):
649+
height=None, scale=None):
641650
"""
642651
Save a image of the plot described by `figure` locally as `filename`.
643652
@@ -655,7 +664,7 @@ def save_as(cls, figure_or_data, filename, format=None, width=None,
655664
elif not ext and format:
656665
filename += '.' + format
657666

658-
img = cls.get(figure_or_data, format, width, height)
667+
img = cls.get(figure_or_data, format, width, height, scale)
659668

660669
f = open(filename, 'wb')
661670
f.write(img)

plotly/tests/test_core/test_image/__init__.py

Whitespace-only changes.

plotly/tests/test_core/test_image/test_image.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@ def setUp(self):
1919

2020

2121
def _generate_image_get_returns_valid_image_test(image_format,
22-
width, height):
22+
width, height, scale):
2323
def test(self):
24-
image = py.image.get(self.data, image_format, width, height)
24+
image = py.image.get(self.data, image_format, width, height, scale)
2525
if image_format in ['png', 'jpeg']:
2626
assert imghdr.what('', image) == image_format
2727

2828
return test
2929

3030

3131
def _generate_image_save_as_saves_valid_image(image_format,
32-
width, height):
32+
width, height, scale):
3333
def _test(self):
3434
f, filename = tempfile.mkstemp('.{}'.format(image_format))
3535
py.image.save_as(self.data, filename, format=image_format,
36-
width=width, height=height)
36+
width=width, height=height, scale=scale)
3737
if image_format in ['png', 'jpeg']:
3838
assert imghdr.what(filename) == image_format
3939
else:
@@ -46,7 +46,8 @@ def _test(self):
4646
kwargs = {
4747
'format': ['png', 'jpeg', 'pdf', 'svg'],
4848
'width': [None, 300],
49-
'height': [None, 300]
49+
'height': [None, 300],
50+
'scale': [None, 5]
5051
}
5152

5253
for args in itertools.product(kwargs['format'], kwargs['width'],

0 commit comments

Comments
 (0)