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

Commit

Permalink
Merge pull request #27 from stefan-it/show-download-progress
Browse files Browse the repository at this point in the history
data-generators: show nice progress bar for download process
  • Loading branch information
lukaszkaiser authored Jun 23, 2017
2 parents eee7bbf + 6ad92c3 commit 9d04261
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion tensor2tensor/data_generators/generator_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ def generate_files(generator,
return output_files


def download_report_hook(count, block_size, total_size):
"""Report hook for download progress
Args:
count: current block number
block_size: block size
total_size: total size
"""
percent = int(count*block_size*100/total_size)
print("\r%d%%" % percent + ' completed', end='\r')


def maybe_download(directory, filename, url):
"""Download filename from url unless it's already in directory.
Expand All @@ -143,7 +155,11 @@ def maybe_download(directory, filename, url):
filepath = os.path.join(directory, filename)
if not tf.gfile.Exists(filepath):
tf.logging.info("Downloading %s to %s" % (url, filepath))
filepath, _ = urllib.urlretrieve(url, filepath)
filepath, _ = urllib.urlretrieve(url, filepath,
reporthook=download_report_hook)

# Print newline to clear the carriage return from the download progress
print()
statinfo = os.stat(filepath)
tf.logging.info("Succesfully downloaded %s, %s bytes." % (filename,
statinfo.st_size))
Expand Down

0 comments on commit 9d04261

Please sign in to comment.