-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
static image support for other formats #43
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -318,7 +318,7 @@ def get_figure(file_owner_or_url, file_id=None, raw=False): | |
| (username, api_key) = _validation_key_logic() | ||
| headers = {'plotly-username': username, | ||
| 'plotly-apikey': api_key, | ||
| 'plotly-version': '2.0', | ||
| 'plotly-version': version.__version__, | ||
| 'plotly-platform': 'python'} | ||
| try: | ||
| test_if_int = int(file_id) | ||
|
|
@@ -513,53 +513,112 @@ class image: | |
| ''' | ||
|
|
||
| @staticmethod | ||
| def get(figure): | ||
| def get(figure_or_data, format='png', width=None, height=None): | ||
| """ Return a static image of the plot described by `figure`. | ||
|
|
||
| Valid formats: 'png', 'svg', 'jpeg', 'pdf' | ||
| """ | ||
| if isinstance(figure_or_data, dict): | ||
| figure = figure_or_data | ||
| elif isinstance(figure_or_data, list): | ||
| figure = {'data': figure_or_data} | ||
|
|
||
| if format not in ['png', 'svg', 'jpeg', 'pdf']: | ||
| raise exceptions.PlotlyError("Invalid format. " | ||
| "This version of your Plotly-Python " | ||
| "package currently only supports " | ||
| "png, svg, jpeg, and pdf. " | ||
| "Learn more about image exporting, " | ||
| "and the currently supported file " | ||
| "types here: " | ||
| "https://plot.ly/python/static-image-export/") | ||
|
|
||
| (username, api_key) = _validation_key_logic() | ||
| headers = {'plotly-username': username, | ||
| 'plotly-apikey': api_key, | ||
| 'plotly-version': '2.0', | ||
| 'plotly-version': version.__version__, | ||
| 'plotly-platform': 'python'} | ||
|
|
||
| payload = { | ||
| 'figure': figure, | ||
| 'format': format | ||
| } | ||
|
|
||
| if width is not None: | ||
| payload['width'] = width | ||
| if height is not None: | ||
| payload['height'] = height | ||
|
|
||
| url = tools.get_config_file()['plotly_domain'] + "/apigenimage/" | ||
| res = requests.post(url, | ||
| data=json.dumps(figure, | ||
| data=json.dumps(payload, | ||
| cls=utils._plotlyJSONEncoder), | ||
| headers=headers) | ||
|
|
||
| headers = res.headers | ||
|
|
||
| if res.status_code == 200: | ||
| return_data = json.loads(res.content) | ||
| return return_data['payload'] | ||
| if ('content-type' in headers and | ||
| headers['content-type'] in ['image/png', 'image/jpeg', | ||
| 'application/pdf', | ||
| 'image/svg+xml']): | ||
| return res.content | ||
|
|
||
| elif ('content-type' in headers and | ||
| 'json' in headers['content-type']): | ||
|
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. should we be more specific than 'json'? 'application/json'? |
||
| return_data = json.loads(res.content) | ||
| return return_data['image'] | ||
| else: | ||
| try: | ||
| return_data = json.loads(res.content) | ||
| if ('content-type' in headers and | ||
| 'json' in headers['content-type']): | ||
| return_data = json.loads(res.content) | ||
| else: | ||
| return_data = {'error': res.content} | ||
| except: | ||
| raise exceptions.PlotlyError("The response " | ||
| "from plotly could " | ||
| "not be translated.") | ||
| raise exceptions.PlotlyError(return_data['error']) | ||
|
|
||
|
|
||
| @classmethod | ||
| def ishow(cls, figure): | ||
| def ishow(cls, figure_or_data, format='png', width=None, height=None): | ||
| """ Display a static image of the plot described by `figure` | ||
| in an IPython Notebook. | ||
| """ | ||
| img = cls.get(figure) | ||
| from IPython.display import display, Image | ||
| display(Image(img)) | ||
| if format == 'pdf': | ||
| raise exceptions.PlotlyError("Aw, snap! " | ||
| "It's not currently possible to embed a pdf into " | ||
| "an IPython notebook. You can save the pdf " | ||
| "with the `image.save_as` or you can " | ||
| "embed an png, jpeg, or svg.") | ||
| img = cls.get(figure_or_data, format, width, height) | ||
| from IPython.display import display, Image, SVG | ||
| if format == 'svg': | ||
| display(SVG(img)) | ||
| else: | ||
| display(Image(img)) | ||
|
|
||
| @classmethod | ||
| def save_as(cls, figure, filename): | ||
| """ Save a static image of the plot described by `figure` | ||
| locally as `filename`. | ||
| def save_as(cls, figure_or_data, filename, format=None, width=None, height=None): | ||
| """ Save a static image of the plot described by `figure` locally as `filename`. | ||
| Valid image formats are 'png', 'svg', 'jpeg', and 'pdf'. | ||
| The format is taken as the extension of the filename or as the supplied format. | ||
| """ | ||
| img = cls.get(figure) | ||
| (base, ext) = os.path.splitext(filename) | ||
| if not ext: | ||
| if not ext and not format: | ||
| filename += '.png' | ||
| elif ext and not format: | ||
| format = ext[1:] | ||
| elif not ext and format: | ||
| filename += '.'+format | ||
| else: | ||
| filename += '.'+format | ||
|
|
||
| img = cls.get(figure_or_data, format, width, height) | ||
|
|
||
| f = open(filename, 'w') | ||
| img = base64.b64decode(img) | ||
| f.write(img) | ||
| f.close() | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| __version__ = '1.0.34' | ||
| __version__ = '1.1.0' | ||
|
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. bold. |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 may want to consider using requests definitions to check status codes:
https://github.com/kennethreitz/requests/blob/master/requests/status_codes.py
In this case, if we are only checking for
ok, so it would look like this:Maybe not here, but the codes (201) 'created' and (202) 'accepted' might be used elsewhere in our APIs and could be returned from our servers.