Skip to content

Commit 50bd25e

Browse files
Implement filename-based default pin titles for .pin_upload
1 parent bdb58e1 commit 50bd25e

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

pins/boards.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,30 @@ def pin_upload(
491491
if not Path(path).is_file():
492492
raise PinsError(f"Path is not a valid file: {path}")
493493

494+
if title is None:
495+
# Use the filename/s as the name.
496+
# Otherwise, the title ends up being something like '...: a pinned str object'
497+
# https://github.com/rstudio/pins-python/issues/346
498+
if len(paths) == 1:
499+
filename = Path(paths[0]).name
500+
title = f"'{filename}': a pinned file"
501+
else:
502+
# Don't let the string get too long; if the number of characters is
503+
# excessive, then just use ... to represent the rest of them
504+
file_list_str = ""
505+
for p in paths:
506+
filename = Path(p).name
507+
508+
if len(file_list_str) > 30:
509+
file_list_str += ", ..."
510+
break
511+
512+
if file_list_str:
513+
file_list_str += ", "
514+
file_list_str += f"'{filename}'"
515+
516+
title = f"{file_list_str}: {len(paths)} pinned files"
517+
494518
return self._pin_store(
495519
paths,
496520
name,

pins/tests/test_boards.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,50 @@ def test_board_pin_upload_path_list(board_with_cache, tmp_path):
281281
(pin_path,) = board_with_cache.pin_download("cool_pin")
282282

283283

284+
@skip_if_dbc
285+
def test_board_pin_upload_name(board_with_cache, tmp_path):
286+
# create and save data
287+
df = pd.DataFrame({"x": [1, 2, 3]})
288+
289+
path = tmp_path / "data.csv"
290+
df.to_csv(path, index=False)
291+
292+
meta = board_with_cache.pin_upload([path], "cool_pin")
293+
294+
assert meta.title == "'data.csv': a pinned file"
295+
296+
297+
@skip_if_dbc
298+
def test_board_pin_upload_name_multiple_paths(board_with_cache, tmp_path):
299+
# create and save data
300+
df = pd.DataFrame({"x": [1, 2, 3]})
301+
302+
path1 = tmp_path / "data1.csv"
303+
path2 = tmp_path / "data2.csv"
304+
df.to_csv(path1, index=False)
305+
df.to_csv(path2, index=False)
306+
307+
meta = board_with_cache.pin_upload([path1, path2], "cool_pin")
308+
309+
assert meta.title == "'data1.csv', 'data2.csv': 2 pinned files"
310+
311+
312+
@skip_if_dbc
313+
def test_board_pin_upload_name_many_paths(board_with_cache, tmp_path):
314+
# create and save data
315+
df = pd.DataFrame({"x": [1, 2, 3]})
316+
317+
paths = []
318+
for i in range(1, 4 + 1):
319+
path = tmp_path / f"data{i}.csv"
320+
df.to_csv(path, index=False)
321+
paths.append(path)
322+
323+
meta = board_with_cache.pin_upload(paths, "cool_pin")
324+
325+
assert meta.title == "'data1.csv', 'data2.csv', 'data3.csv', ...: 4 pinned files"
326+
327+
284328
@skip_if_dbc
285329
def test_board_pin_download_filename_multifile(board_with_cache, tmp_path):
286330
# create and save data

0 commit comments

Comments
 (0)