dharhas
November 20, 2024, 1:47pm
1
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:
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'
Hoxbro
November 21, 2024, 7:13am
3
Can one of you fike a bug report on Github?
dharhas
November 21, 2024, 1:22pm
4
@pierrotsmnrd opened an issue:
opened 08:56AM - 21 Nov 24 UTC
#### ALL software version info
<details>
<summary>Software Version Info</sum… mary>
```plaintext
panel==1.5.4
MacOS 14.6.1
```
</details>
#### Description of expected behavior and the observed behavior
- A FileSelector is first displayed with property `visible=False`
- On a click on a button, its `visible` property is set to `True`
- The FileSelector is not entirely displayed, only the horizontal line is shown
#### Complete, minimal, self-contained example code that reproduces the issue
```python
import panel as pn
pn.extension()
file_selector = pn.widgets.FileSelector('~/', name='Select Files', visible=False)
toggle_button = pn.widgets.Button(name='Show File Selector', button_type='primary')
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'
toggle_button.on_click(toggle_visibility)
layout = pn.panel(pn.Column(toggle_button, file_selector))
layout.servable()
```
#### Diagnostic
FileSelector is a composite widget. After investigating the HTML structure, it appears that several components inside the `_composite` property remain with `visible=False` despite setting the `FileSelector`'s `visible` property to `True`
#### Workaround
Updating the `toggle_visibility` function to this illustrates the diagnostic and gives the expected behavior
```python
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'
```
#### Stack traceback and/or browser JavaScript console output
None
#### Screenshots or screencasts of the bug in action
https://github.com/user-attachments/assets/8855d4f7-60ce-4f1d-ab11-9ae15b21dede
- [X] I may be interested in making a pull request to address this (I just don't know exactly where the problem lies in the code, despite investigating. I need a hint :))
1 Like