'Python dash django app : trying to record cells selected into a dataframe but keeps overwriting
I'm new to dash, I'm building an app in a django framework, part of the app is a dash table where the user will select cells, I want a callback that will format the current selected cell and those previously selected. So far I have tried two approaches with no luck, the first had some success, it formatted the active cell but didn't record the previously selected cells. The second approach has no success as the code doesn't seem to get read at all.
First approach:
dff=pd.Dataframe()
pre_sel=pd.Dataframe()
df4=[django_table] """df4 works fine and shows in below table so haven't included all code
here"""
app.layout= [ dash_table.DataTable(
id='table',
columns=[{"name": i, "id": i, 'type': 'numeric', 'format': Format(precision=4,
scheme=Scheme.fixed, trim=Trim.yes)} for i in df4],
data=df4.to_dict('records'),
style_table={'overflowX': 'scroll'}
)
)]
@app.callback(
Output('table', 'style_data_conditional'),
Input('table', 'active_cell')
)
def prev_sel(active_cell):
new_exc = pd.DataFrame(active_cell, index=[0]) if active_cell else None
pre_sel= dff.append(new_exc, ignore_index=True)
count = len(pre_sel)
exc = 0
style_data_conditional = []
while exc < count:
style_data_conditional = [
{
"if": {"column_id": pre_sel.iloc[exc, 2], "row_index": pre_sel.iloc[exc, 0]},
"backgroundColor": "rgba(150, 180, 225, 0.2)",
"border": "1px solid blue",
},
{
"if": {"column_id": "index"},
"backgroundColor": "rgba(116, 116, 117, .03)",
"border": "green",
"color": "black",
}
]
print(pre_sel)
print(pre_sel.iloc[exc, 0])
print(pre_sel.iloc[exc, 2]) """these were 3 print checks which were showing in console"""
exc += 1
return style_data_conditional
Second approach was to set up an empty dash table and write the data back:
clmnex =['row', 'column', 'column_id']
tablex = dash_table.DataTable(columns=[{'name': column, 'id': column} for column in clmnex],
data = [], id='tablex')
app.layout= [ dash_table.DataTable(
id='table',
columns=[{"name": i, "id": i, 'type': 'numeric', 'format': Format(precision=4,
scheme=Scheme.fixed, trim=Trim.yes)} for i in df4],
data=df4.to_dict('records'),
style_table={'overflowX': 'scroll'}
)
)]
@app.callback(
Output('table', 'style_data_conditional'),
Output('tablex', 'data'),
Input('table', 'active_cell'),
[Input('tablex', 'data')]
)
def prev_sel(active_cell,data):
new = pd.DataFrame(active_cell), """if active_cell else None"""
data += new.to_dict('records')
print('here')
count = len(data)
exc = 0
while exc < count:
style_data_conditional = [
{
"if": {"column_id": data.iloc[exc, 2], "row_index": data.iloc[exc, 0]},
"backgroundColor": "rgba(150, 180, 225, 0.2)",
"border": "1px solid blue",
},
{
"if": {"column_id": "index"},
"backgroundColor": "rgba(116, 116, 117, .03)",
"border": "green",
"color": "black",
}
]
exc += 1
return style_data_conditional, data
First approach had some success, it was just missing the recording previous selections, second never printed the 'here' statement, no idea why. Only need one of the approaches to work.
I've also noticed that I'm only able to pass one input to a function in callbacks, is this normal?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
