FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Forward port get_svg_data by martinRenou · Pull Request #1685 · bqplot/bqplot · GitHub

/ bqplot Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .ts  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
20 changes: 20 additions & 0 deletions bqplot/figure.py
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
Expand Up @@ -160,6 +160,7 @@ def __init__(self, **kwargs):
super(Figure, self).__init__(**kwargs)

self._upload_png_callback = None
self._upload_svg_callback = None
self.on_msg(self._handle_custom_msgs)

@default('scale_x')
Expand Down Expand Up @@ -210,6 +211,20 @@ def get_png_data(self, callback, scale=None):
self._upload_png_callback = callback
self.send({'type': 'upload_png', 'scale': scale})

def get_svg_data(self, callback):
'''
Gets the Figure as an SVG memory view (utf-8 encoded bytes).

Parameters
----------
callback: callable
Called with the SVG data (utf-8 encoded bytes) as the only positional argument.
'''
if self._upload_svg_callback:
raise Exception('get_svg_data already in progress')
self._upload_svg_callback = callback
self.send({'type': 'upload_svg'})

@validate('min_aspect_ratio', 'max_aspect_ratio')
def _validate_aspect_ratio(self, proposal):
value = proposal['value']
Expand All @@ -227,6 +242,11 @@ def _handle_custom_msgs(self, _, content, buffers=None):
self._upload_png_callback(buffers[0])
finally:
self._upload_png_callback = None
elif content.get('event') == 'upload_svg':
try:
self._upload_svg_callback(buffers[0])
finally:
self._upload_svg_callback = None

_view_name = Unicode('Figure').tag(sync=True)
_model_name = Unicode('FigureModel').tag(sync=True)
Expand Down
17 changes: 17 additions & 0 deletions js/src/Figure.ts
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
Expand Up @@ -454,6 +454,8 @@ export class Figure extends DOMWidgetView {
this.model.on('save_png', this.save_png, this);
this.model.on('save_svg', this.save_svg, this);
this.model.on('upload_png', this.upload_png, this);
this.model.on('upload_svg', this.upload_svg, this);
console.log('RENDER FFS');

this.webGLCanvas.width = this.plotareaWidth;
this.webGLCanvas.height = this.plotareaHeight;
Expand Down Expand Up @@ -1396,6 +1398,21 @@ export class Figure extends DOMWidgetView {
});
}

async upload_svg(model) {
const svg_string = await this.get_svg();
const svg_blob = new Blob([svg_string], {
type: 'image/svg+xml;charset=utf-8',
});
const svg_buffer = await svg_blob.arrayBuffer();
model.send(
{
event: 'upload_svg',
},
null,
[svg_buffer]
);
}

save_png(filename, scale) {
// Render a SVG data into a canvas and download as PNG.

Expand Down
2 changes: 2 additions & 0 deletions js/src/FigureModel.ts
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
Expand Up @@ -72,6 +72,8 @@ export class FigureModel extends widgets.DOMWidgetModel {
this.trigger('save_svg', msg.filename);
} else if (msg.type === 'upload_png') {
this.trigger('upload_png', this, msg.scale);
} else if (msg.type === 'upload_svg') {
this.trigger('upload_svg', this, msg.scale);
}
}

Expand Down
Loading

Back | FazBrowse Home | New Git URL