Get full path from file with FileInput widgets

Dear all !

I’m trying to get the full path of a file selected with the FileInput widgets. Is that possible ?
I cannot use the FileSelector widgets because

  • as I can see, I can’t navigate easily into folder with the mouse (double clic in a folder doesn’t work)
  • I just a small non-complex UI to select a file (like with the FileInput :grin:)

Thank you very much for your help =D
Best regards,
Thibault.

I don’t think it is possible due to web securities think I’ve read this somewhere before

My understanding is file selector works server side that’s why you can get file path, fileinput widget is a feature of the web browser and constrained to its rules

1 Like

Okay thank you for your precisions :slightly_smiling_face: !

I find a workarround with TK for now… Altought I’m not sure this is optimal…

import panel as pn
pn.extension('tabulator', 'terminal', 'ipywidgets',sizing_mode = 'stretch_width', loading_spinner='dots')

from tkinter import Tk, filedialog

def select_files(*b):
    root = Tk()
    root.withdraw()                                        
    root.call('wm', 'attributes', '.', '-topmost', True)   
    files = filedialog.askopenfilename(multiple=False)    
    print(files)
    return files                                         


button = pn.widgets.Button(name="Load file")
button.on_click(select_files)

button.servable()

Cheers,
Thibault.

5 Likes

I can’t comment about it being optimal or not but it works nicely for myself in small tests just done, I think will be very useful!

Thanks, Carl.

I don’t understand. Why is this possible with Tk but not Panel?

It would be nice if FileSelector were a modal file dialog. In most cases, I don’t want to display the FileSelector unless I am selecting a file or directory.

Yes indeed:

  • the FileSelector is just meant to access files that are hosted on the server
  • web browsers cannot get the full path to files on your system, for security reasons as already mentioned

I am pretty sure the tkinter trick just works locally, i.e. when you or your users are using the app by launching it locally with Panel, not just by visiting a URL. It’s fine if that’s the intended usage of your app, but a pretty big limitation if it’s meant to be served over a network.

Sorry, I have confused the topic. My concern is the server side. I would prefer a basic modal file dialog to the static FileSelector widget.

Ok! Maybe then you could open a feature request on Panel’s Github. I believe that the current FileSelector could be improved to get a widget that’s good enough to select a file:

  • double clicking on a repository should open it (now it’s done with a button)
  • there could be a parameter to limit the number of files selected

But you could also open a feature request for a new widget. Before that though you could try to look around if this is something that is actually common and how it’s implemented (maybe with django, flask, etc.)

1 Like