使列宽适合数据,并在DASH DATATABLE中

发布于 2025-02-03 17:40:10 字数 955 浏览 1 评论 0原文

我实际上正在使用破折号,但还没有找到任何方法来正确设置适合内容的列宽度。 因为当我将fill_width设置为false时,宽度适合数据,但不适合标题,并且在我的某些列中,标题比数据宽。

有什么正确的解决方法吗?预先感谢

NB:我的代码,即使我认为这没有什么帮助:

# Dashboard
html.Div(id="table", children=[
    dash_table.DataTable(
        style_header={
                'backgroundColor': colors['bg_board'],
                'color': colors['text_category'],
                'fontWeight': 'bold'
        },
        style_cell={
            'textAlign': 'center'
        },
        style_cell_conditional=[
            {
                'if': {'column_id': 'Maturity'},
                'backgroundColor': colors['bg_board'],
                'color': colors['text_category'],
            }
        ],
        style_table={
            'overflowY': 'auto',
            'overflowX': 'auto'
        },
        fixed_rows={'headers': True},
        fixed_columns={'headers': True},
        id='tbl'
    )
]),

“数据”是在回调中设置的,但这只是一个简单的数据帧。

I am using Dash actually but havent found any way to properly set column width adapted to the content.
Because when I set fill_width to False, the width fits to the datas but not to the header and on some of my columns the header is wider than the datas.

Any proper way to fix this ? Thank you in advance

NB : my code, even if I think it is not that helpful:

# Dashboard
html.Div(id="table", children=[
    dash_table.DataTable(
        style_header={
                'backgroundColor': colors['bg_board'],
                'color': colors['text_category'],
                'fontWeight': 'bold'
        },
        style_cell={
            'textAlign': 'center'
        },
        style_cell_conditional=[
            {
                'if': {'column_id': 'Maturity'},
                'backgroundColor': colors['bg_board'],
                'color': colors['text_category'],
            }
        ],
        style_table={
            'overflowY': 'auto',
            'overflowX': 'auto'
        },
        fixed_rows={'headers': True},
        fixed_columns={'headers': True},
        id='tbl'
    )
]),

'data' is set in a callback but it is just a simple dataframe.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何时共饮酒 2025-02-10 17:40:10

我正在使用以下代码来造型我的冲刺。希望这有所帮助:

dash_table.DataTable(
    id='table_data',
    columns=[{"name":i,"id":i} for i in df.columns],
    data=[],
    style_table={'overflow':'scroll','height':550},
    style_header={'backgroundColor':'#305D91','padding':'10px','color':'#FFFFFF'},
    style_cell={'textAlign':'center','minWidth': 95, 'maxWidth': 95, 'width': 95,'font_size': '12px','whiteSpace':'normal','height':'auto'},
    editable=True,              # allow editing of data inside all cells
    filter_action="native",     # allow filtering of data by user ('native') or not ('none')
    sort_action="native",       # enables data to be sorted per-column by user or not ('none')
    sort_mode="single",         # sort across 'multi' or 'single' columns
    column_selectable="multi",  # allow users to select 'multi' or 'single' columns
    row_selectable="multi",     # allow users to select 'multi' or 'single' rows
    row_deletable=True,         # choose if user can delete a row (True) or not (False)
    selected_columns=[],        # ids of columns that user selects
    selected_rows=[],           # indices of rows that user selects
    page_action="native")

I'm using below code to style my dashtable. Hope this help:

dash_table.DataTable(
    id='table_data',
    columns=[{"name":i,"id":i} for i in df.columns],
    data=[],
    style_table={'overflow':'scroll','height':550},
    style_header={'backgroundColor':'#305D91','padding':'10px','color':'#FFFFFF'},
    style_cell={'textAlign':'center','minWidth': 95, 'maxWidth': 95, 'width': 95,'font_size': '12px','whiteSpace':'normal','height':'auto'},
    editable=True,              # allow editing of data inside all cells
    filter_action="native",     # allow filtering of data by user ('native') or not ('none')
    sort_action="native",       # enables data to be sorted per-column by user or not ('none')
    sort_mode="single",         # sort across 'multi' or 'single' columns
    column_selectable="multi",  # allow users to select 'multi' or 'single' columns
    row_selectable="multi",     # allow users to select 'multi' or 'single' rows
    row_deletable=True,         # choose if user can delete a row (True) or not (False)
    selected_columns=[],        # ids of columns that user selects
    selected_rows=[],           # indices of rows that user selects
    page_action="native")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文