FileSelector widget doesn't display dynamically if `visible` kwarg is initially set as False

So I have this code (written by chatgpt) that I am trying to get working. Essentially, I want to show or hide the FileSelector widget based on a button press. Eventually, I will use this to let someone choose a file and return its path but this is minimal code to show functionality.

In the code below, if I start with visible=False

file_selector = pn.widgets.FileSelector('~/', name='Select Files', visible=False)

I do not see the FileSelector when I press the button, just a line:

image

If I start with visible=True, then the button works as expected, showing/hiding the FileSelector each time I click.

note: this is with Panel 1.4.5, I will try updating panel and trying again.

import panel as pn

# Initialize Panel extension
pn.extension()

# Create a FileSelector widget
file_selector = pn.widgets.FileSelector('~/', name='Select Files', visible=False)

# Create a Button widget
toggle_button = pn.widgets.Button(name='Show File Selector', button_type='primary')

# Define the button callback
def toggle_visibility(event):
    file_selector.visible = not file_selector.visible
    toggle_button.name = 'Hide File Selector' if file_selector.visible else 'Show File Selector'

# Attach the callback to the button
toggle_button.on_click(toggle_visibility)

# Create the layout
layout = pn.Column(toggle_button, file_selector)

# Show the app
layout.servable()

This seems to be a bug in Panel. with visible=False at start, changing the visibility of the FileSelector doesn’t change the visibility of all the underlying widgets in the _composite structure. Quick workaround :

def toggle_visibility(event):
    file_selector.visible = not file_selector.visible
    file_selector._composite[0].visible =  file_selector.visible
    file_selector._composite[-1].visible = file_selector.visible
    toggle_button.name = 'Hide File Selector' if file_selector.visible else 'Show File Selector'

Can one of you fike a bug report on Github?

@pierrotsmnrd opened an issue:

1 Like