破折号-动态创建一个下拉菜单,选择多行,表导出? [英] dash plotly - create a dropdown menu dynamically, selection of multiple rows, table export?

查看:104
本文介绍了破折号-动态创建一个下拉菜单,选择多行,表导出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数据集:

I have a data set that looks like this:

cat_id  author  year    publisher   country value (dollars)
name1   kunga   1998    D and D Australia   10
name2   siba    2001    D and D UK  20
name3   siba    2001    D and D US  20
name3   shevara 2001    D and D UK  10
name3   dougherty   1992    D and D Australia   20
name4   ken 2011    S and K Australia   10

我想把它变成一张桌子.我拥有要设置的大部分代码:

I want to turn this into a table in plotly dash. I have most of the code that I want set up:

#!/usr/bin/env python
import dash
from dash.dependencies import Input, Output, State
import dash_table
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd

app = dash.Dash(__name__)
df = pd.read_excel('dash_test_doc.xlsx')


app.layout = html.Div([
    html.Div([
        dcc.Input(
            id='adding-rows-name',
            placeholder='Enter a column name...',
            value='',
            style={'padding': 10},
        ),
        html.Button('Add Column', id='adding-rows-button', n_clicks=0)
    ], style={'height': 50}),



     dash_table.DataTable(
         id='adding-rows-table',
         columns=[{"name": i, "id": i} for i in df.columns],
         data = df.to_dict('rows'),
         editable=True,
         filtering=True,
         sorting=True,
         sorting_type="multi",
         row_selectable="multi",
         row_deletable=True,
         selected_rows=[],
         pagination_mode="fe",
         style_cell_conditional=[
         {
             'if': {'row_index': 'odd'},
             'backgroundColor': 'rgb(230, 255, 230)'
         }
     ] + [
         {
             'if': {'column_id': c},
             'textAlign': 'left'
        } for c in ['Date', 'Region']
    ],
    style_header={
        'backgroundColor': 'white',
        'fontWeight': 'bold'
    }
    ),

    html.Button('Add Row', id='editing-rows-button', n_clicks=0),
    dcc.Graph(id='adding-rows-graph'),
])

@app.callback(
    Output('adding-rows-table', 'data'),
    [Input('editing-rows-button', 'n_clicks')],
    [State('adding-rows-table', 'data'),
     State('adding-rows-table', 'columns')])
def add_row(n_clicks, rows, columns):
    if n_clicks > 0:
        rows.append({c['id']: '' for c in columns})
    return rows


@app.callback(
    Output('adding-rows-table', 'columns'),
    [Input('adding-rows-button', 'n_clicks')],
    [State('adding-rows-name', 'value'),
     State('adding-rows-table', 'columns')])
def update_columns(n_clicks, value, existing_columns):
    if n_clicks > 0:
        existing_columns.append({
            'id': value, 'name': value,
            'editable_name': True, 'deletable': True
        })
    return existing_columns


@app.callback(
    Output('adding-rows-graph', 'figure'),
    [Input('adding-rows-table', 'data'),
     Input('adding-rows-table', 'columns')])
def display_output(rows, columns):
    return {
        'data': [{
            'type': 'heatmap',
            'z': [[row.get(c['id'], None) for c in columns] for row in rows],
            'x': [c['name'] for c in columns]
        }]
    }


if __name__ == '__main__':
    app.run_server(debug=True)

如果有人能帮助我,我将苦苦挣扎的只有三件事:

There is just three things that I’m struggling with if someone could help I would appreciate it:

  1. 我想为每个列创建一个下拉菜单,而无需预先定义下拉选项并允许多项选择.例如,使用此处的表,在发布者中,有两个选择(D和D,以及S和K);我希望这些内容自动显示为下拉选项,以进行过滤,而无需对其进行硬编码,因为例如,如果我编辑输入表并添加一行,并且添加了另一个发布者(例如A和D),我希望A和D自动添加到下拉选项? (因此,最终的想法是模仿excel,在这里我可以从下拉菜单中选择,例如,该列的下拉菜单中的D和D以及S和K,然后将基于此过滤条目.)

  1. I want to create a dropdown menu for each column, without pre-defining the dropdown options and allowing for multiple selection. for example using the table here, in publisher, there are two choices (D and D, and S and K); i want those to automatically appear as a dropdown option to filter by, without having to hardcode them in, because for example if I edit the input table and add a row, and there’s another publisher added (e.g. A and D), i want A and D to automatically be added to the dropdown options? (so ultimately, the idea would be to mimic excel, where I could select from a dropdown e.g. D and D and S and K from a dropdown for this column, and the entries would be filtered based on this).

是否可以选择多个行,并用一个动作将其删除?在这一刻,我可以删除单行.

is it possible to select multiple rows, and delete them with one action? at the minute, I can delete single rows.

是否可以导出表格?例如说我读了一个表,然后有人删除了一些列/行,我可以导出结果表吗?

Is it possible to export the table? e.g. say I read in a table, and then someone deletes some columns/rows, can I export the resulting table?

如果有人可以帮助我找出这些代码,我将不胜感激.附带说明,由于我才刚刚起步,如果有人对这个脚本有其他改进建议,我将不胜感激.

If anyone could help me figure out the code for these I’d appreciate it. As a a side note, since I’m just starting out, if anyone has any other suggestions for improvements to this script I’d appreciate it.

推荐答案

  1. 您可能想在这里做的是为每个下拉列表创建一个回调(或所有下拉列表的多输出回调),并输出到options道具.可能是由于表的更新而触发的.您可以使用虚拟下拉菜单选项来初始化布局中的下拉菜单,然后让回调函数从那里开始.

  1. What you would probably want to do here is create a callback for each dropdown (or a multi-output callback for all of them) that outputs to the options prop. Maybe triggered by updates to the table. You can initialize the dropdowns in the layout with a dummy dropdown option to start, and let the callback take it from there.

我不确定该表是否单独执行此操作,但是您可以使该表的回调做到这一点.因为每个唯一输出只能有一个回调,所以任何涉及更新表的data属性的操作都必须在seam回调下运行.您可能添加删除行"之类的按钮以Input的形式进行监听,并使用数据和选定的行作为State的形式.单击该按钮并触发回调时,您只需为表重建数据框,而在rows_selected值中不包含行,然后再次输出到表的数据.

I'm not sure whether the table does that on its own, but you could make your callback for the table do this. Because you can only have one callback per unique output, anything that involves updating the table's data prop would have to be run under the seam callback. You could probably add a button like "delete rows" to listen for as Input, and use the data and selected rows as State. When that button is clicked and fires the callback, you simply rebuild the dataframe for the table without rows in the rows_selected value, and then output to the table's data again.

好的.怎么导出?您可以通过导出数据"按钮设置回调,该回调可以根据需要导出.您可以使用该函数将数据转储到文件中,或将其打印到控制台,将其保存在数据库中,通过电子邮件发送,或者基本上您想要的任何东西.您实际上可以使用Python进行的任何操作.回调将需要输出,因此可能只需打开对话框或小吃店,让用户知道已接收并开始/完成了导出请求.

Sure. Export it how? You could set up a callback from a button, "export data" which could export it however you want. You could have the function dump the data to a file, or print it to the console, save it in a database, email it, or whatever you want basically. Anything you could do with Python really. The callback would need an output, so maybe just open a dialog or a snackbar letting the user know that the export request was received and started/completed.

这篇关于破折号-动态创建一个下拉菜单,选择多行,表导出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆