Datable o.weights[e] is undefined

Hi everyone,

(edit: Just wanted to clarify that I am just looking for an explanation to what the error means to debug my code, not for a solution :slight_smile:)

I have been getting a weird error that I seem to be unable to replicate to publish a minimum viable code here, thus I am looking for a general understanding of what that means. When sending back a df.to_dict(ā€˜recordsā€™),
with back-end paging in python, if I add the options for multi selection (row_selectable=ā€œmultiā€ and selected_row=), I get the following error (edit: when I donā€™t add multi selection the table works):

ā€œo.weights[e] is undefinedā€

What does that error mean? Cannot find anything online

I am doing the following in the code

className=ā€œnine columns chart_divā€,
children=[
dt.DataTable(
id=ā€˜apg_ppg_tableā€™,
columns=,#[{ā€œnameā€: i, ā€œidā€: i} for i in df.columns],
data=pd.DataFrame().to_dict(ā€˜recordsā€™),

                                                        row_selectable="multi",
                                                        selected_rows=[],

                                                        #action on table
                                                        page_current=0,
                                                        page_size=PAGE_SIZE,
                                                        page_action='custom',

                                                        filter_action='custom',
                                                        filter_query='',

                                                        sort_action='custom',
                                                        sort_mode='multi',
                                                        sort_by=[],
                                                )
                                 ],

@app.callback([Output(component_id=ā€˜apg_ppg_tableā€™, component_property=ā€˜dataā€™),Output(component_id=ā€˜apg_ppg_tableā€™, component_property=ā€˜columnsā€™)]
,[Input(ā€˜button_filteringā€™, ā€˜n_clicksā€™),Input(ā€˜additional_columns_tableā€™, ā€˜valueā€™),Input(ā€˜apg_ppg_tableā€™, ā€œpage_currentā€), Input(ā€˜apg_ppg_tableā€™, ā€œpage_sizeā€),Input(ā€˜apg_ppg_tableā€™, ā€œsort_byā€),Input(ā€˜apg_ppg_tableā€™, ā€˜filter_queryā€™)],)

def update_table(n_clicks,name_cols_to_show,page_current,page_size,sort_by,query):
df = find_in_table(df,query.split(ā€™ && '))
df = sort_table(df,sort_by)
page_current = min(page_current,max(round(len(df)/page_size)-1,0))
df = df.loc[page_current*page_size:(page_current+ 1)*page_size,cols_to_show].reset_index(drop=True) #this reset index should be useless rn
cols_to_show.remove(ā€˜HIGHLIGHTā€™)
data_table_columns = [columns_type_dict[i] for i in df[cols_to_show].columns]
print(df)
return [df.to_dict(ā€˜recordsā€™),data_table_columns]

Where sort_table and find_in_table are taken from the datatable walkthrough (find_in_table == filtering action, just in a separate function)

1 Like

Just wanted to clarify that I am just looking for an explanation to what the error means to debug my code, not for a solution :slight_smile:

I ended up finding the solution to this error.

It happens because of the following snippet of code:

columns=,#[{ā€œnameā€: i, ā€œidā€: i} for i in df.columns],
data=pd.DataFrame().to_dict(ā€˜recordsā€™),

which conflicts with the row_selectable=ā€œmultiā€ argument.

I suspect row_selectable counts/needs the (number?) rows of the data to create the selectable row. Passing an empty dataframe must not be accounted for properly and creates a problem which prevents the creation of the table.

Very simple work-around it is just to initialize the data or pass the entire table as a children of that function.

2 Likes

Hello,

Would you mind explaining your solution a bit more clearly?

I had a similar error but I believe the cause is not the exact same.

A better understanding of your solution may help me solve it.

Thank you

since I am passing an empty data frame (which by definition has no rows), the row_selectable=ā€œmultiā€ cannot create the click button you see on the side of the table.

I am assuming the code for the creation of the clickable thing on the side prevents the creation of the table.

My solution was to simply pass it real data and have the table updated with what I wanted via callback

I had the same error. Thanks to your explanation, I could finally find the source. I had an initially empty table and that didnā€™t get along well with the
row_deletable=True attribute of the data table.

Instead of prefilling the table with real data, I listed in my callback the ā€˜row_deletableā€™ attribute of the table as an output and return True. That way ā€˜row_deletableā€™ is set only after data has arrived.
Looks like this:

@app.callback(
    [Output('mytable', 'columns')
     Output('mytable', 'data'),
     Output('mytable', 'row_deletable')],
    [Input('someinput', 'stuff')]
)
def generate_table(input):
    # somehow make a dataframe out of your input
    df = make_somehow_df(input)
    columns = [{ā€œnameā€: i, ā€œidā€: i} for i in df.columns]
    data = df.to_dict(ā€˜recordsā€™)
    return columns, data, True     # True is the value for mytable's row_deletable attribute

Many thanks @lemon for pointing out the reason of the error.

Hi, I had a similar problem that was due to the first callback being fired by the app itself. This callback received no data and the solution was to prevent the update by raise dash.PreventUpdate that was called conditional on not receiving any data. I am far from being any expert in any of this, so please tell me if that is a silly way out, but at least it worked in my case.

best wishes,
Matteo