AZT, I had similar problems. I battled through and attach some code to highlight cells in a datatable, collect the values in a dataframe for subsequent processing. The idea is you can toggle the highlighted cell or clear them with a button. I am a novice so I am sure the code could be more efficient and would welcome comments.
import os
import numpy as np
import dash
from dash.dependencies import Input, Output, State
import dash_table
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import time
from datetime import datetime
results_table_headings = [ ‘ASX Code’, ‘Amount or Weighting Percent’,‘Calculated Weighting Percent’ ,‘Dividend Yield Percent’, ‘Franking Percent’,
‘Effective Franked Dividend’, ‘Capital Gain’,‘Total Return’,
‘Return Contribution’, ‘Standard Deviation’]
initialdata = [[‘AFI’, ‘$3000’,’’, ‘’, ‘’, ‘’,’’,’’,’’,’’],[‘CBAPF’, ‘$3600’,’’, ‘’, ‘’,’’,’’,’’,’’,’’],
[‘MFF’, ‘$4000’,’’, ‘’, ‘’,’’,’’,’’,’’,’’],[‘CASH’, ‘$1500’,’’ , ‘’, ‘’,’’,’’,’’,’’,’’]]
initialised_table = pd.DataFrame(initialdata, columns = results_table_headings)
app = dash.Dash(name)
app.layout = html.Div([
dash_table.DataTable(
id=‘table-editing-simple’,
columns=( [{‘id’: p, ‘name’: p, “hideable”: True} for p in results_table_headings] ),
data = initialised_table.to_dict(‘records’),
editable=True,
filter_action=“native”,
sort_action=“native”,
sort_mode=“multi”,
column_selectable=“single”,
row_selectable=“multi”,
row_deletable=True,
selected_columns=[],
selected_rows=[],
page_action=“native”,
page_current= 0,
page_size= 10,
),
html.Button( id=‘clear_selection_btn’, children=‘Clear Selection’,
n_clicks_timestamp=0, style={ ‘display’: ‘inline’, ‘backgroundColor’: ‘White’ , ‘color’ :‘black’ }),
dcc.Store(id=‘stylememory’,storage_type=‘session’),
dcc.Store(id=‘cellmemory’,storage_type=‘session’),
html.Div(id =‘Output Message’),
])
@app.callback([Output(‘stylememory’, ‘data’), Output(‘cellmemory’, ‘data’)],
[Input(‘table-editing-simple’, ‘active_cell’),Input(‘table-editing-simple’, ‘data’),Input(‘clear_selection_btn’, ‘n_clicks_timestamp’)],
[State(‘stylememory’, ‘data’),State(‘cellmemory’, ‘data’),State(‘clear_selection_btn’, ‘n_clicks’)])
def store_styles(active_cell, data, n_clicks_t, currstyle, currstylecells, n_clicks ):
if not active_cell: return (),’’
if currstylecells == '' : currstylecells = pd.DataFrame()
else: currstylecells = pd.DataFrame.from_dict(currstylecells, orient='columns')
if n_clicks != None: # Clear selection button
if len(currstylecells) != 0:
if n_clicks_t > currstylecells.iloc[:1].timestamp[0]:
return [{ 'background_color': 'transparent' }], ''
# highlighting style
style = [{ 'if': { 'row_index': active_cell['row'] , 'column_id': active_cell['column_id'] } , 'background_color': '#ffffe0' }]
# collect the active cell information
activecellrow = int(active_cell['row'])
activecellcol = str(active_cell['column_id'])
activecellval = str(data[active_cell['row']][active_cell['column_id']])
timestamp = int(time.time()*1000)
activecellinfo = {'activecellrow': activecellrow, 'activecellcol': activecellcol, 'activecellval': activecellval, 'timestamp': timestamp }
if len(currstylecells) == 0 : # first cell clicked
finalstyle = style
currstylecells = currstylecells.append([activecellinfo],ignore_index=True)
else: # subsequnt cells
if ((currstylecells.activecellrow == activecellrow).any() and (currstylecells.activecellcol == activecellcol).any()):
i = 0 # look for duplicates signifying a toggle
for i in range(len(currstyle)-1):
if activecellrow == currstyle[i]['if']['row_index'] and activecellcol == currstyle[i]['if']['column_id']:
droppedstyle = currstyle.pop(i) # remove style entry
currstylecells = currstylecells.drop(currstylecells.activecellrow[currstylecells.activecellrow == activecellrow].index.tolist()[0])
finalstyle = currstyle
else: # no duplicates so add to both files
currstylecells = currstylecells.append([activecellinfo],ignore_index=True)
finalstyle = currstyle
finalstyle.extend(style)
currstylecells = currstylecells.to_dict('list')
return finalstyle, currstylecells
@app.callback(Output(‘table-editing-simple’, ‘style_data_conditional’),
[Input(‘stylememory’, ‘data’)],
[State(‘table-editing-simple’, ‘data’),State(‘table-editing-simple’, ‘columns’)])
def update_styles(style, rows, columns):
if not style: return style
else:
results_table = pd.DataFrame(rows, columns=[c[‘name’] for c in columns])
return style
if name == ‘main’:
app.run_server(port=8077, host=‘127.0.0.1’)