kopia lustrzana https://github.com/crahan/ipyfilechooser
Merge pull request #60 from crahan/width_and_fixes
Configure width, folder path sanitizing, cleaner layout, support value propertymaster
commit
0cfe3bd77e
|
@ -1,16 +1,28 @@
|
||||||
#Ignore vscode individual settings
|
# Ignore macOS .DS_Store files
|
||||||
.vscode
|
.DS_Store
|
||||||
|
|
||||||
#Ignore egg-info files and folders
|
# Ignore backup files
|
||||||
*.egg-info
|
*~
|
||||||
|
|
||||||
|
# Ignore vscode individual settings
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
# Ignore egg-info files and folders
|
||||||
|
*.egg-info/
|
||||||
*.pyc
|
*.pyc
|
||||||
|
|
||||||
#Ignore pyenv version files
|
# Ignore pyenv version files
|
||||||
.python-version
|
.python-version
|
||||||
|
|
||||||
#Ignore build and dist folders
|
# Ignore build and dist folders
|
||||||
build/*
|
build/
|
||||||
dist/*
|
dist/
|
||||||
|
|
||||||
# Jupyter notebook checkpoints
|
# Ignore __pycache__ folder
|
||||||
.ipynb_checkpoints
|
__pycache__/
|
||||||
|
|
||||||
|
# Ignore Python virtualenv folder
|
||||||
|
venv/
|
||||||
|
|
||||||
|
# Ignore Jupyter notebook checkpoints
|
||||||
|
.ipynb_checkpoints/
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
import os
|
import os
|
||||||
from typing import Optional, Sequence, Mapping, Callable
|
from typing import Optional, Sequence, Mapping, Callable
|
||||||
from ipywidgets import Dropdown, Text, Select, Button, HTML
|
from ipywidgets import Dropdown, Text, Select, Button, HTML
|
||||||
from ipywidgets import Layout, GridBox, HBox, VBox, ValueWidget
|
from ipywidgets import Layout, GridBox, Box, HBox, VBox, ValueWidget
|
||||||
from .utils import get_subpaths, get_dir_contents, match_item
|
from .utils import get_subpaths, get_dir_contents, match_item
|
||||||
|
|
||||||
|
|
||||||
class FileChooser(VBox, ValueWidget):
|
class FileChooser(VBox, ValueWidget):
|
||||||
"""FileChooser class."""
|
"""FileChooser class."""
|
||||||
|
|
||||||
_LBL_TEMPLATE = '<span style="margin-left:10px; color:{1};">{0}</span>'
|
_LBL_TEMPLATE = '<span style="color:{1};">{0}</span>'
|
||||||
_LBL_NOFILE = 'No file selected'
|
_LBL_NOFILE = 'No file selected'
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
|
@ -23,9 +23,10 @@ class FileChooser(VBox, ValueWidget):
|
||||||
use_dir_icons: bool = False,
|
use_dir_icons: bool = False,
|
||||||
show_only_dirs: bool = False,
|
show_only_dirs: bool = False,
|
||||||
filter_pattern: Optional[Sequence[str]] = None,
|
filter_pattern: Optional[Sequence[str]] = None,
|
||||||
|
layout: Layout = Layout(width='500px'),
|
||||||
**kwargs):
|
**kwargs):
|
||||||
"""Initialize FileChooser object."""
|
"""Initialize FileChooser object."""
|
||||||
self._default_path = path.rstrip(os.path.sep)
|
self._default_path = os.path.normpath(path)
|
||||||
self._default_filename = filename
|
self._default_filename = filename
|
||||||
self._selected_path = None
|
self._selected_path = None
|
||||||
self._selected_filename = None
|
self._selected_filename = None
|
||||||
|
@ -65,13 +66,17 @@ class FileChooser(VBox, ValueWidget):
|
||||||
self._cancel = Button(
|
self._cancel = Button(
|
||||||
description='Cancel',
|
description='Cancel',
|
||||||
layout=Layout(
|
layout=Layout(
|
||||||
width='auto',
|
min_width='6em',
|
||||||
|
width='6em',
|
||||||
display='none'
|
display='none'
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self._select = Button(
|
self._select = Button(
|
||||||
description=self._select_desc,
|
description=self._select_desc,
|
||||||
layout=Layout(width='auto')
|
layout=Layout(
|
||||||
|
min_width='6em',
|
||||||
|
width='6em'
|
||||||
|
)
|
||||||
)
|
)
|
||||||
self._title = HTML(
|
self._title = HTML(
|
||||||
value=title
|
value=title
|
||||||
|
@ -91,7 +96,8 @@ class FileChooser(VBox, ValueWidget):
|
||||||
self._label = HTML(
|
self._label = HTML(
|
||||||
value=self._LBL_TEMPLATE.format(self._LBL_NOFILE, 'black'),
|
value=self._LBL_TEMPLATE.format(self._LBL_NOFILE, 'black'),
|
||||||
placeholder='',
|
placeholder='',
|
||||||
description=''
|
description='',
|
||||||
|
layout=Layout(margin='0 0 0 1em')
|
||||||
)
|
)
|
||||||
|
|
||||||
# Layout
|
# Layout
|
||||||
|
@ -103,7 +109,7 @@ class FileChooser(VBox, ValueWidget):
|
||||||
],
|
],
|
||||||
layout=Layout(
|
layout=Layout(
|
||||||
display='none',
|
display='none',
|
||||||
width='500px',
|
width='auto',
|
||||||
grid_gap='0px 0px',
|
grid_gap='0px 0px',
|
||||||
grid_template_rows='auto auto',
|
grid_template_rows='auto auto',
|
||||||
grid_template_columns='60% 40%',
|
grid_template_columns='60% 40%',
|
||||||
|
@ -118,7 +124,7 @@ class FileChooser(VBox, ValueWidget):
|
||||||
children=[
|
children=[
|
||||||
self._select,
|
self._select,
|
||||||
self._cancel,
|
self._cancel,
|
||||||
self._label
|
Box([self._label], layout=Layout(overflow='auto'))
|
||||||
],
|
],
|
||||||
layout=Layout(width='auto')
|
layout=Layout(width='auto')
|
||||||
)
|
)
|
||||||
|
@ -137,7 +143,7 @@ class FileChooser(VBox, ValueWidget):
|
||||||
self._gb,
|
self._gb,
|
||||||
buttonbar
|
buttonbar
|
||||||
],
|
],
|
||||||
layout=Layout(width='auto'),
|
layout=layout,
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -318,7 +324,7 @@ class FileChooser(VBox, ValueWidget):
|
||||||
self._label.value = self._LBL_TEMPLATE.format(self._LBL_NOFILE, 'black')
|
self._label.value = self._LBL_TEMPLATE.format(self._LBL_NOFILE, 'black')
|
||||||
|
|
||||||
if path is not None:
|
if path is not None:
|
||||||
self._default_path = path.rstrip(os.path.sep)
|
self._default_path = os.path.normpath(path)
|
||||||
|
|
||||||
if filename is not None:
|
if filename is not None:
|
||||||
self._default_filename = filename
|
self._default_filename = filename
|
||||||
|
@ -399,7 +405,7 @@ class FileChooser(VBox, ValueWidget):
|
||||||
@default_path.setter
|
@default_path.setter
|
||||||
def default_path(self, path: str) -> None:
|
def default_path(self, path: str) -> None:
|
||||||
"""Set the default_path."""
|
"""Set the default_path."""
|
||||||
self._default_path = path.rstrip(os.path.sep)
|
self._default_path = os.path.normpath(path)
|
||||||
self._set_form_values(self._default_path, self._filename.value)
|
self._set_form_values(self._default_path, self._filename.value)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -453,6 +459,11 @@ class FileChooser(VBox, ValueWidget):
|
||||||
self._filter_pattern = filter_pattern
|
self._filter_pattern = filter_pattern
|
||||||
self.refresh()
|
self.refresh()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def value(self) -> Optional[str]:
|
||||||
|
"""Get selected value."""
|
||||||
|
return self.selected
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def selected(self) -> Optional[str]:
|
def selected(self) -> Optional[str]:
|
||||||
"""Get selected value."""
|
"""Get selected value."""
|
||||||
|
|
Ładowanie…
Reference in New Issue