You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
documentation request (i.e. "X is missing from the documentation." If instead I want to ask "how to use X?" I understand [StackOverflow#tqdm] is more appropriate)
new feature request
I have visited the [source website], and in particular
read the [known issues]
I have searched through the [issue tracker] for duplicates
I have mentioned version numbers, operating system and
environment, where applicable:
4.66.1 3.12.0 | packaged by conda-forge | (main, Oct 3 2023, 08:43:22) [GCC 12.3.0] linux
Hi! I already posted a thread on SO but I think I have to specify my question further. I would like to separate the internal logic of my function from the optional progress output using tqdm. Specifically I would like to print the current amount of data copied if wished by the user. For that, it would be nice, if I could use tqdm(my_iterable) to "activate" the progress bar but then also pass a second iterable to it (cumulative file sizes) that is used to show the progress. Here's an example:
importosimportshutilimporttimefromtqdmimporttqdmimportnumpyasnp################################################################################# Example Preparation ################################################################################ start with a clean directoryifos.path.isdir('./src'):
shutil.rmtree('./src')
ifos.path.isdir('./dst'):
shutil.rmtree('./dst')
# create test files with different sizesos.makedirs('./src')
foriinrange(10):
withopen(f"./src/{i}", 'wb') asbigfile:
bigfile.seek(1048575*i)
bigfile.write(b'0')
# get list of filepaths and list of destination directories where these files# should be copied tofiles= [os.path.join('./src', file) forfileinos.listdir('./src')]
dst_dirs= ['./dst'foriinrange(len(files))]
# get cumulative sizes of files (in percentage)filesizes= [os.path.getsize(file) forfileinfiles]
bytes_total=sum(filesizes)
cumulative_sizes=list(np.cumsum(filesizes) /bytes_total*100)
################################################################################# Actual Question################################################################################ this function should NOT use tqdm internally (separate logic of function# from displaying user information)defcopy_files(files,dst_dirs):
'''Copy files based on a list of source filepaths and a corresponding list of destination directories. '''fordst_dirinset(dst_dirs):
os.makedirs(dst_dir)
forfile,dst_dirinzip(files,dst_dirs):
shutil.copy2(file,dst_dir)
# just for dev purposes so we can actually see the outputtime.sleep(0.2)
# set verbosityverbose=Trueifverbose==True:
# would be nice if we also could pass cumulative_sizes to tqdm() so it can # display the current progress of copied datacopy_files(tqdm(files,desc='Copy files'), dst_dirs)
elifverbose==False:
copy_files(files, dst_dirs)
The text was updated successfully, but these errors were encountered:
I think a more general solution is to pass in a lambda expression (or function) which takes the current item as the argument. For example,
for i in tqdm(range(100), display_current=lambda t:f'Item{t}'):
# do task
@JohannesWiesner, it is too restrictive to pass in a list for displaying the current item because tqdm does allow passing in an iterator, in which case the actual label name of subsequent items might not be ready at the time when you call tqdm() function.
read the [known issues]
environment, where applicable:
Hi! I already posted a thread on SO but I think I have to specify my question further. I would like to separate the internal logic of my function from the optional progress output using tqdm. Specifically I would like to print the current amount of data copied if wished by the user. For that, it would be nice, if I could use
tqdm(my_iterable)
to "activate" the progress bar but then also pass a second iterable to it (cumulative file sizes) that is used to show the progress. Here's an example:The text was updated successfully, but these errors were encountered: