Skip to content

Commit f5f7881

Browse files
committed
update finance charts to resolve conflicts
2 parents 15d7f19 + 94234f4 commit f5f7881

File tree

5 files changed

+148
-21
lines changed

5 files changed

+148
-21
lines changed

plotly/plotly/plotly.py

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -545,11 +545,26 @@ class image:
545545
546546
"""
547547
@staticmethod
548-
def get(figure_or_data, format='png', width=None, height=None):
549-
"""
550-
Return a static image of the plot described by `figure`.
548+
def get(figure_or_data, format='png', width=None, height=None, scale=None):
549+
"""Return a static image of the plot described by `figure_or_data`.
551550
552-
Valid formats: 'png', 'svg', 'jpeg', 'pdf'
551+
positional arguments:
552+
- figure_or_data: The figure dict-like or data list-like object that
553+
describes a plotly figure.
554+
Same argument used in `py.plot`, `py.iplot`,
555+
see https://plot.ly/python for examples
556+
- format: 'png', 'svg', 'jpeg', 'pdf'
557+
- width: output width
558+
- height: output height
559+
- scale: Increase the resolution of the image by `scale` amount (e.g. `3`)
560+
Only valid for PNG and JPEG images.
561+
562+
example:
563+
```
564+
import plotly.plotly as py
565+
fig = {'data': [{'x': [1, 2, 3], 'y': [3, 1, 5], 'type': 'bar'}]}
566+
py.image.get(fig, 'png', scale=3)
567+
```
553568
554569
"""
555570
# TODO: format is a built-in name... we shouldn't really use it
@@ -570,6 +585,13 @@ def get(figure_or_data, format='png', width=None, height=None):
570585
"supported file types here: "
571586
"https://plot.ly/python/static-image-export/"
572587
)
588+
if scale is not None:
589+
try:
590+
scale = float(scale)
591+
except:
592+
raise exceptions.PlotlyError(
593+
"Invalid scale parameter. Scale must be a number."
594+
)
573595

574596
headers = _api_v2.headers()
575597
headers['plotly_version'] = version.__version__
@@ -580,7 +602,8 @@ def get(figure_or_data, format='png', width=None, height=None):
580602
payload['width'] = width
581603
if height is not None:
582604
payload['height'] = height
583-
605+
if scale is not None:
606+
payload['scale'] = scale
584607
url = _api_v2.api_url('images/')
585608

586609
res = requests.post(
@@ -614,21 +637,37 @@ def get(figure_or_data, format='png', width=None, height=None):
614637
"not be translated.")
615638
raise exceptions.PlotlyError(return_data['error'])
616639

617-
618640
@classmethod
619-
def ishow(cls, figure_or_data, format='png', width=None, height=None):
620-
"""
621-
Display a static image of the plot described by `figure`
641+
def ishow(cls, figure_or_data, format='png', width=None, height=None,
642+
scale=None):
643+
"""Display a static image of the plot described by `figure_or_data`
622644
in an IPython Notebook.
623645
646+
positional arguments:
647+
- figure_or_data: The figure dict-like or data list-like object that
648+
describes a plotly figure.
649+
Same argument used in `py.plot`, `py.iplot`,
650+
see https://plot.ly/python for examples
651+
- format: 'png', 'svg', 'jpeg', 'pdf'
652+
- width: output width
653+
- height: output height
654+
- scale: Increase the resolution of the image by `scale` amount
655+
Only valid for PNG and JPEG images.
656+
657+
example:
658+
```
659+
import plotly.plotly as py
660+
fig = {'data': [{'x': [1, 2, 3], 'y': [3, 1, 5], 'type': 'bar'}]}
661+
py.image.ishow(fig, 'png', scale=3)
624662
"""
625663
if format == 'pdf':
626-
raise exceptions.PlotlyError("Aw, snap! "
664+
raise exceptions.PlotlyError(
665+
"Aw, snap! "
627666
"It's not currently possible to embed a pdf into "
628667
"an IPython notebook. You can save the pdf "
629668
"with the `image.save_as` or you can "
630669
"embed an png, jpeg, or svg.")
631-
img = cls.get(figure_or_data, format, width, height)
670+
img = cls.get(figure_or_data, format, width, height, scale)
632671
from IPython.display import display, Image, SVG
633672
if format == 'svg':
634673
display(SVG(img))
@@ -637,14 +676,32 @@ def ishow(cls, figure_or_data, format='png', width=None, height=None):
637676

638677
@classmethod
639678
def save_as(cls, figure_or_data, filename, format=None, width=None,
640-
height=None):
641-
"""
642-
Save a image of the plot described by `figure` locally as `filename`.
679+
height=None, scale=None):
680+
"""Save a image of the plot described by `figure_or_data` locally as
681+
`filename`.
643682
644683
Valid image formats are 'png', 'svg', 'jpeg', and 'pdf'.
645684
The format is taken as the extension of the filename or as the
646685
supplied format.
647686
687+
positional arguments:
688+
- figure_or_data: The figure dict-like or data list-like object that
689+
describes a plotly figure.
690+
Same argument used in `py.plot`, `py.iplot`,
691+
see https://plot.ly/python for examples
692+
- filename: The filepath to save the image to
693+
- format: 'png', 'svg', 'jpeg', 'pdf'
694+
- width: output width
695+
- height: output height
696+
- scale: Increase the resolution of the image by `scale` amount
697+
Only valid for PNG and JPEG images.
698+
699+
example:
700+
```
701+
import plotly.plotly as py
702+
fig = {'data': [{'x': [1, 2, 3], 'y': [3, 1, 5], 'type': 'bar'}]}
703+
py.image.save_as(fig, 'my_image.png', scale=3)
704+
```
648705
"""
649706
# todo: format shadows built-in name
650707
(base, ext) = os.path.splitext(filename)
@@ -653,11 +710,9 @@ def save_as(cls, figure_or_data, filename, format=None, width=None,
653710
elif ext and not format:
654711
format = ext[1:]
655712
elif not ext and format:
656-
filename += '.'+format
657-
else:
658-
filename += '.'+format
713+
filename += '.' + format
659714

660-
img = cls.get(figure_or_data, format, width, height)
715+
img = cls.get(figure_or_data, format, width, height, scale)
661716

662717
f = open(filename, 'wb')
663718
f.write(img)

plotly/tests/test_core/test_file/test_file.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ def setUp(self):
1919
py.sign_in('PythonTest', '9v9f20pext')
2020

2121
def _random_filename(self):
22-
random_chars = [random.choice(string.ascii_uppercase)
23-
for _ in range(5)]
24-
unique_filename = 'Valid Folder'+''.join(random_chars)
22+
choice_chars = string.ascii_letters + string.digits
23+
random_chars = [random.choice(choice_chars) for _ in range(10)]
24+
unique_filename = 'Valid Folder ' + ''.join(random_chars)
2525
return unique_filename
2626

2727
def test_create_folder(self):

plotly/tests/test_core/test_image/__init__.py

Whitespace-only changes.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from __future__ import absolute_import
2+
3+
from unittest import TestCase
4+
import imghdr
5+
import tempfile
6+
import os
7+
import itertools
8+
9+
from plotly.plotly import plotly as py
10+
11+
12+
class TestImage(TestCase):
13+
def setUp(self):
14+
py.sign_in('PlotlyImageTest', '786r5mecv0',
15+
plotly_domain='https://plot.ly',
16+
plotly_api_domain='https://api.plot.ly')
17+
18+
self.data = [{'x': [1, 2, 3], 'y': [3, 1, 6]}]
19+
20+
21+
def _generate_image_get_returns_valid_image_test(image_format,
22+
width, height, scale):
23+
def test(self):
24+
image = py.image.get(self.data, image_format, width, height, scale)
25+
if image_format in ['png', 'jpeg']:
26+
assert imghdr.what('', image) == image_format
27+
28+
return test
29+
30+
31+
def _generate_image_save_as_saves_valid_image(image_format,
32+
width, height, scale):
33+
def _test(self):
34+
f, filename = tempfile.mkstemp('.{}'.format(image_format))
35+
py.image.save_as(self.data, filename, format=image_format,
36+
width=width, height=height, scale=scale)
37+
if image_format in ['png', 'jpeg']:
38+
assert imghdr.what(filename) == image_format
39+
else:
40+
assert os.path.getsize(filename) > 0
41+
42+
os.remove(filename)
43+
44+
return _test
45+
46+
kwargs = {
47+
'format': ['png', 'jpeg', 'pdf', 'svg'],
48+
'width': [None, 300],
49+
'height': [None, 300],
50+
'scale': [None, 5]
51+
}
52+
53+
for args in itertools.product(kwargs['format'], kwargs['width'],
54+
kwargs['height'], kwargs['scale']):
55+
for test_generator in [_generate_image_get_returns_valid_image_test,
56+
_generate_image_save_as_saves_valid_image]:
57+
58+
if args[0] in ['jpeg', 'pdf', 'svg'] and args[3] is not None:
59+
# Shouldn't need to skip these tests, the server should
60+
# be responding with a 400 level error since scale isn't supported,
61+
# but it doesn't yet, so just skip them
62+
continue
63+
64+
_test = test_generator(*args)
65+
arg_string = ', '.join([str(a) for a in args])
66+
test_name = test_generator.__name__.replace('_generate', 'test')
67+
test_name += '({})'.format(arg_string)
68+
setattr(TestImage, test_name, _test)

plotly/version.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
<<<<<<< HEAD
12
__version__ = '1.8.0'
3+
=======
4+
__version__ = '1.7.8'
5+
>>>>>>> master

0 commit comments

Comments
 (0)