plotly破折号上传按钮没有反应

问题描述 投票:0回答:1

我有这段代码,几个月前就可以运行。没有错误,但是现在当我点击上传按钮时不再响应。由于没有错误,我无法解决发生的问题。我还尝试使用plotly的示例:https://dash.plotly.com/dash-core-components/upload但代码仍然不起作用。有人遇到同样的问题吗

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

app = Dash(__name__, external_stylesheets=external_stylesheets)

# if deploy onto a server
server = app.server


# create a background template for plots
templates = ['plotly', 'seaborn', 'simple_white', 'ggplot2',
             'plotly_white', 'plotly_dark', 'presentation', 'xgridoff',
             'ygridoff', 'gridon', 'none']


# create a blank figure to prevent plotly dash error when runnng the app, even though it still works without this.
def blankfigure():
    figure = go.Figure(go.Scatter(x=[],y=[]))
    figure.update_layout(template = None)
    figure.update_xaxes(showgrid = False, showticklabels = False, zeroline = False)
    figure.update_yaxes(showgrid = False, showticklabels = False, zeroline = False)

    return figure


app.layout = html.Div([
        
        # components for label and input content by user
        dbc.Row([dbc.Col(html.H1('Plotter App', style={'textAlign': 'center', "font-size":"60px"}))]), # title

        # components for upload file 
        dbc.Row([dbc.Col(html.Label('Upload file') ,style={'textAlign': 'left',"font-size":"30px"})]),
        dbc.Row([dcc.Upload(id='upload-data',children=html.Div(['Drag and Drop or ',html.A('Select Files')]),
                    style={
                        'width': '30%',
                        'height': '60px',
                        'lineHeight': '60px',
                        'borderWidth': '1px',
                        'borderStyle': 'dashed',
                        'borderRadius': '5px',
                        'textAlign': 'center',
                        'margin': '10px'
                            },
                        # Allow multiple files to be uploaded
                        multiple=False)
                ]),
        html.Div(id='output-data-upload', children = ''),    

        # component for radio button for show/hide table
        dbc.Row([dcc.RadioItems(id = 'show_hide_table_button', options = ['show_table', 'hide_table','show_plot'], value = 'hide')]),        
])

def parse_contents(contents, file_name): # 'contents/filename' property is needed for callbacks
    
    content_type, content_string = contents.split(',')
    #print(content_type)
    
    decoded = base64.b64decode(content_string)
    #print(decoded)
   
    if 'csv' in file_name:
        # Assume that the user uploaded a CSV file
        df = pd.read_csv(io.StringIO(decoded.decode('utf-8')))
    elif 'xls' in file_name:
        # Assume that the user uploaded an excel file
        df = pd.read_excel(io.BytesIO(decoded))
    elif 'txt' in file_name:
        # Assume that the user uploaded an text file
        # 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte -> USE utf-16
        df = pd.read_csv(io.StringIO(decoded.decode('utf-16')), delimiter = '\t') # \t separates columns separated by tabs

    return df

@app.callback(
    Output('output-data-upload', 'children'),
    [Input('upload-data', 'contents'), # refer to first arg in upload_data_file()
    Input('upload-data', 'filename'), # refer to 2nd arg in upload_data_file()
    Input(component_id = 'show_hide_table_button', component_property = 'value')] # refer to 3rd arg in upload_data_file()
    
    #State('upload-data', 'last_modified'),
    #prevent_initial_call=True
)
    
def upload_data_file(contents, file_name, display):
    show_table = html.Div()
    show_plot = html.Div()
    
    global uploaded_df
    if contents is not None:
        uploaded_df = parse_contents(contents, file_name) # dataframe object
        dropdown = uploaded_df.columns
        show_table = html.Div([
                    dbc.Row([dbc.Col([html.H5(file_name)])]),
                    
                    # show data table
                    dbc.Row([dbc.Col([dash_table.DataTable(data=uploaded_df.to_dict('rows'), columns=[{"name": i, "id": i} for i in uploaded_df.columns])])]),
                    ])

        show_plot = html.Div([
            
                    # component for show dropdown options
                    dbc.Row([dbc.Col(html.Label('Select x-axis from dropdown'), width=2), #width = between Cols
                            dbc.Col(dcc.Dropdown(id = 'xaxis_column', options = dropdown, value = None)),
                            dbc.Col(html.Label('Select x-axis from dropdown'), width=2), #width = between Cols
                            dbc.Col(dcc.Dropdown(id = 'xaxis_column1', options = dropdown, value = None)),
                            ]),

                    dbc.Row([dbc.Col(html.Label('Select y-axis from dropdown'), width=2),
                            dbc.Col(dcc.Dropdown(id = 'yaxis_column', options = dropdown, value = None, multi = True)),
                            dbc.Col(html.Label('Select y-axis from dropdown'), width=2),
                            dbc.Col(dcc.Dropdown(id = 'yaxis_column1', options = dropdown, value = None, multi = True)),
                            ]),
            
                    # component for output graph(s) when data is uploaded
                    dbc.Row([dbc.Col(dcc.RadioItems(id='template', options = [{'label': k, 'value': k} for k in templates], value = None, inline=True)),
                            dbc.Col(dcc.RadioItems(id='template1', options = [{'label': k, 'value': k} for k in templates], value = None, inline=True)), # grid style
                            ]), # grid style
                    
                    html.Br(),
        
                    # components for graph, graph options, and download data from plot (for two plots)
                    dbc.Row([dbc.Col(dcc.Graph(id="graph", figure=blankfigure())),
                            dbc.Col(dcc.Graph(id="graph1", figure=blankfigure()))
                            ]),
                    dbc.Row([dbc.Col(dcc.Dropdown(id = 'plot_type', options = ['scatter,','line','bar','box'], value = 'line', style={'font-size':20})),
                            dbc.Col(dcc.Dropdown(id = 'plot_type1', options = ['scatter','line','bar','box'], value = 'line', style={'font-size':20})),
                            ]),
                    ])
    
                    
    # connecting radio button options with output of upload button
    if display == 'show': 
        return show_table
    if display == 'hide_table':
        return None
    if display == 'show_plot':
        return show_plot

这以前有效,我什至尝试复制并粘贴绘图破折号代码示例,但上传按钮仍然不起作用。

python plotly plotly-dash
1个回答
0
投票

可能是因为您使用的是 Mac 笔记本电脑。我以前遇到过同样的问题。我尝试了你的代码,当 http 出现时,我能够上传 csv 文件。

© www.soinside.com 2019 - 2024. All rights reserved.