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

Dash_table update on click not working

$
0
0

Hello,

I am trying to update a dash_table based on data from an external data source when a button has been clicked. I have managed to get the selected rows working it highlights the rows but the function for on click isn’t working. The current code is:

def account_data(db_reference):
  ref = db.reference(db_reference)
  users = ref.get()
  users = pd.DataFrame.from_dict(users, orient = "index")
  users.reset_index(inplace=True)
  users.rename(columns = {"index":"uid"}, inplace=True)
  #clean the data
  users = users.replace(r'^\s*$', np.nan, regex=True)
  users["city_of_residence"] = users["city_of_residence"].str.split(",").str[0]
  users["city_of_residence"] = users["city_of_residence"].str.replace('"', "")
  users["city_of_residence"] = users["city_of_residence"].str.rstrip()

  return users

def auth_data():

  user_dict = {}

  for user in fb_auth.list_users().iterate_all():
    user_dict[user.uid] = {"Email":user.email}

  auth_users = pd.DataFrame.from_dict(user_dict, orient = "index")
  auth_users.reset_index(inplace=True)
  auth_users.rename(columns = {"index":"uid"}, inplace=True)

  return auth_users

def unathorised_users(accounts, users):

  uids_no_account = list(set(accounts["uid"]) - set(users["uid"]))

  uids_not_verified = users[(users["Verified"] == False)
                              | (users["Verified"] == "False")
                              | (users["Verified"] == "false")
                              | (users["Verified"].isna())]
  uids_not_verified = list(uids_not_verified["uid"])

  #merge_list 
  uids_to_verify = uids_no_account + uids_not_verified

  return uids_to_verify

def data_to_verify(users, accounts, uids):

  users_to_verify = users.merge(accounts,
                                on = "uid",
                                how = "outer")

  users_to_verify = users_to_verify[users_to_verify["uid"].isin(uids)]

  users_to_verify["name"] = users_to_verify["name"].replace(np.nan, "Name not given")
  users_to_verify["Email"] = users_to_verify["Email"].replace(np.nan, "Email not given")
  users_to_verify.rename(columns = {"uid":"User id",
                                  "name":"Name"}, inplace=True)

  return users_to_verify


users = account_data("/users/") 

auth_users = auth_data()

uids_to_verify = unathorised_users(auth_users, users)

users_to_verify = data_to_verify(users, auth_users, uids_to_verify)

columns_to_verify = ["User id", "Name", "Email"]

app.layout = html.Div([
html.Div([html.H3("Table of unverfied users", style = {"color":"grey"}),
                 dash_table.DataTable(id = "email-auth",
                                 columns = [{"name":i, "id":i} for i in columns_to_verify],
                                 data = users_to_verify.to_dict("records"),
                                #for page size i.e. pages page_size = 10
                                  page_size = 50,
                                 page_action = "none",
                                 #add row selectivity
                                 row_selectable = "multi",
                                 #store selected rows
                                 selected_rows = [],
                                  fixed_rows = {"headers":True},
                                  style_header = {"backgroundColor":"blue",
                                                  "color": "white",
                                                  "border":"1px solid black"},
                                  style_cell = {"textAlign":"left",
                                                "border":"1px solid black"},
                                 style_table={ "overflowY":"auto",
                                                "overflowX":"auto"})],
                style = {"display":"block",
                        "width":"80%",
                        "marginTop":10,
                        "marginBottom":20,
                        "margin-left":"auto",
                        "margin-right":"auto",
                        "padding":"10px"}),

    html.Div([
    
    dcc.ConfirmDialogProvider(
      children = html.Button("Verify Users", id = "but-verify", n_clicks = 0),
      id = "check_verification",
      message = "Are you sure you want to verify selected users"
    ),

    dcc.ConfirmDialogProvider(
      children = html.Button("Remove Users", id =  "but-remove", n_clicks = 0),
      id = "check_removal",
      message = "Are you sure you want to permanently remove selected users"
    )
    ])
])

@app.callback(
  Output("email-auth", "style_data_conditional"),
  Input("email-auth", "selected_rows")
 # prevent_initial_call = True
)
def update_styles(selected_rows):
  print("I am called")
  print(selected_rows)
  table_style = [{"if": {"row_index":i}, "background_color":"#F0F8FF"} for i in selected_rows]

  return table_style  

@app.callback(
  Output("email-auth", "data"),
  [Input("but-verify", "n_clicks")],
  [State("email-auth", "selected_rows")],
  prevent_initial_call=True
)
def verify_data(verify_clicks, selected_rows):

  if verify_clicks is None:
    raise dash.exceptions.PreventUpdate
  else:

    users = account_data("/users/")
    auth_users = auth_data()
    uids_to_verify = unathorised_users(auth_users, users)
    users_to_verify = data_to_verify(users, auth_users, uids_to_verify)

    user_ids = users_to_verify.iloc[selected_rows]
    user_ids = list(user_ids["User id"])

    for user_id in user_ids:
      try:
        ref.child(user_id).child("Verified").set(True)
      except:
        print(user_id, " cannot be verified")   

    users = account_data("/users/")
    auth_users = auth_data()
    uids_to_verify = unathorised_users(auth_users, users)
    users_to_verify = data_to_verify(users, auth_users, uids_to_verify)

  return users_to_verify.to_dict("records")

The account data(), auth_data(), unauthorised_users() and data to verify() functions all work and extract data from the firebase database when the app is initially loaded. I just want to update the realtime database, then the same calls are made to get the updated data. The function doesn’t seem to want to work at all for updating the dash table, or the firebase realtime database and no errors are thrown.

Any advice appreciated!

Thanks!

5 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 6271

Latest Images

Trending Articles



Latest Images