Python bokeh.embed.file_html() Examples
The following are 5
code examples of bokeh.embed.file_html().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
bokeh.embed
, or try the search function
.
Example #1
Source File: bokeh.py From backtrader_plotting with GNU General Public License v3.0 | 8 votes |
def _output_plot_file(self, model, idx, filename=None, template="basic.html.j2"): if filename is None: tmpdir = tempfile.gettempdir() filename = os.path.join(tmpdir, f"bt_bokeh_plot_{idx}.html") env = Environment(loader=PackageLoader('backtrader_plotting.bokeh', 'templates')) templ = env.get_template(template) templ.globals['now'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") html = file_html(model, template=templ, resources=CDN, template_variables=dict( stylesheet=self._output_stylesheet(), show_headline=self.p.scheme.show_headline, ) ) with open(filename, 'w') as f: f.write(html) return filename
Example #2
Source File: chart.py From chartify with Apache License 2.0 | 6 votes |
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. """ driver = self._initialize_webdriver() # 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
Example #3
Source File: chart.py From chartify with Apache License 2.0 | 6 votes |
def _figure_to_svg(self): """ Convert the figure to an svg so that it can be saved to a file. https://github.com/bokeh/bokeh/blob/master/bokeh/io/export.py """ driver = self._initialize_webdriver() html = file_html(self.figure, resources=INLINE, title="") fp = tempfile.NamedTemporaryFile( 'w', prefix='chartify', suffix='.html', encoding='utf-8') fp.write(html) fp.flush() driver.get("file:///" + fp.name) svgs = driver.execute_script(_SVG_SCRIPT) fp.close() driver.quit() return svgs[0]
Example #4
Source File: renderer.py From holoviews with BSD 3-Clause "New" or "Revised" License | 5 votes |
def html(self, obj, fmt=None, css=None, resources='CDN', **kwargs): """ Renders plot or data structure and wraps the output in HTML. The comm argument defines whether the HTML output includes code to initialize a Comm, if the plot supplies one. """ plot, fmt = self._validate(obj, fmt) figdata, _ = self(plot, fmt, **kwargs) if isinstance(resources, basestring): resources = resources.lower() if css is None: css = self.css if isinstance(plot, Viewable): doc = Document() plot._render_model(doc) if resources == 'cdn': resources = CDN elif resources == 'inline': resources = INLINE return file_html(doc, resources) elif fmt in ['html', 'json']: return figdata else: if fmt == 'svg': figdata = figdata.encode("utf-8") elif fmt == 'pdf' and 'height' not in css: _, h = self.get_size(plot) css['height'] = '%dpx' % (h*self.dpi*1.15) if isinstance(css, dict): css = '; '.join("%s: %s" % (k, v) for k, v in css.items()) else: raise ValueError("CSS must be supplied as Python dictionary") b64 = base64.b64encode(figdata).decode("utf-8") (mime_type, tag) = MIME_TYPES[fmt], HTML_TAGS[fmt] src = HTML_TAGS['base64'].format(mime_type=mime_type, b64=b64) html = tag.format(src=src, mime_type=mime_type, css=css) return html
Example #5
Source File: save.py From panel with BSD 3-Clause "New" or "Revised" License | 4 votes |
def save_png(model, filename, template=None, template_variables=None): """ Saves a bokeh model to png Arguments --------- model: bokeh.model.Model Model to save to png filename: str Filename to save to template: template file, as used by bokeh.file_html. If None will use bokeh defaults template_variables: template_variables file dict, as used by bokeh.file_html """ from bokeh.io.webdriver import webdriver_control if not state.webdriver: state.webdriver = webdriver_control.create() webdriver = state.webdriver try: if template: def get_layout_html(obj, resources, width, height): return file_html( obj, resources, title="", template=template, template_variables=template_variables, suppress_callback_warning=True, _always_new=True ) old_layout_fn = bokeh.io.export.get_layout_html bokeh.io.export.get_layout_html = get_layout_html export_png(model, filename=filename, webdriver=webdriver) except Exception: raise finally: if template: bokeh.io.export.get_layout_html = old_layout_fn #--------------------------------------------------------------------- # Public API #---------------------------------------------------------------------