Panel - Dynamically Viewing the file in File Selector

Hi All,

I am new to Panel and new help with File Selector.

I have defined a save_template_button to save templates and a FileSelector. When I click on save_templte_button it saves a template to the location from where File Selector should pick it.

self.save_template_button = pn.widgets.Button(name="\u21bb Save Template", button_type="primary", height=50, width=200)
self.files = pn.widgets.FileSelector('/Vineet/Saved_Files')
def callback(target, event):
   if len(target.value) > 1:
        target.value = [target.val[-1]]
        target._update_files(target)

self.files.link(
    self.files,
    callbacks = {"value":callback}
)
self.files

However, when I add a new file using the save_template_button, I am not able to see the new file it in the FileSelector unless I restart the server.
How can we dynamically update the files in File Selector?

It is easier to help if the minimal running code is complete, you can copy,paste and run it. I completed it according to what I believed. Sure there is a public method, but the ._refresh method works.

import panel as pn 

class Files():
  def get_files(self):
    self.save_template_button = pn.widgets.Button(name="\u21bb Save Template", 
        button_type="primary", height=50, width=200)
    self.files = pn.widgets.FileSelector('../Desktop')
    def callback(event):
        self.files._refresh()
    self.save_template_button.on_click(callback)
    return pn.Column(self.save_template_button, self.files)

f = Files()
pn.Row(f.get_files()).servable()

1 Like

Hi @nghenzi2019,

Apologies for the missing code. The detail for the problem are defined below-
I have a directory ‘/Users/Shaheen/Desktop/Vineet/MS_SE_Spring2019/Panel/Files’.

Our batch job will dumb files to this folder, now we should be able to select files from this folder and be able to upload further, I have implemented the part for uploading. The challenge I am facing is to dynamically refresh the files in the ‘Files’ folder.

I used the code you shared-
import panel as pn

class Files():

def get_files(self):

    def callback(event):
        self.files._refresh()

    self.refresh_template_button = pn.widgets.Button(name="\u21bb Refresh Template", 
        button_type="primary", height=50, width=200)
    self.files = pn.widgets.FileSelector('/Users/Shaheen/Desktop/Vineet/MS_SE_Spring2019/Panel/Files')

    self.refresh_template_button.on_click(callback)
    return pn.Column(self.refresh_template_button, self.files)

f = Files()
pn.Row(f.get_files()).servable()

Currently I have two files at the location-
‘/Users/Shaheen/Desktop/Vineet/MS_SE_Spring2019/Panel/Files’

Now if I any batch adds one or more files and we click on ‘Refresh Template’ button, we are not able to see the new files added in the ‘File Selector’ panel.
Hopefully, I am able to explain the issue I am facing here. Please feel free if you need any additional information. Thanks a lot!

maybe it is related to panel version. Try to upgrade to latest panel

1 Like

.Thanks @nghenzi2019.
I am getting the error when I click on ‘Save Template’ button.

File “/Users/Shaheen/Desktop/Vineet/MS_SE_Spring2019/Panel/file.py”, line 13, in callback
self.files._refresh()
AttributeError: ‘FileSelector’ object has no attribute ‘_refresh’.

Attached is the file I am using for running the code. The panel version in my machine is- ‘0.10.3’

Ifile.py (637 Bytes)

-UPDATE

I updated my panel to ‘0.11.2’ and it works on Jupyter notebook now. However, when I run the file from command line - ‘panel serve file.py’, I get bokeh issue-

Failed to load Bokeh session veIIFdF9NuGVgkeBpPT76gncc4s6ihdvdtc4GSZP7F9w: Error: Lost websocket connection, 1005

Thanks a lot @nghenzi2019. I will try to fix the bokeh issue. Panel issue seems to work fine now.