Is it possible to edit all the selected cells in one go in an editable Dash table? In spreadsheet programs, you can typically hold ctrl while having cells selected to apply that value to all the selected cells, but this doesn’t seem to work in Dash. I can’t find an option for this when setting up the table, so I guess I need to do this in a callback with the selected_cells attribute. Can anyone give me any pointers on how to achieve this?
I see that when you have selected some cells, hitting backspace/delete will actually empty all the selected cells.
Try this example. After editing one cell, push intro (return in mac) to make the change effective and voilá!
from dash import Dash, dash_table, dcc, html, callback, Input, Output, State
from dash.dependencies import Input, Output
import pandas as pd
import json
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv')
# add an id column and set it as the index
# in this case the unique ID is just the country name, so we could have just
# renamed 'country' to 'id' (but given it the display name 'country'), but
# here it's duplicated just to show the more general pattern.
df['id'] = df['country']
df.set_index('id', inplace=True, drop=False)
app = Dash(__name__)
app.layout = html.Div([
dash_table.DataTable(
id='table',
columns=[
{'name': i, 'id': i, 'deletable': True} for i in df.columns
# omit the id column
if i != 'id'
],
data=df.to_dict('records'),
editable=True,
# row_selectable='multi',
# row_deletable=True,
# selected_rows=[],
page_action='native',
page_current= 0,
page_size= 10,
)
])
@callback(
Output('table', 'data'),
Input('table','data'),
State('table','columns'),
State('table','selected_cells'),
prevent_initial_call=True
)
def check(data, cols, selected_cells):
selected_row_id = [c['row_id'] for c in selected_cells]
changed_cell = selected_cells[0]
new_value = data[changed_cell['row']][changed_cell['column_id']]
for r in data :
if r['id'] in selected_row_id :
r[changed_cell['column_id']] = new_value
return data
if __name__ == '__main__':
app.run_server(debug=True)