User interface 在Dash中的其他组件中使用上载数据时出现的问题

User interface 在Dash中的其他组件中使用上载数据时出现的问题,user-interface,flask,plotly,plotly-dash,User Interface,Flask,Plotly,Plotly Dash,在用Dash编写程序时,我遇到了一些问题。在使用上传组件时,我很难在其他组件上正确使用这些数据。 我的目标是使用上传的数据(CSV文件)向两个相同的下拉组件添加选项,这些组件是导入文件的列的名称。 随后将使用下拉菜单上的选定值作为图形的轴来生成图形 任何帮助都将不胜感激 import base64 import datetime import io import dash import dash_table import dash_core_components as dcc import d

在用Dash编写程序时,我遇到了一些问题。在使用上传组件时,我很难在其他组件上正确使用这些数据。 我的目标是使用上传的数据(CSV文件)向两个相同的下拉组件添加选项,这些组件是导入文件的列的名称。 随后将使用下拉菜单上的选定值作为图形的轴来生成图形

任何帮助都将不胜感激

import base64
import datetime
import io

import dash
import dash_table
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import plotly.express as px
import pandas as pd

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
df = pd.DataFrame()
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div([
    html.Div(children='this is an attempt to do stuff right'),
    dcc.Dropdown(id='Drop1'),
    dcc.Dropdown(id='Drop2'),
    dcc.Dropdown(id='graphtype', options=[
        {'label': 'Bar', 'value': 'Bar'},
        {'label': 'Scatter', 'value': 'Scatter'},
        {'label': 'Histogram', 'value': 'Hist'}
    ]),
    dcc.Upload(
        id='upload-data',
        children=html.Div([
            'Drag and Drop or ',
            html.A('Select Files')
        ]),
            style={
            'width': '100%',
            'height': '60px',
            'lineHeight': '60px',
            'borderWidth': '1px',
            'borderStyle': 'dashed',
            'borderRadius': '5px',
            'textAlign': 'center',
            'margin': '10px'
        },
        # Allow multiple files to be uploaded
        multiple=True
    ),
    html.Div(id='output-data-upload'),
    dcc.Graph(id='output-graph')

]
)


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

    decoded = base64.b64decode(content_string)
    try:
        if 'csv' in filename:
            # Assume that the user uploaded a CSV 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))
    except Exception as e:
        print(e)
        return html.Div([
            'There was an error processing this file.'
        ])

    return html.Div([
        html.H5(filename),
        html.H6(datetime.datetime.fromtimestamp(date)),

        dash_table.DataTable(
            data=df.to_dict('records'),
            columns=[{'name': i, 'id': i} for i in df.columns]
        ),

        html.Hr(),  # horizontal line

        # For debugging, display the raw contents provided by the web browser
        html.Div('Raw Content'),
        html.Pre(contents[0:200] + '...', style={
            'whiteSpace': 'pre-wrap',
            'wordBreak': 'break-all'
        })

    ])


@app.callback(Output('output-data-upload', 'children'),
              [Input('upload-data', 'contents')],
              [State('upload-data', 'filename'),
               State('upload-data', 'last_modified')])
def update_output(list_of_contents, list_of_names, list_of_dates):
    if list_of_contents is not None:
        children = [
            parse_contents(c, n, d) for c, n, d in
            zip(list_of_contents, list_of_names, list_of_dates)]
        print(children)
        return children




if __name__ == '__main__':
    app.run_server(debug=True)
从文件中可以得到你所需要的一切。如果上载CSV文件,则可以使用:

df=pd.read\u csv(io.StringIO(decoded.decode('utf-8'))

从这里开始,只需将其用作普通的熊猫数据帧。

您能分享到目前为止的代码吗?它出了什么问题吗?@coralvanda我发布了代码,问题主要在于不知道是否可以将上传的文件转换为数据帧(我可以使用它)或者我应该使用结果表作为al计算,并将其作为隐藏div.Oh。是的,您当然可以将其转换为数据帧并使用它。因此,当我需要将其用于其他组件时,我会保留生成的表并将其转换为数据帧?我还是不知道该怎么做。。。