def __call__(self, obj, fmt=None, doc=None): """ Render the supplied HoloViews component using the appropriate backend. The output is not a file format but a suitable, in-memory byte stream together with any suitable metadata. """ plot, fmt = self._validate(obj, fmt) info = {'file-ext': fmt, 'mime_type': MIME_TYPES[fmt]} if self.mode == 'server': return self.server_doc(plot, doc), info elif isinstance(plot, tuple(self.widgets.values())): return plot(), info elif fmt == 'png': if bokeh_version < '0.12.6': raise RuntimeError( 'Bokeh png export only supported by versions >=0.12.6.') from bokeh.io import _get_screenshot_as_png if bokeh_version > '0.12.6': img = _get_screenshot_as_png(plot.state, None) else: img = _get_screenshot_as_png(plot.state) imgByteArr = BytesIO() img.save(imgByteArr, format='PNG') return imgByteArr.getvalue(), info elif fmt == 'html': html = self.figure_data(plot, doc=doc) html = "<div style='display: table; margin: 0 auto;'>%s</div>" % html return self._apply_post_render_hooks(html, obj, fmt), info elif fmt == 'json': return self.diff(plot), info
def test__get_screenshot_as_png_large_plot(): layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=800, plot_width=800, toolbar_location=None, outline_line_color=None, background_fill_color=None, border_fill_color=None) driver = webdriver.PhantomJS(service_log_path=os.path.devnull) assert driver.get_window_size() == {'width': 400, 'height': 300} io._get_screenshot_as_png(layout, driver=driver) # LC: Although the window size doesn't match the plot dimensions (unclear # why), the window resize allows for the whole plot to be captured assert driver.get_window_size() == {'width': 1366, 'height': 768} # Have to manually clean up the driver session driver.quit()
def test__get_screenshot_as_png(): layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=20, plot_width=20, toolbar_location=None, outline_line_color=None, background_fill_color=None, border_fill_color=None) png = io._get_screenshot_as_png(layout) assert png.size == (20, 20) # a 20x20px image of transparent pixels assert png.tobytes() == ("\x00"*1600).encode()
def test__get_screenshot_as_png_with_driver(): layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=20, plot_width=20, toolbar_location=None, outline_line_color=None, background_fill_color=None, border_fill_color=None) driver = webdriver.PhantomJS(service_log_path=os.path.devnull) png = io._get_screenshot_as_png(layout, driver=driver) # Have to manually clean up the driver session driver.quit() assert png.size == (20, 20) # a 20x20px image of transparent pixels assert png.tobytes() == ("\x00"*1600).encode()
def file_contents(self, args, doc): if args.width is not None or args.height is not None: layout = doc.roots if len(layout) != 1 or not isinstance(layout[0], Plot): warnings.warn( "Export called with height or width kwargs on a non-single Plot layout. The size values will be ignored." ) else: plot = layout[0] plot.plot_height = args.height or plot.plot_height plot.plot_width = args.width or plot.plot_width image = _get_screenshot_as_png(doc, driver=self.driver) buf = io.BytesIO() image.save(buf, "png") buf.seek(0) return buf.read()
def file_contents(self, args, doc): image = _get_screenshot_as_png(doc, self.driver) buf = io.BytesIO() image.save(buf, "png") buf.seek(0) return buf.read()