I have a callback where I am adding or deleting rows. After adding/deleting, I would like to pass the table as rowData or virtualRowData to another callback.
Dash is able to do that if row is added, but not able to do that if row is deleted.
I believe, this line is causing the problem. As I understand, table with deleted rows is not saved in/as virtualRowData, correct? How do I bypass it? How do I pass the data/table with a deleted row to another callback ? How do I push the table with a deleted row to rowData or virtualRowData?
So when I click the “Add row” button, the callback in my post is triggered, a row is added and the new table is saved in Output("sql-table", "rowData").
Then, in another callback #2, I input that same table (with added row) as State('sql-table', 'virtualRowData') and process it further. It works.
When I click the “Delete row” buton, the callback in my post is triggered, a row is deleted (I see it on my screen). But the data that is appearing in the other callback #2 is still the original data (row is not deleted). It seems like the new table (after deleted a row) is not saved in/parsed as virtualRowData.
So even though I see with my own eyes on my screen, that the row is deleted, I cannot pass this new table (after deleting a row) to another callback #2 as State('sql-table', 'virtualRowData').
I don’t know how to explain it, but is there no “connection” between deleteSelectedRows and virtualRowData ?
Trigger for secondary callback is simply a “Save to Excel” or “Save to SQL” button (I have both and both are supposed to be taking data from the main callback for adding/deleting rows).
@callback(
[
Output('placeholder', 'children'),
Output("store", "data")
],
[
Input('save-to-sql', 'n_clicks'),
Input("interval", "n_intervals")
],
[
State('sql-table', 'virtualRowData'), #<====here is where the data table (after adding/deleting rows should be falling into
State('original-data', 'data'),
State('store', 'data')
],
prevent_initial_call=True
)
I think I found a clue and I think I need to rephrase my question.
Question: How do I find deleted rows? How do I save them in a separate df?
I did some research and found this topic where they are referencing to a callback parameter data_orevious. I didn’t know about it.
So I can find deleted rows by comparing “current data” to “previous data”. But how do I do that in AG Grid? The question topic above is about a DataTable case, not AG Grid.
Please tell me if I should make a new topic with a proper question (and close this one) or we can continue here.
You can’t save deletedRows into a different row, but what you can do is insert a spot where when you click the delete button, you also store the selectedRows into a new df.
Should it look something like this?
-added State("sql-table", "selectedRows")
-added Output("deleted-rows", "rowData")
-added df_deleted=pd.DataFrame(selection)
@callback(
[
Output("sql-table", "deleteSelectedRows"),
Output("sql-table", "rowData"), # <=== new line
Output("deleted-rows", "rowData")
],
[
Input("delete-row-button", "n_clicks"),
Input("add-row-button", "n_clicks")
],
[
State("sql-table", "rowData"),
State("sql-table", "selectedRows"), # <=== new line
],
prevent_initial_call=True,
)
def update_dash_table(n_dlt, n_add, data, selection):
if ctx.triggered_id == "add-row-button":
#do something
return False, updated_table.to_dict("records"), no_update
elif ctx.triggered_id == "delete-row-button":
df_deleted=pd.DataFrame(selection) # <=== new line
return True, no_update, df_deleted
Hmm, in your code, all your deleted rows would get blasted by the next deleted rows that you were giving. I’d recommend using rowTransaction for your deleted grid.
rowTransaction updates the info clientside, so it is interesting that you werent able to get it working.
I think in my case it could be fine for now. After I am adding/deleting rows, I will be saving the data in SQL (or Excel) via a separate callback/button. And then I am pulling the newly saved edited data back again.
So each time I should have freshly saved data , thus, I can add/delete rows anew again without worrying about losing previously added/deleted ones.
Yes, you are absolutely right about back and forth between the server.
It’s my first attempt to build a fully functioning and complex webApp, so just learning by doing
I will try to fix the back and forth thing in future updates (I need to at least make it work first )