我正在使用Dash构建一个应用程序,该应用程序的一部分是显示数据表。我使用dash-bootstrap-components中的Table()函数在Dash中将表从pandas数据帧转换为Bootstrap table。
我的桌子准备好了
import dash-bootstrap-components as dbc
import dash_html_components as html
import dash
data_table = dbc.Table(# code to construct table)我想使用dbc.Tooltip向我的data_table中的项目(html.td()元素)添加工具提示。我的第一个想法是为表中的每个元素分配一个与其位置相对应的id,然后附加一个工具提示
tooltip = dbc.Tooltip("Tooltip text", target = id)然后我们把它们放在一起。
app = dash.Dash(_name_)
app.layout = html.Div(
children = [data_table, tooltip]
)然而,这似乎不起作用,我正在为如何在理论上实现它而苦苦挣扎,也找不到太多的帮助。
更新:
下面是我正在尝试做的一些示例代码。它会给出一个错误,如果你删除了"REMOVE TO WORK“这段注释,代码将会工作。
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
import pandas as pd
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
data = {
"Name" : ["Tom", "Jack", "Jill", "Jane"],
"Age" : [21, 30, 45, 80]
}
fixt_df = pd.DataFrame.from_dict(data)
#Setup table
table = dbc.Table(
# Header
[html.Thead([html.Tr([html.Th(col) for col in fixt_df.columns])])] +
# Body
[html.Tbody([html.Tr([html.Td(html.A(fixt_df.iloc[i][col], id = ""+str(i)+"_" + col))
for col in fixt_df.columns])
for i in range(len(fixt_df))])]
,
hover = True,
className = "table-responsive table-hover"
)
items = [table]
# REMOVE TO WORK
for col in fixt_df.columns:
for i in range(len(fixt_df)):
items.extend([dbc.Tooltip("test", target = str(i)+"_"+col)])
# REMOVE TO WORK
app.layout = html.Div(
children = [
#header
html.Div(
html.H1("Example")
),
#table
html.Div(
items
)
]
)
if __name__ == '__main__':
app.run_server(debug=True)发布于 2019-01-27 17:22:32
我发现了您的问题-由于某些原因,dbc.Tooltip元素不能很好地处理ID以数字开头的元素。
要克服这一点,只需在元素in和工具提示目标中更改:
str(i)+"_"+col至:
col+"_"+str(i)或者,您可以添加字母前缀:
"p_"+str(i)+"_"+colhttps://stackoverflow.com/questions/54286143
复制相似问题