短跑短跑-按下按钮时回调不起作用

时间:2019-06-13 11:56:49

标签: python plotly-dash

我正在尝试通过单击“生成地图”按钮来更新一些已上传数据的地图。然后,回调应运行函数“ udate_figure”,但无法正常工作。


layout = html.Div([
    html.Div(html.H3('Map')),
    html.Div(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(dcc.Graph(id='my-graph')),
    html.Button('Generate map', id='button'),
    html.Div(id='output-data-upload'),
])

df_ = pd.DataFrame()


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

    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)]
        update_figure()
        return children


@app.callback(
    Output('my-graph', 'children'),
    [Input('button', 'n_clicks')])
def update_figure():
    trace = []
    trace.append(
        go.Scattermapbox(lat=df_["lat"], lon=df_["long"], mode='markers', marker=go.scattermapbox.Marker(
                         size=14), hoverinfo='text'))
    return {"data": trace,
            "layout": go.Layout(autosize=True, hovermode='closest', showlegend=False, height=700,
                                mapbox={'accesstoken': mapbox_access_token, 'bearing': 0,
                                        'center': {'lat': 38, 'lon': -94}, 'pitch': 30, 'zoom': 3,
                                        "style": 'mapbox://styles/mapbox/light-v9'})}


除了使按钮起作用之外,还调用了update_figure-是否可以在立即上传文件时删除按钮,然后更新地图??

0 个答案:

没有答案