Unable to retrieve the filename of FileInput widget in the callback function but only after

Hello everyone!

I am relatively new to Bokeh but starting to get around a bit better.
I have a relatively long file that first ask to updload some csv files and then the data in the files is filtered and plotted in 4 graphs.
I would like to give the the filename of the csv to the file when uploaded with the FileInput widget. But for some reason, I can only retrieve the name after I click on the button “upload data” and not directly in the callback function of the FileInput widget.
Here is the simplified code with only one button.

series = []

def upload_data1(attr, old, new):  # there are several fileinput widgets
    print('file uploaded successfully...')
    decoded = b64decode(new)
    f = io.BytesIO(decoded)
    data1 = pd.read_csv(f, sep=';')
    data1 = data1.loc[:, ~data1.columns.str.contains('^Unnamed')]
    print('file_input1.filename 1 :', file_input1.filename)

def load_data():
    print('file_input1.filename 2:', file_input1.filename)
    columns = sorted(series[0].columns)  # every set of data uploaded has the same columns
    plotting(series, columns)  # function that creates new widgets and plots graphs with all the data in series

# File input
file_input1 = FileInput(accept=".csv", width=250)
file_input1.on_change('value', upload_data1)

bt = Button(label="Load data", width=150)

data_set = row([file_input1, bt])

def plotting(series, columns):

I then call my code with bokeh serve --websocket-max-message-size 100000000 github.py

As you can see, I can only access the filename in the function load_data:

Is there a way to get in the callback function ?
Thank you for your help

This is an open issue:

[FEATURE] where there are multiple related properties that might need to be responded to as a group, respond to them only after all are guaranteed to be updated · Issue #11461 · bokeh/bokeh · GitHub

You might have better luck adding a callback on the filename property as it seems it updated after the value property, so should be able to access both at that point.

Great, thank you very much. It works perfectly now!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.