Skip to content
This repository was archived by the owner on Jul 7, 2023. It is now read-only.

Commit 9d04261

Browse files
authored
Merge pull request #27 from stefan-it/show-download-progress
data-generators: show nice progress bar for download process
2 parents eee7bbf + 6ad92c3 commit 9d04261

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

tensor2tensor/data_generators/generator_utils.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@ def generate_files(generator,
126126
return output_files
127127

128128

129+
def download_report_hook(count, block_size, total_size):
130+
"""Report hook for download progress
131+
132+
Args:
133+
count: current block number
134+
block_size: block size
135+
total_size: total size
136+
"""
137+
percent = int(count*block_size*100/total_size)
138+
print("\r%d%%" % percent + ' completed', end='\r')
139+
140+
129141
def maybe_download(directory, filename, url):
130142
"""Download filename from url unless it's already in directory.
131143
@@ -143,7 +155,11 @@ def maybe_download(directory, filename, url):
143155
filepath = os.path.join(directory, filename)
144156
if not tf.gfile.Exists(filepath):
145157
tf.logging.info("Downloading %s to %s" % (url, filepath))
146-
filepath, _ = urllib.urlretrieve(url, filepath)
158+
filepath, _ = urllib.urlretrieve(url, filepath,
159+
reporthook=download_report_hook)
160+
161+
# Print newline to clear the carriage return from the download progress
162+
print()
147163
statinfo = os.stat(filepath)
148164
tf.logging.info("Succesfully downloaded %s, %s bytes." % (filename,
149165
statinfo.st_size))

0 commit comments

Comments
 (0)