def _initialize_webdriver(self): """Initialize headless chrome browser""" options = Options() options.add_argument("window-size={width},{height}".format( width=self.style.plot_width, height=self.style.plot_height)) options.add_argument("start-maximized") options.add_argument("disable-infobars") options.add_argument("disable-gpu") options.add_argument('no-sandbox') # Required for use in docker. options.add_argument("--disable-extensions") options.add_argument('--headless') options.add_argument('--hide-scrollbars') driver = webdriver.Chrome(options=options) return driver
def _figure_to_png(self): """Convert figure object to PNG Bokeh can only save figure objects as html. To convert to PNG the HTML file is opened in a headless browser. """ # Initialize headless browser options options = Options() options.add_argument("window-size={width},{height}".format( width=self.style.plot_width, height=self.style.plot_height)) options.add_argument("start-maximized") options.add_argument("disable-infobars") options.add_argument("disable-gpu") options.add_argument('no-sandbox') # Required for use in docker. options.add_argument("--disable-extensions") options.add_argument('--headless') options.add_argument('--hide-scrollbars') driver = webdriver.Chrome(options=options) # Save figure as HTML html = file_html(self.figure, resources=INLINE, title="") fp = tempfile.NamedTemporaryFile('w', prefix='chartify', suffix='.html', encoding='utf-8') fp.write(html) fp.flush() # Open html file in the browser. driver.get("file:///" + fp.name) driver.execute_script("document.body.style.margin = '0px';") png = driver.get_screenshot_as_png() driver.quit() fp.close() # Resize image if necessary. image = Image.open(BytesIO(png)) target_dimensions = (self.style.plot_width, self.style.plot_height) if image.size != target_dimensions: image = image.resize(target_dimensions, resample=Image.LANCZOS) return image