IPyFileChooser
IPyFileChooser is a Python file chooser widget designed for use in Jupyter/IPython notebooks, integrating with `ipywidgets`. It allows users to interactively select file paths and filenames. The library is actively maintained, with its latest stable version 0.6.0 released in September 2021.
Common errors
-
Failed to load model class 'VBoxModel' from module '@jupyter-widgets/controls' (or widget simply not displaying).
cause Jupyter Notebook/Lab extensions for ipywidgets are not correctly installed or enabled, or JupyterLab needs a rebuild.fixFor Jupyter Notebook: `jupyter nbextension enable --py --sys-prefix widgetsnbextension`. For JupyterLab: `jupyter labextension install @jupyter-widgets/jupyterlab-manager` and then `jupyter lab build`. In Docker environments, building JupyterLab within the Dockerfile often solves this. -
AttributeError: 'FileChooser' object has no attribute 'use_dir_icons'
cause Attempting to use the `use_dir_icons` property which was removed in `v0.6.0`.fixUse `dir_icon` and `dir_icon_append` instead. Example: `fc.dir_icon = '๐'` and `fc.dir_icon_append = True` to customize folder icons. -
FileNotFoundError: [Errno 2] No such file or directory: '/non_existent_path'
cause The `default_path`, `sandbox_path`, or other path provided to `FileChooser` does not exist or is not accessible by the Jupyter kernel's user.fixVerify that the specified path exists and has appropriate read/write permissions for the user running the Jupyter kernel. Use `os.path.exists()` to check programmatically before initializing `FileChooser`.
Warnings
- breaking In `v0.6.0`, the `use_dir_icons` property was replaced by `dir_icon` and `dir_icon_append` for customizing folder icons. Direct usage of `use_dir_icons` will result in an `AttributeError`.
- gotcha For the widget to display correctly in Jupyter environments (Notebook or Lab), `ipywidgets` extensions must be properly enabled. In JupyterLab, a rebuild might be necessary after installation.
- gotcha Since `v0.4.4`, `filter_pattern` values are treated as case-insensitive. This behavior might differ from expectations on case-sensitive file systems if strict case matching is desired.
- deprecated If using `ipywidgets.interact` with `ipyfilechooser`, the interact functionality might not update as expected due to significant changes in `ipywidgets` since `ipyfilechooser`'s last major updates.
Install
-
pip install ipyfilechooser -
conda install -c conda-forge ipyfilechooser
Imports
- FileChooser
from ipyfilechooser import FileChooser
Quickstart
import os
from ipyfilechooser import FileChooser
from IPython.display import display
# Get the current working directory as the default path
current_dir = os.getcwd()
# Create and display a FileChooser widget
# You can specify a default path, filename, and filter patterns
fc = FileChooser(current_dir, title='Choose a file:')
fc.filter_pattern = ['*.txt', '*.csv'] # Example: filter for text and CSV files
display(fc)
# To access the selected path and filename after user interaction
# For example, after a button click or callback
# print(f"Selected Path: {fc.selected_path}")
# print(f"Selected Filename: {fc.selected_filename}")
# print(f"Full Selected File: {fc.selected}")
# Example of using a callback function
def on_selection_change(chooser):
print(f"Callback triggered! Selected: {chooser.selected}")
fc.register_callback(on_selection_change)