i am adding sentiment_classification column dynamically and want to have dropdown in it.
@app.callback(
[Output(‘datatable-paging’, “data”),
Output(‘datatable-interactivity-container’, “children”),
Output(‘datatable-filter-container’, “children”),
Output(‘datatable-paging’, “columns”),
Output(‘datatable-paging’, “dropdown”),
],
[Input('datatable-paging', "page_current"),
Input('datatable-paging', "page_size"),
Input('datatable-paging', "sort_by"),
Input('datatable-paging', "filter_query"),
Input(component_id='drop_column_dropdown', component_property='value'),
Input("classification_column_dropdown", "value"),
Input("sentiment_classification_btn", "n_clicks")])
def update_table(page_current, page_size, sort_by, filter,drop_columns,classification_column,sentiment_clicked):
filtering_expressions = filter.split(’ && ')
dff = load(os.path.join(UPLOAD_DIRECTORY, upload_pickle))
#dff[(dff[‘region’].isin(town)]
print(“columns to drop”,drop_columns)
if drop_columns is not None:
dff.drop(drop_columns, axis = 1,inplace=True)
for filter_part in filtering_expressions:
filter_part = str(filter_part)+"|\?"
col_name, operator, filter_value = split_filter_part(filter_part)
if operator in ('eq', 'ne', 'lt', 'le', 'gt', 'ge'):
# these operators match pandas series operator method names
dff = dff.loc[getattr(dff[col_name], operator)(filter_value)]
elif operator == 'contains':
dff = dff.loc[dff[col_name].astype(str).str.contains(filter_value)]
elif operator == 'datestartswith':
# this is a simplification of the front-end filtering logic,
# only works with complete fields in standard format
dff = dff.loc[dff[col_name].astype(str).str.startswith(filter_value)]
if len(sort_by):
dff = dff.sort_values(
[col['column_id'] for col in sort_by],
ascending=[
col['direction'] == 'asc'
for col in sort_by
],
inplace=False
)
column_dropdown = {}
if sentiment_clicked is not None:
dff["Sentiment_Classification"] = ["Negative","Positive"]*(int(len(dff)/2))
column_dropdown = {
'Sentiment_Classification': {
'options': [
{'label': i, 'value': i}
`for i in dff['Sentiment_Classification'].unique()`
]
}}
columns=[{'name': i, 'id': i,} for i in dff.columns]
return dff.iloc[
page_current*page_size: (page_current + 1)*page_size
].to_dict('records'),f"Total rows:{dff.shape[0]} ,Total columns: {dff.shape[1]}",f"Filter(s) Applied : {filter}" ,columns,column_dropdown`Preformatted text`