简体   繁体   English

Plotly-Dash:- 文件上传后在 plotly dash 中进行多列过滤

[英]Plotly-Dash:- Multiple column filtering in plotly dash after file upload

I would like to implement the multiple column filter in a data frame after uploading a excel or csv file and save the resultant data-frame.我想在上传 excel 或 csv 文件并保存结果数据框后在数据框中实现多列过滤器。 Below is the code.下面是代码。 I have done the code for uploading the file and able to display it.我已经完成了上传文件的代码并能够显示它。 I need to get filtering options and save the result data.我需要获取过滤选项并保存结果数据。 Below is my current code下面是我当前的代码

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

#HTML Layout

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)


server = app.server
app.layout = html.Div([           
    html.Div([
   html.Center(
       dcc.Upload(
        id='upload-data',
        children=html.Div([
            'Drag and Drop or ',
            html.A('Select File')
        ]),
        style={
            'width': '20%',
            'height': '32px',
            'lineHeight': '32px',
            'borderWidth': '1px',
            'borderStyle': 'dashed',
            'borderRadius': '5px',
            'textAlign': 'center',
            'margin': '10px'
        },
        # Allow multiple files to be uploaded
        multiple=True
    )),
    ]),
    html.Div(id='output-data-upload'),
    html.Br(),
    
    html.Br(),
    html.Div([
        html.Center(html.H6(id='my-output'))
        #html.Div(id='my-output'),
    ]),
   
    ]),
    
   
    
])

    
# Function for reading the data

def parse_data(contents, filename):
    content_type, content_string = contents.split(',')

    decoded = base64.b64decode(content_string)
    try:
        if 'csv' in filename:
            # Assume that the user uploaded a CSV or TXT file
            df = pd.read_csv(
                io.StringIO(decoded.decode('utf-8')))
        elif 'xls' in filename:
            # Assume that the user uploaded an excel file
            df = pd.read_excel(io.BytesIO(decoded))
        elif 'txt' or 'tsv' in filename:
            # Assume that the user upl, delimiter = r'\s+'oaded an excel file
            df = pd.read_csv(
                io.StringIO(decoded.decode('utf-8')), delimiter = r'\s+')
    except Exception as e:
        print(e)
        return html.Div([
            'There was an error processing this file.'
        ])

    return df



            
#Call backs 

def generate_table(df_final):
    return dash_table.DataTable(data=df_final.to_dict('rows'),columns=[{'name': i, 'id': i} for i in df_final.columns],editable=True,
                                    virtualization=True,
                                    fixed_rows={'headers': True},
                                    page_current=0,
                                    page_size=5,
                                    style_table={ 'height':'350px','overflowY': 'auto'},
                                    style_cell_conditional=[{'if': {'column_id': c},'textAlign': 'left'} 
                                                           for c in ['Date', 'Region']],style_data_conditional=[{
                                       'if': {'row_index': 'odd'},'backgroundColor': 'rgb(248, 248, 248)'}],
                                    style_header={'backgroundColor': 'rgb(230, 230, 230)','fontFamily':'sans-serif',
                                                  'fontWeight': 'bold',"fontSize":'13px'},
                                    
                                    style_cell={'minWidth': 95, 'maxWidth': 95, 'width': 95}
                                   )

@app.callback(Output('output-data-upload','children'),
              [Input('upload-data', 'contents')],
              [State('upload-data', 'filename')]
    
)


#function for displaying the preview of the input file.

def display_table(contents,filename):
    if contents:
        contents = contents[0]
        filename= filename[0]
        df = parse_data(contents,filename)
        return html.Div([
            html.Center(html.H5('Preview')),
            html.Center(
                html.Div([
               
                    html.Div([
                        generate_table(df_final_1)
                    ])
                    ],style={'width':'85%'}
                )
        )])
    
@app.callback(Output('output-confirm','children'),
              [Input('submit-filter', 'n_clicks')]
    
)

def filter_button(n_clicks):
    if n_clicks:
        return 'Successful'
   
    

#Calling the server    

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

There are many questions lurking under the surface here, and far too much to cover in detail in a single answer.这里隐藏着许多问题,在一个答案中无法详细介绍。 Very generally, what you'll want to do is using the values from the CSV to update something like a dropdown, or multi-dropdown, or other components to allow the user to make the filtering selections.通常,您需要做的是使用 CSV 中的值来更新下拉列表、多下拉列表或其他组件等内容,以允许用户进行过滤选择。 That component (or those components) would then trigger a callback which also includes the data from the CSV, performs the filtering, and outputs it either to the Dash UI, or to whatever form of storage you're using (ex. local file system, SQL database, etc.).然后该组件(或那些组件)将触发一个回调,该回调还包括来自 CSV 的数据,执行过滤,并将其输出到 Dash UI 或您使用的任何形式的存储(例如本地文件系统) 、SQL 数据库等)。

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

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