本文整理汇总了Python中bokeh.embed.file_html方法的典型用法代码示例。如果您正苦于以下问题:Python embed.file_html方法的具体用法?Python embed.file_html怎么用?Python embed.file_html使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bokeh.embed
的用法示例。
在下文中一共展示了embed.file_html方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _output_plot_file
# 需要导入模块: from bokeh import embed [as 别名]
# 或者: from bokeh.embed import file_html [as 别名]
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
示例2: _figure_to_png
# 需要导入模块: from bokeh import embed [as 别名]
# 或者: from bokeh.embed import file_html [as 别名]
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
示例3: _figure_to_svg
# 需要导入模块: from bokeh import embed [as 别名]
# 或者: from bokeh.embed import file_html [as 别名]
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]
示例4: html
# 需要导入模块: from bokeh import embed [as 别名]
# 或者: from bokeh.embed import file_html [as 别名]
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
示例5: save_png
# 需要导入模块: from bokeh import embed [as 别名]
# 或者: from bokeh.embed import file_html [as 别名]
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
#---------------------------------------------------------------------