How to access selected file name in Panel Fileinput widget?

Viewed 1753

I am using python Panel Fileinput widget to upload a file. Upload works and I need to save uploaded data with same filename, but I can'n find way to access uploaded file name.

import panel as pn
pn.extension()

file_input = pn.widgets.FileInput()
file_input

After selecting file the widget displays filename next to it, but filename is not included in objects enter image description here

file_input.get_param_values()

These are parameters of the sample file 'test.txt'

[('align', 'start'),
 ('aspect_ratio', None),
 ('background', None),
 ('css_classes', None),
 ('disabled', False),
 ('height', None),
 ('height_policy', 'auto'),
 ('margin', (5, 10)),
 ('max_height', None),
 ('max_width', None),
 ('mime_type', 'text/plain'),
 ('min_height', None),
 ('min_width', None),
 ('name', ''),
 ('sizing_mode', None),
 ('value', b''),
 ('width', None),
 ('width_policy', 'auto')]

Need: file_input.filename variable for accessing uploaded file name.

2 Answers

Which version of panel are you using?
If I do this with panel 0.6.2, it works for me:

import panel as pn
pn.extension()

file_input = pn.widgets.FileInput()
file_input

file_input.get_param_values()


Before I select a file, there's already the following attributes:

[('filename', None), ('value', None), ]

And after selecting a file, I see the following attributes:

[('filename', 'Screenshot 2019-04-04 at 18.42.55.png'), ('value', content of the image file)]

So getting the filename, should just be:

file_input.filename

And getting the file, should be:

file_input.value

This was a bug, certainly with panel 0.7.0 and bokeh 1.4.0 (probably due to some change in bokeh - did not investigate). Fixed in this PR.

Related