首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Dash向Bootstrap Table中的单元格添加工具提示

使用Dash向Bootstrap Table中的单元格添加工具提示
EN

Stack Overflow用户
提问于 2019-01-21 16:44:52
回答 1查看 2.7K关注 0票数 2

我正在使用Dash构建一个应用程序,该应用程序的一部分是显示数据表。我使用dash-bootstrap-components中的Table()函数在Dash中将表从pandas数据帧转换为Bootstrap table。

我的桌子准备好了

代码语言:javascript
复制
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,然后附加一个工具提示

代码语言:javascript
复制
tooltip = dbc.Tooltip("Tooltip text", target = id)

然后我们把它们放在一起。

代码语言:javascript
复制
app = dash.Dash(_name_)
app.layout = html.Div(
                 children = [data_table, tooltip]
             )

然而,这似乎不起作用,我正在为如何在理论上实现它而苦苦挣扎,也找不到太多的帮助。

更新:

下面是我正在尝试做的一些示例代码。它会给出一个错误,如果你删除了"REMOVE TO WORK“这段注释,代码将会工作。

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-27 17:22:32

我发现了您的问题-由于某些原因,dbc.Tooltip元素不能很好地处理ID以数字开头的元素。

要克服这一点,只需在元素in和工具提示目标中更改:

代码语言:javascript
复制
str(i)+"_"+col

至:

代码语言:javascript
复制
col+"_"+str(i)

或者,您可以添加字母前缀:

代码语言:javascript
复制
"p_"+str(i)+"_"+col
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54286143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档