简体   繁体   English

Python Dash 如何显示数据表中的所有行?

[英]Python Dash how to show all rows in a data table?

I have made an interactive table using dash and plotly in python, currently the table only gives the first few rows, is there a way to make the table have pages or be scrollable so all rows are interactively viewable?我在 python 中使用dashplotly制作了一个交互式表格,目前该表格仅提供前几行,有没有办法使表格具有页面或可滚动,以便所有行都可以交互式查看?

Here is the code I'm using:这是我正在使用的代码:

data = X_interactive

app = JupyterDash(__name__)

app.layout = html.Div([
    dash_table.DataTable(
        id='datatable-interactivity',
        columns=[
            {"name": i, "id": i, "deletable": True, "selectable": True} for i in X_interactive.columns
        ],
        data=X_interactive.to_dict('records'),
        editable=True,
        filter_action="native",
        sort_action="native",
        sort_mode="multi",
        column_selectable="single",
        row_selectable="multi",
        row_deletable=True,
        selected_columns=[],
        selected_rows=[],
        page_action="native",
        page_current= 0,
        page_size= 10,
    ),
    html.Div(id='datatable-interactivity-container')
])

@app.callback(
    Output('datatable-interactivity', 'style_data_conditional'),
    Input('datatable-interactivity', 'selected_columns')
)
def update_styles(selected_columns):
    return [{
        'if': { 'column_id': i },
        'background_color': '#D2F3FF'
    } for i in selected_columns]

@app.callback(
    Output('datatable-interactivity-container', "children"),
    Input('datatable-interactivity', "derived_virtual_data"),
    Input('datatable-interactivity', "derived_virtual_selected_rows"))
def update_graphs(rows, derived_virtual_selected_rows):

    if derived_virtual_selected_rows is None:
        derived_virtual_selected_rows = []

    dff = X_interactive if rows is None else pd.DataFrame(rows)

    colors = ['#7FDBFF' if i in derived_virtual_selected_rows else '#0074D9'
              for i in range(len(dff))]

    return [
        dcc.Graph(
            id=column,
            figure={
                "data": [
                    {
                        "x": dff["Gene"],
                        "y": dff[column],
                        "type": "bar",
                        "marker": {"color": colors},
                    }
                ],
                "layout": {
                    "xaxis": {"automargin": True},
                    "yaxis": {
                        "automargin": True,
                        "title": {"text": column}
                    },
                    "height": 250,
                    "margin": {"t": 10, "l": 10, "r": 10},
                },
            },
        )
        # check if column exists - user may have deleted it
        # If `column.deletable=False`, then you don't
        # need to do this check.
        for column in ["col1", "col2", "col3"] if column in dff
    ]

    
app.run_server(mode='inline',port=8056)

From trying to find similar questions I've been trying to get pages with pagination_settings but I'm new to python and not sure how to properly get it into my current code without losing how the table currently is (interactive with being able to filter columns).从试图找到类似的问题,我一直在尝试使用pagination_settings获取页面,但我是 python 的新手,不知道如何正确地将其放入我当前的代码中而不丢失表当前的状态(与能够过滤列交互)。

Just trying to add pagination_settings within html.Div() gives me an error:只是试图在html.Div()中添加pagination_settings给我一个错误:

TypeError: The `dash_table.DataTable` component (version 4.11.2) with the ID "datatable-interactivity" received an unexpected keyword argument: `pagination_settings

pagination-settings isn't a valid arguement as seen by the error below: pagination-settings不是一个有效的论点,如下面的错误所示:

TypeError: The `dash_table.DataTable` component (version 4.10.1) with the ID 

"table" received an unexpected keyword argument: `pagination_settings`
Allowed arguments: active_cell, cell_selectable, column_selectable, columns, css, data, data_previous, data_timestamp, derived_filter_query_structure, derived_viewport_data, derived_viewport_indices, derived_viewport_row_ids, derived_viewport_selected_columns, derived_viewport_selected_row_ids, derived_viewport_selected_rows, derived_virtual_data, derived_virtual_indices, derived_virtual_row_ids, derived_virtual_selected_row_ids, derived_virtual_selected_rows, dropdown, dropdown_conditional, dropdown_data, editable, end_cell, export_columns, export_format, export_headers, fill_width, filter_action, filter_query, fixed_columns, fixed_rows, hidden_columns, id, include_headers_on_copy_paste, is_focused, loading_state, locale_format, markdown_options, merge_duplicate_headers, page_action, page_count, page_current, page_size, persisted_props, persistence, persistence_type, row_deletable, row_selectable, selected_cells, selected_columns, selected_row_ids, selected_rows, sort_action, sort_as_null, sort_by, sort_mode, start_cell, style_as_list_view, style_cell, style_cell_conditional, style_data, style_data_conditional, style_filter, style_filter_conditional, style_header, style_header_conditional, style_table, tooltip, tooltip_conditional, tooltip_data, tooltip_delay, tooltip_duration, virtualization

And by looking at the official documentation to get pagination in a datatable并通过查看官方文档来获取数据表中的分页

Code from the documentation:文档中的代码:

import dash
from dash.dependencies import Input, Output
import dash_table
import pandas as pd


df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv')

df[' index'] = range(1, len(df) + 1)

app = dash.Dash(__name__)

PAGE_SIZE = 5

app.layout = dash_table.DataTable(
    id='datatable-paging',
    columns=[
        {"name": i, "id": i} for i in sorted(df.columns)
    ],
    page_current=0,
    page_size=PAGE_SIZE,
    page_action='custom'
)
if __name__ == '__main__':
    app.run_server(debug=True)

You can see that the required and accepted arguments are page_current , page_size , page_action , page_count (from the error).您可以看到所需和接受的 arguments 是page_currentpage_sizepage_actionpage_count (来自错误)。

In your code, I think adding the page_count argument should end up being the fix.在您的代码中,我认为添加page_count参数最终应该是解决方法。

If you want to display all dataframe rows in your datatable set this option:如果要在数据表中显示所有 dataframe 行,请设置此选项:

pd.set_option('display.max_rows',None)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM