I am new to Dash and I am trying to set up a basic app where some data is shown, which the user can modify (and plot too although I’ve not included the chart in this minimal piece of code for simplicity). The user should be able to restore the original values i.e. revert back after making some changes. I’ve included a button for that. It seems to work. Have I done it properly?
The user should also have the option to restore the original values of a specific country (row) instead of the whole table e.g. if they are happy with the changes they made to various countries except for one country and they want to restore the original values for that one country. I’ve created selection buttons for that but I don’t know what the callback should be.
Here is my “work in progress” code:
import dash
import pandas as pd
from dash.dependencies import Input, Output, State
df = {
"country": ["angola", "china", "greece", "italy", "myanmar"],
"2024-01-01": [4, 24, 31, 2, 3],
"2024-02-01": [25, 94, 57, 62, 70],
"2024-03-01": [25, 94, 57, 62, 70],
"2024-04-01": [25, 94, 57, 62, 70],
}
df = pd.DataFrame(df)
app = dash.Dash(__name__)
app.layout = dash.html.Div(
children=[
dash.dash_table.DataTable(
id="table",
data=df.to_dict("records"),
columns=[{"name": i, "id": i} for i in df.columns],
editable=True,
row_selectable="multi",
),
dash.html.Button("Restore", id="button", n_clicks=0),
]
)
@dash.callback(
Output("table", "data"),
Input("button", "n_clicks"),
State("table", "selected_rows"),
)
def update_table(n_clicks, selected_rows):
return df.to_dict("records")
if __name__ == "__main__":
app.run_server(debug=True)
Please is anyone able to give me a hand?