Guest User

Untitled

a guest
Sep 25th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. {
  2. "metadata": {
  3. "name": "progbar"
  4. },
  5. "nbformat": 3,
  6. "worksheets": [
  7. {
  8. "cells": [
  9. {
  10. "cell_type": "code",
  11. "collapsed": true,
  12. "input": [
  13. "import sys, time",
  14. "try:",
  15. " from IPython.core.display import clear_output",
  16. " have_ipython = True",
  17. "except ImportError:",
  18. " have_ipython = False",
  19. "",
  20. "class ProgressBar:",
  21. " def __init__(self, iterations):",
  22. " self.iterations = iterations",
  23. " self.prog_bar = '[]'",
  24. " self.fill_char = '*'",
  25. " self.width = 40",
  26. " self.__update_amount(0)",
  27. " if have_ipython:",
  28. " self.animate = self.animate_ipython",
  29. " else:",
  30. " self.animate = self.animate_noipython",
  31. "",
  32. " def animate_ipython(self, iter):",
  33. " try:",
  34. " clear_output()",
  35. " except Exception:",
  36. " # terminal IPython has no clear_output",
  37. " pass",
  38. " print '\\r', self,",
  39. " sys.stdout.flush()",
  40. " self.update_iteration(iter + 1)",
  41. "",
  42. " def update_iteration(self, elapsed_iter):",
  43. " self.__update_amount((elapsed_iter / float(self.iterations)) * 100.0)",
  44. " self.prog_bar += ' %d of %s complete' % (elapsed_iter, self.iterations)",
  45. "",
  46. " def __update_amount(self, new_amount):",
  47. " percent_done = int(round((new_amount / 100.0) * 100.0))",
  48. " all_full = self.width - 2",
  49. " num_hashes = int(round((percent_done / 100.0) * all_full))",
  50. " self.prog_bar = '[' + self.fill_char * num_hashes + ' ' * (all_full - num_hashes) + ']'",
  51. " pct_place = (len(self.prog_bar) / 2) - len(str(percent_done))",
  52. " pct_string = '%d%%' % percent_done",
  53. " self.prog_bar = self.prog_bar[0:pct_place] + \\",
  54. " (pct_string + self.prog_bar[pct_place + len(pct_string):])",
  55. "",
  56. " def __str__(self):",
  57. " return str(self.prog_bar)"
  58. ],
  59. "language": "python",
  60. "outputs": [],
  61. "prompt_number": 19
  62. },
  63. {
  64. "cell_type": "code",
  65. "collapsed": false,
  66. "input": [
  67. "p = ProgressBar(1000)",
  68. "for i in range(1001):",
  69. " p.animate(i)",
  70. "# time.sleep(0.01)"
  71. ],
  72. "language": "python",
  73. "outputs": [],
  74. "prompt_number": 20
  75. },
  76. {
  77. "cell_type": "code",
  78. "collapsed": false,
  79. "input": [
  80. "import uuid",
  81. "from IPython.core.display import HTML, Javascript, display",
  82. "",
  83. "divid = str(uuid.uuid4())",
  84. "",
  85. "pb = HTML(",
  86. "\"\"\"",
  87. "<div style=\"border: 1px solid black; width:500px\">",
  88. " <div id=\"%s\" style=\"background-color:blue; width:0%%\">&nbsp;</div>",
  89. "</div> ",
  90. "\"\"\" % divid)",
  91. "display(pb)",
  92. "for i in range(1,101):",
  93. " time.sleep(0.1)",
  94. " ",
  95. " display(Javascript(\"$('div#%s').width('%i%%')\" % (divid, i)))"
  96. ],
  97. "language": "python",
  98. "outputs": [],
  99. "prompt_number": 21
  100. }
  101. ]
  102. }
  103. ]
  104. }
Add Comment
Please, Sign In to add comment