Skip to content

Commit a1d5143

Browse files
committed
Strip out save_image and save_html methods.
Image saving will wait for Orca, html saving will just use the existing offline.plot method
1 parent 72ee40c commit a1d5143

File tree

3 files changed

+0
-263
lines changed

3 files changed

+0
-263
lines changed

js/src/Figure.js

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,6 @@ var FigureModel = widgets.DOMWidgetModel.extend({
190190
_py2js_removeTraceProps: null,
191191

192192

193-
/**
194-
* @typedef {null|Object} Py2JsSvgRequestMsg
195-
* @property {String} request_id
196-
* Unique svg request identifier. This identifier is returned
197-
* along with the SVG image
198-
*/
199-
_py2js_svgRequest: null,
200-
201193
// JS -> Python messages
202194
// ---------------------
203195
// Messages are sent by the JavaScript side by assigning the
@@ -374,19 +366,6 @@ var FigureModel = widgets.DOMWidgetModel.extend({
374366
*/
375367
_js2py_pointsCallback: null,
376368

377-
// SVG image request message
378-
// -------------------------
379-
/**
380-
* @typedef {Object} Js2PySvgResponseMsg
381-
* @property {String} request_id
382-
* Unique identifier of the Py2JsSvgRequestMsg message that
383-
* triggered this response
384-
* @property {String} svg_uri
385-
* Response svg image encoded as a data uri string
386-
* e.g. 'data:image/svg+xml;base64,...'
387-
*/
388-
_js2py_svgResponse: null,
389-
390369
// Message tracking
391370
// ----------------
392371
/**
@@ -739,8 +718,6 @@ var FigureView = widgets.DOMWidgetView.extend({
739718
this.do_update, this);
740719
this.model.on("change:_py2js_animate",
741720
this.do_animate, this);
742-
this.model.on("change:_py2js_svgRequest",
743-
this.do_svgRequest, this);
744721

745722
// Get message ids
746723
// ---------------------
@@ -1343,30 +1320,6 @@ var FigureView = widgets.DOMWidgetView.extend({
13431320
}
13441321
},
13451322

1346-
/**
1347-
* Handle svg image request
1348-
*/
1349-
do_svgRequest: function() {
1350-
console.log("FigureView: do_svgRequest");
1351-
1352-
/** @type {Py2JsSvgRequestMsg} */
1353-
var msgData = this.model.get("_py2js_svgRequest");
1354-
var that = this;
1355-
if (msgData !== null) {
1356-
var req_id = msgData.request_id;
1357-
Plotly.toImage(this.el, {format: "svg"}).then(function (svg_uri) {
1358-
1359-
/** @type {Js2PySvgResponseMsg} */
1360-
var responseMsg = {
1361-
request_id: req_id,
1362-
svg_uri: svg_uri
1363-
};
1364-
that.model.set("_js2py_svgResponse", responseMsg);
1365-
that.touch();
1366-
});
1367-
}
1368-
},
1369-
13701323
/**
13711324
* Construct layout delta object and send layoutDelta message to the
13721325
* Python side

plotly/basedatatypes.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,39 +2064,6 @@ def to_plotly_json(self):
20642064
"""
20652065
return self.to_dict()
20662066

2067-
def save_html(self, filename, auto_open=False, responsive=False):
2068-
"""
2069-
Save figure to a standalone html file
2070-
2071-
Parameters
2072-
----------
2073-
filename : str
2074-
Full path and filename of the html file to be created
2075-
auto_open : bool
2076-
True if the the newly created HTML file be opened automatically,
2077-
False otherwise
2078-
responsive : bool
2079-
True if the figure in the resulting HTML should automatically
2080-
resize to fill the browser. If false then the width and height
2081-
will be fixed to the figure layout's width and heigh respectively
2082-
Returns
2083-
-------
2084-
None
2085-
"""
2086-
data = self.to_dict()
2087-
if responsive:
2088-
if 'height' in data['layout']:
2089-
data['layout'].pop('height')
2090-
if 'width' in data['layout']:
2091-
data['layout'].pop('width')
2092-
else:
2093-
# Assign width/height explicitly in case these were defaults
2094-
data['layout']['height'] = self.layout.height
2095-
data['layout']['width'] = self.layout.width
2096-
2097-
pyo.plot(data, filename=filename, show_link=False,
2098-
auto_open=auto_open)
2099-
21002067
# Static helpers
21012068
# --------------
21022069
@staticmethod

plotly/basewidget.py

Lines changed: 0 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ class BaseFigureWidget(BaseFigure, widgets.DOMWidget):
7575
**custom_serializers)
7676
_py2js_removeTraceProps = Dict(allow_none=True).tag(sync=True,
7777
**custom_serializers)
78-
_py2js_svgRequest = Dict(allow_none=True).tag(sync=True)
7978

8079
# ### JS -> Python message properties ###
8180
# These properties are used to receive messages from the frontend.
@@ -99,8 +98,6 @@ class BaseFigureWidget(BaseFigure, widgets.DOMWidget):
9998
**custom_serializers)
10099
_js2py_pointsCallback = Dict(allow_none=True).tag(sync=True,
101100
**custom_serializers)
102-
_js2py_svgResponse = Dict(allow_none=True).tag(sync=True,
103-
**custom_serializers)
104101

105102
# ### Message tracking properties ###
106103
# The _last_layout_edit_id and _last_trace_edit_id properties are used
@@ -168,186 +165,6 @@ def __init__(self,
168165
# views of this widget
169166
self._view_count = 0
170167

171-
# SVG
172-
# ---
173-
# Dict of pending SVG requests that have been sent to the frontend
174-
self._svg_requests = {}
175-
176-
def save_image(self, filename, image_type=None, scale_factor=2):
177-
"""
178-
Save figure to a static image file
179-
180-
Parameters
181-
----------
182-
filename : str
183-
Image output file name
184-
image_type : str
185-
Image file type. One of: 'svg', 'png', 'pdf', or 'ps'. If not
186-
set, file type is inferred from the filename extension
187-
scale_factor : number
188-
(For png image type) Factor by which to increase the number of
189-
pixels in each dimension. A scale factor of 1 will result in a
190-
image with pixel dimensions (layout.width, layout.height). A
191-
scale factor of 2 will result in an image with dimensions
192-
(2*layout.width, 2*layout.height),
193-
doubling image's DPI. (Default 2)
194-
"""
195-
196-
# Validate / infer image_type
197-
# ---------------------------
198-
supported_image_types = ['svg', 'png', 'pdf', 'ps']
199-
cairo_image_types = ['png', 'pdf', 'ps']
200-
supported_types_csv = ', '.join(supported_image_types)
201-
202-
# ### Image type found ###
203-
if not image_type:
204-
# Infer image type from extension
205-
_, extension = os.path.splitext(filename)
206-
207-
if not extension:
208-
raise ValueError('No image_type specified and file extension has no extension '
209-
'from which to infer an image type '
210-
'Supported image types are: {image_types}'
211-
.format(image_types=supported_types_csv))
212-
213-
image_type = extension[1:]
214-
215-
# ### Image type supported ###
216-
image_type = image_type.lower()
217-
if image_type not in supported_image_types:
218-
raise ValueError("Unsupported image type '{image_type}'\n"
219-
"Supported image types are: {image_types}"
220-
.format(image_type=image_type,
221-
image_types=supported_types_csv))
222-
223-
# ### Dependencies available for image type ###
224-
# Validate cairo dependency
225-
if image_type in cairo_image_types:
226-
# Check whether we have cairosvg available
227-
try:
228-
import_module('cairosvg')
229-
except ImportError:
230-
raise ImportError('Exporting to {image_type} requires cairosvg'
231-
.format(image_type=image_type))
232-
233-
# ### Validate scale_factor ###
234-
if not isinstance(scale_factor, numbers.Number) or scale_factor <= 0:
235-
raise ValueError(
236-
'scale_factor must be a positive number.\n'
237-
' Received: {scale_factor}'.format(
238-
scale_factor=scale_factor
239-
)
240-
)
241-
242-
# Build image request
243-
# -------------------
244-
# ### Create UID for request ###
245-
req_id = str(uuid.uuid1())
246-
247-
# ### Register request ###
248-
self._svg_requests[req_id] = {'filename': filename,
249-
'image_type': image_type,
250-
'scale_factor': scale_factor}
251-
252-
# ### Send request to the frontend###
253-
self._py2js_svgRequest = {'request_id': req_id}
254-
self._py2js_svgRequest = None
255-
256-
@observe('_js2py_svgResponse')
257-
def _handler_js2py_svgResponse(self, change):
258-
"""
259-
Handle _js2py_svgResponse message from the frontend
260-
261-
Parameters
262-
----------
263-
change : dict
264-
Message dict containing the following keys:
265-
- request_id: str
266-
The UID of the image request that triggered this message
267-
- svg_uri: str
268-
The SVG image encoded as a data uri
269-
(e.g. 'data:image/svg+xml,...')
270-
"""
271-
272-
# Receive message
273-
# ---------------
274-
response_data = change['new']
275-
self._js2py_svgResponse = None
276-
277-
if not response_data:
278-
return
279-
280-
# Extract message fields
281-
# ----------------------
282-
req_id = response_data['request_id']
283-
svg_uri = response_data['svg_uri']
284-
285-
# Save image
286-
# ----------
287-
self._do_save_image(req_id, svg_uri)
288-
289-
def _do_save_image(self, req_id, svg_uri):
290-
"""
291-
Save requested image to a file
292-
293-
Parameters
294-
----------
295-
req_id : str
296-
The UID of the image request that triggered this message
297-
svg_uri :
298-
The SVG image encoded as a data uri
299-
(e.g. 'data:image/svg+xml,...')
300-
"""
301-
302-
# Get request info
303-
# ----------------
304-
# Lack of request info means that widget has multiple frontend views
305-
# and that the request was already processed.
306-
req_info = self._svg_requests.pop(req_id, None)
307-
if not req_info:
308-
return
309-
filename = req_info['filename']
310-
image_type = req_info['image_type']
311-
scale_factor = req_info['scale_factor']
312-
313-
# Convert URI string to svg bytes
314-
# -------------------------------
315-
316-
# ### Remove URI prefix###
317-
if not svg_uri.startswith('data:image/svg+xml,'):
318-
raise ValueError('Invalid svg data URI: ' + svg_uri[:20])
319-
320-
svg = svg_uri.replace('data:image/svg+xml,', '')
321-
322-
# ### Unquote special characters ###
323-
# (e.g. '%3Csvg%20' -> '<svg ')
324-
svg_bytes = parse.unquote(svg).encode('utf-8')
325-
326-
# Save as svg
327-
# -----------
328-
# This requires no external dependencies
329-
if image_type == 'svg':
330-
with open(filename, 'wb') as f:
331-
f.write(svg_bytes)
332-
333-
# Save as cairo image type
334-
# ------------------------
335-
else:
336-
# We already made sure cairosvg is available in save_image
337-
cairosvg = import_module('cairosvg')
338-
339-
if image_type == 'png':
340-
cairosvg.svg2png(
341-
bytestring=svg_bytes,
342-
write_to=filename,
343-
scale=scale_factor)
344-
elif image_type == 'pdf':
345-
cairosvg.svg2pdf(
346-
bytestring=svg_bytes, write_to=filename)
347-
elif image_type == 'ps':
348-
cairosvg.svg2ps(
349-
bytestring=svg_bytes, write_to=filename)
350-
351168
# Python -> JavaScript Messages
352169
# -----------------------------
353170
def _send_relayout_msg(self, layout_data, source_view_id=None):

0 commit comments

Comments
 (0)