templates/index.html
文件添加到Directory-style Bokeh App中。 模板应该是Jinja2 template。 在<head>
中需要定义两个替换项:
{{ bokeh_css }}
{{ bokeh_js }}
<body>
中还需要定义两个替换项:
{{ plot_div }}
{{ plot_script }}
plot_script
的位置。 除此之外,您可以应用任何所需的HTML和CSS。 您可以在此处查看具体示例:
https://github.com/bokeh/bokeh/blob/master/examples/app/crossfilter
一个简化的改变页面背景的模板可能如下所示:<!DOCTYPE html>
<html lang="en">
<head>
<style>
body { background: #2F2F2F; }
</style>
<meta charset="utf-8">
{{ bokeh_css }}
{{ bokeh_js }}
</head>
<body>
{{ plot_div|indent(8) }}
{{ plot_script|indent(8) }}
</body>
</html>
对我来说,更改.bk-root样式起作用了:
from bokeh.resources import Resources
from bokeh.io.state import curstate
from bokeh.io import curdoc, output_file, save
from bokeh.util.browser import view
from bokeh.models.widgets import Panel, Tabs
from bokeh.plotting import figure
class MyResources(Resources):
@property
def css_raw(self):
return super().css_raw + [
""".bk-root {
background-color: #000000;
border-color: #000000;
}
"""
]
f = figure(height=200, width=200)
f.line([1,2,3], [1,2,3])
tabs = Tabs( tabs=[ Panel( child=f, title="TabTitle" ) ], height=500 )
output_file("BlackBG.html")
curstate().file['resources'] = MyResources(mode='cdn')
save(tabs)
view("./BlackBG.html")
row
或column
在文档中显示多个图形,则可以通过以下方式设置background
属性来解决问题:curdoc().add_root(row(fig1, fig2, background="beige"))
。来自Bokeh文档:
The background fill style is controlled by the background_fill_color and background_fill_alpha properties of the Plot object:
from bokeh.plotting import figure, output_file, show output_file("background.html") p = figure(plot_width=400, plot_height=400) p.background_fill_color = "beige" p.background_fill_alpha = 0.5 p.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=10) show(p)