【发布时间】:2019-08-25 20:36:36
【问题描述】:
我正在创建可移植的 HTML 文件,这些文件使用 python bokeh 可视化一些数据。我使用一些按钮(切换小部件)来显示/隐藏绘图元素组(线、圆等)。 问题是按钮只有 4 种颜色可用,而且很难区分活动按钮和非活动按钮。
我决定动态编辑散景 css 类或添加我自己的类。 Toggle 类可以选择使用 bokeh 内置 css 类:
button = Toggle(label="Button 1", button_type="primary", css_classes=['some_class'])
但我找不到将 css 类注入输出 HTML 的方法。
有一个 bokeh.resources 类具有一些有趣的属性,例如 js_raw, css_raw 但我无法修改它们:
from bokeh.resources import Resources
my_css_class = """
.some_class {
background-color: black;
color: white;
margin: 20px;
padding: 20px;
}
"""
res = Resources(js_raw="<style>{}</style".format(my_css_class))
#or
res = Resources(css_raw=my_css_class)
#or
setattr(res, 'css_raw', my_css_class)
没用:( 返回:“AttributeError:无法设置属性”
完整代码:
from bokeh.models.widgets import Toggle
from bokeh.io import save
from bokeh.plotting import figure, output_file
from bokeh.models import CustomJS
from bokeh.layouts import Column
from bokeh.resources import Resources
output_file('a.html')
N_lines = 10
x = range(3)
fig = figure()
# adding custom CSS class - if the button does not appear - it works:)
my_css_class = """
.some_class {
display:none;
}
"""
res = Resources(mode='inline', version=None, root_dir=None, minified=False, log_level='info', root_url=None, path_versioner=None, components=None)
# !!!!!!!!! line below does not add the class, toggle button still appears
res.css_raw.append(my_css_class)
# !!!!!!!!!!!!!! line below produces the error:
setattr(res, 'css_raw', my_css_class)
# ---------------------------------------------------------------------------
# AttributeError Traceback (most recent call last)
# <ipython-input-119-f642d1ca6dc6> in <module>
# 24
# 25 # !!!!!!!!!!!!!! line below produces the error:
# ---> 26 setattr(res, 'css_raw', my_css_class)
# 27 # ---------------------------------------------------------------------------
# 28 # AttributeError Traceback (most recent call last)
#
# AttributeError: can't set attribute
callback_code = """
active = cb_obj.active;
"""
args = []
for i in range(N_lines):
glyph = fig.line(
x=x,
y=[random() for j in x],
color=choice(Category20_20),
legend=str(i)
)
args += [('line{}'.format(str(i)), glyph)]
callback_code += "line{}.visible = active;".format(str(i))
toggle = Toggle(label="Hide All", button_type="success", active=True, css_classes=['some_class'])
toggle.callback = CustomJS(
args={key:value for key,value in args},
code=callback_code
)
fig.legend.click_policy='hide'
children = [fig, toggle]
col = Column(children=children)
save(col, resources=res)
有什么建议吗?
【问题讨论】:
-
res = Resources()res.js_raw.append('text')res.css_raw.append('text')save(..., resources=res)? -
您可以在保存后使用自己的