Quantcast
Channel: 🎛️ Dash - Plotly Community Forum
Viewing all articles
Browse latest Browse all 6271

Manage many callbacks to the same output object

$
0
0

Good Morning community,
I came from R Shiny and i just started to use Dash ! it’s great!!! Congratulations to all developers.

I’m facing with a logic problem regarding inputs/outputs/states.
Basically, my app should do this steps:

  1. Get a sql table from my server ( df )
  2. Filter this table, based on three user filters. ( 1° callback )
  3. Update some columns values, based on user inputs. ( 2° callback )
  4. Save back user inputs values to sql table. ( 3° callback )

I get an error about duplicate outputs and i’can’t solve it… [dash.exceptions.DuplicateCallbackOutput]

This is my app ( about callbacks script) :

# callback to filter data based on filters selection
@app.callback(
    Output('computed-table', 'data'),
    [Input('ANNO_filter_dropdown', 'value'),
     Input('AP_filter_dropdown', 'value'),
     Input('CANALE_filter_dropdown', 'value')]
     )

def filtered_data(selected_anno, selected_ap, selected_canale):
    dff = df[(df.ANNO_AP == selected_anno) & (df.NUM_AP == selected_ap) & (df.CANALE_AP == selected_canale)]
    return dff.to_dict('records')


########################################################################################################################
# callback to update cells data based on insert into others cells
@app.callback(
    Output('computed-table', 'data'),
    [Input('computed-table', 'data_timestamp')],
    [State('computed-table', 'data')]
    )

def update_columns(timestamp, rows):
    for row in rows:
        try:
            row['Desc_Articolo'] = check.find_desc_art(int(row['Cod_Articolo']))
            row['Costo_Interc'] = check.intercept_cost(int(row['Cod_Articolo']), 20200812)
            row['Stato_Art'] = check.find_stato_art(int(row['Cod_Articolo']))
        except:
            row['Costo_Med_Pond'] = 'NA'
    return rows


########################################################################################################################

# callback to export data
@app.callback(
    Output("output-1", "children"),
    [Input("save-button", "n_clicks")],
    [State("computed-table", "data")]
    )

def filtered_data_to_sql(nclicks, table1):
    if nclicks > 0:
      for value in table1:
        cursor = conn.cursor()
        cursor.execute("UPDATE [Mydb].[dbo].[test_python_table] SET Cessione_Off=? , Pubblico_Off=? WHERE Cod_Articolo=?", (value['Cessione_Off'], value['Pubblico_Off'], value['Cod_Articolo']) )
        conn.commit()
        cursor.close()

    return "Data Submitted"

5 posts - 3 participants

Read full topic


Viewing all articles
Browse latest Browse all 6271

Trending Articles