Skip to content

Commit e62569c

Browse files
committed
made it optional & updated docs
1 parent ca47af1 commit e62569c

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

docs/source/developers/contents.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ Models may contain the following entries:
6161
+--------------------+------------+-------------------------------+
6262
| **item_count** | int or | The number of items in a |
6363
| | ``None`` | directory, or ``None`` for |
64-
| | | files and notebooks. |
64+
| | | files and notebooks. This |
65+
| | | field is None by default |
66+
| | | unless |
67+
| | | ``count_directory_items`` is |
68+
| | | set to True. |
6569
+--------------------+------------+-------------------------------+
6670
| **format** | unicode or | The format of ``content``, |
6771
| | ``None`` | if any. (:ref:`See |
@@ -115,6 +119,8 @@ model. There are three model types: **notebook**, **file**, and **directory**.
115119
models representing the entities in the directory.
116120
- The ``hash`` field is always ``None``.
117121
- The ``item_count`` field contains the number of items in the directory
122+
which is False by default unless ``count_directory_items`` is set to
123+
True.
118124

119125
.. note::
120126

jupyter_server/services/contents/filemanager.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ def _checkpoints_class_default(self):
119119
if safe. And if ``delete_to_trash`` is True, the directory won't be deleted.""",
120120
)
121121

122+
count_directory_items = Bool(
123+
False,
124+
config=True,
125+
help="Whether to count items in directories. Disable for better performance with large/remote directories.",
126+
).tag(config=True)
127+
122128
@default("files_handler_class")
123129
def _files_handler_class_default(self):
124130
return AuthenticatedFileHandler
@@ -296,16 +302,21 @@ def _dir_model(self, path, content=True):
296302
model["size"] = None
297303
os_dir = self._get_os_path(path)
298304
dir_contents = os.listdir(os_dir)
299-
filtered_count = 0
300-
for name in dir_contents:
301-
try:
302-
os_path = os.path.join(os_dir, name)
303-
if self.should_list(name) and (self.allow_hidden or not is_file_hidden(os_path)):
304-
filtered_count += 1
305-
except OSError as e:
306-
self.log.warning("Error accessing %s: %s", os.path.join(os_dir, name), e)
307305

308-
model["item_count"] = filtered_count
306+
if self.count_directory_items:
307+
filtered_count = 0
308+
for name in dir_contents:
309+
try:
310+
os_path = os.path.join(os_dir, name)
311+
if self.should_list(name) and (
312+
self.allow_hidden or not is_file_hidden(os_path)
313+
):
314+
filtered_count += 1
315+
except OSError as e:
316+
self.log.warning("Error accessing %s: %s", os.path.join(os_dir, name), e)
317+
318+
model["item_count"] = filtered_count
319+
309320
if content:
310321
model["content"] = contents = []
311322
for name in os.listdir(os_dir):
@@ -779,16 +790,21 @@ async def _dir_model(self, path, content=True):
779790
model["size"] = None
780791
os_dir = self._get_os_path(path)
781792
dir_contents = await run_sync(os.listdir, os_dir)
782-
filtered_count = 0
783-
for name in dir_contents:
784-
try:
785-
os_path = os.path.join(os_dir, name)
786-
if self.should_list(name) and (self.allow_hidden or not is_file_hidden(os_path)):
787-
filtered_count += 1
788-
except OSError as e:
789-
self.log.warning("Error accessing %s: %s", os.path.join(os_dir, name), e)
790793

791-
model["item_count"] = filtered_count
794+
if self.count_directory_items:
795+
filtered_count = 0
796+
for name in dir_contents:
797+
try:
798+
os_path = os.path.join(os_dir, name)
799+
if self.should_list(name) and (
800+
self.allow_hidden or not is_file_hidden(os_path)
801+
):
802+
filtered_count += 1
803+
except OSError as e:
804+
self.log.warning("Error accessing %s: %s", os.path.join(os_dir, name), e)
805+
806+
model["item_count"] = filtered_count
807+
792808
if content:
793809
model["content"] = contents = []
794810
for name in dir_contents:

tests/services/contents/test_manager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ async def test_modified_date(jp_contents_manager):
549549

550550
async def test_get(jp_contents_manager):
551551
cm = jp_contents_manager
552+
cm.count_directory_items = True
552553
# Create a notebook
553554
model = await ensure_async(cm.new_untitled(type="notebook"))
554555
name = model["name"]

0 commit comments

Comments
 (0)