'Key must be string' Error

I’m trying to run a function based on an input of a dropdown.

@app.callback(Output('table', 'rows'), 
[Input('field-dropdown', 'value')] , [], [Event('submit-button', 'click')])
def update_table(user_selection):
    df = run_function(user_selection)
    return df.to_dict('records')

The function produces a dataframe, which I want the app to portray. The function works perfectly when I run it alone, but in the app, no table is produced giving me an error ‘Key must be string’.

What does this mean and how can I resolve it?

Thanks!

Perhaps field-dropdown is providing a number instead of a string, because of its setup-code.
If that is the case, you could either use str(user_selection) or change the value's in the options
(and the default/selected value) to strings.

Thanks for the input, but that’s not the issue. I tried the `str(user_selection)… and I dont get an error when the same process calls a dataframe, it just occurs when i call a function that produces a dataframe.

I’ll let you know if i find a way around it!

I found something quiet odd. So I googled the error, its a bug and someone suggested the following work around,
@app.callback(Output(‘table’, ‘rows’),
[Input(‘field-dropdown’, ‘value’)] , [], [Event(‘submit-button’, ‘click’)])
def update_table(user_selection):
df = run_Bid(user_selection)
for key in df.keys():
if type(key) is not str:
try:
df[str(key)] = df[key]
except:
try:
df[repr(key)] = df[key]
except:
pass
del df[key]
return df.to_dict(‘records’)
But then, the columns with ‘string’ indexes are all deleted! I have no idea whats happening haha

Soo… the error is caused because some of the column indexes were strings and the others were numbers. Once I renamed them all to be strings or integers, the problem was solved.

1 Like