|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "8f8f5f2f", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# tqdm\n", |
| 9 | + "\n", |
| 10 | + "Smart progress bars for CLI / Jupyter notebooks\n", |
| 11 | + "\n", |
| 12 | + "https://github.com/tqdm/tqdm" |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "code", |
| 17 | + "execution_count": 1, |
| 18 | + "id": "ecb65344", |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "from time import sleep" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "code", |
| 27 | + "execution_count": 2, |
| 28 | + "id": "5f436f47", |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [ |
| 31 | + { |
| 32 | + "name": "stdout", |
| 33 | + "output_type": "stream", |
| 34 | + "text": [ |
| 35 | + "Done.\n" |
| 36 | + ] |
| 37 | + } |
| 38 | + ], |
| 39 | + "source": [ |
| 40 | + "# Example long(ish) loop:\n", |
| 41 | + "\n", |
| 42 | + "for i in range(50):\n", |
| 43 | + " sleep(0.1)\n", |
| 44 | + "print(\"Done.\")" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": 3, |
| 50 | + "id": "5056269a", |
| 51 | + "metadata": {}, |
| 52 | + "outputs": [], |
| 53 | + "source": [ |
| 54 | + "from tqdm.notebook import tqdm" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "code", |
| 59 | + "execution_count": 4, |
| 60 | + "id": "8043e888", |
| 61 | + "metadata": {}, |
| 62 | + "outputs": [ |
| 63 | + { |
| 64 | + "data": { |
| 65 | + "application/vnd.jupyter.widget-view+json": { |
| 66 | + "model_id": "cd8e4e4c132b44239aa137fb2c5bf4ab", |
| 67 | + "version_major": 2, |
| 68 | + "version_minor": 0 |
| 69 | + }, |
| 70 | + "text/plain": [ |
| 71 | + " 0%| | 0/50 [00:00<?, ?it/s]" |
| 72 | + ] |
| 73 | + }, |
| 74 | + "metadata": {}, |
| 75 | + "output_type": "display_data" |
| 76 | + }, |
| 77 | + { |
| 78 | + "name": "stdout", |
| 79 | + "output_type": "stream", |
| 80 | + "text": [ |
| 81 | + "Done.\n" |
| 82 | + ] |
| 83 | + } |
| 84 | + ], |
| 85 | + "source": [ |
| 86 | + "# Same example with progress bar:\n", |
| 87 | + "\n", |
| 88 | + "for i in tqdm(range(50)):\n", |
| 89 | + " sleep(0.1)\n", |
| 90 | + "print(\"Done.\")" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "code", |
| 95 | + "execution_count": 5, |
| 96 | + "id": "87bed23c", |
| 97 | + "metadata": {}, |
| 98 | + "outputs": [ |
| 99 | + { |
| 100 | + "data": { |
| 101 | + "application/vnd.jupyter.widget-view+json": { |
| 102 | + "model_id": "0b5a82f7a14a4bf3884e5b7183a461d0", |
| 103 | + "version_major": 2, |
| 104 | + "version_minor": 0 |
| 105 | + }, |
| 106 | + "text/plain": [ |
| 107 | + " 0%| | 0/4 [00:00<?, ?it/s]" |
| 108 | + ] |
| 109 | + }, |
| 110 | + "metadata": {}, |
| 111 | + "output_type": "display_data" |
| 112 | + }, |
| 113 | + { |
| 114 | + "name": "stdout", |
| 115 | + "output_type": "stream", |
| 116 | + "text": [ |
| 117 | + "abcd\n" |
| 118 | + ] |
| 119 | + } |
| 120 | + ], |
| 121 | + "source": [ |
| 122 | + "# Wrap any interable:\n", |
| 123 | + "\n", |
| 124 | + "text = \"\"\n", |
| 125 | + "for char in tqdm([\"a\", \"b\", \"c\", \"d\"]):\n", |
| 126 | + " sleep(0.25)\n", |
| 127 | + " text = text + char\n", |
| 128 | + "print(text)\n" |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "code", |
| 133 | + "execution_count": 6, |
| 134 | + "id": "c2ed2b0a", |
| 135 | + "metadata": {}, |
| 136 | + "outputs": [ |
| 137 | + { |
| 138 | + "data": { |
| 139 | + "application/vnd.jupyter.widget-view+json": { |
| 140 | + "model_id": "b82684b4983344778a144bfd148e2e78", |
| 141 | + "version_major": 2, |
| 142 | + "version_minor": 0 |
| 143 | + }, |
| 144 | + "text/plain": [ |
| 145 | + " 0%| | 0/4 [00:00<?, ?it/s]" |
| 146 | + ] |
| 147 | + }, |
| 148 | + "metadata": {}, |
| 149 | + "output_type": "display_data" |
| 150 | + } |
| 151 | + ], |
| 152 | + "source": [ |
| 153 | + "# Customize by instantiating outside loop:\n", |
| 154 | + "\n", |
| 155 | + "pbar = tqdm([\"a\", \"b\", \"c\", \"d\"])\n", |
| 156 | + "for char in pbar:\n", |
| 157 | + " sleep(0.25)\n", |
| 158 | + " pbar.set_description(\"Processing %s\" % char)" |
| 159 | + ] |
| 160 | + }, |
| 161 | + { |
| 162 | + "cell_type": "code", |
| 163 | + "execution_count": 7, |
| 164 | + "id": "0885c8fb", |
| 165 | + "metadata": {}, |
| 166 | + "outputs": [ |
| 167 | + { |
| 168 | + "data": { |
| 169 | + "application/vnd.jupyter.widget-view+json": { |
| 170 | + "model_id": "6f9e95c15fd2444ba426bbcea1bda6c6", |
| 171 | + "version_major": 2, |
| 172 | + "version_minor": 0 |
| 173 | + }, |
| 174 | + "text/plain": [ |
| 175 | + " 0%| | 0/100 [00:00<?, ?it/s]" |
| 176 | + ] |
| 177 | + }, |
| 178 | + "metadata": {}, |
| 179 | + "output_type": "display_data" |
| 180 | + } |
| 181 | + ], |
| 182 | + "source": [ |
| 183 | + "# Manually control updates:\n", |
| 184 | + "\n", |
| 185 | + "with tqdm(total=100) as pbar:\n", |
| 186 | + " for i in range(10):\n", |
| 187 | + " sleep(0.1)\n", |
| 188 | + " pbar.update(10)" |
| 189 | + ] |
| 190 | + }, |
| 191 | + { |
| 192 | + "cell_type": "code", |
| 193 | + "execution_count": 8, |
| 194 | + "id": "c9588103", |
| 195 | + "metadata": {}, |
| 196 | + "outputs": [ |
| 197 | + { |
| 198 | + "data": { |
| 199 | + "application/vnd.jupyter.widget-view+json": { |
| 200 | + "model_id": "2a1f3bfd17e2448d89c2a8496578a818", |
| 201 | + "version_major": 2, |
| 202 | + "version_minor": 0 |
| 203 | + }, |
| 204 | + "text/plain": [ |
| 205 | + "Overall: 0%| | 0/4 [00:00<?, ?it/s]" |
| 206 | + ] |
| 207 | + }, |
| 208 | + "metadata": {}, |
| 209 | + "output_type": "display_data" |
| 210 | + }, |
| 211 | + { |
| 212 | + "data": { |
| 213 | + "application/vnd.jupyter.widget-view+json": { |
| 214 | + "model_id": "", |
| 215 | + "version_major": 2, |
| 216 | + "version_minor": 0 |
| 217 | + }, |
| 218 | + "text/plain": [ |
| 219 | + "Step: 0%| | 0/5 [00:00<?, ?it/s]" |
| 220 | + ] |
| 221 | + }, |
| 222 | + "metadata": {}, |
| 223 | + "output_type": "display_data" |
| 224 | + }, |
| 225 | + { |
| 226 | + "data": { |
| 227 | + "application/vnd.jupyter.widget-view+json": { |
| 228 | + "model_id": "", |
| 229 | + "version_major": 2, |
| 230 | + "version_minor": 0 |
| 231 | + }, |
| 232 | + "text/plain": [ |
| 233 | + "Step: 0%| | 0/5 [00:00<?, ?it/s]" |
| 234 | + ] |
| 235 | + }, |
| 236 | + "metadata": {}, |
| 237 | + "output_type": "display_data" |
| 238 | + }, |
| 239 | + { |
| 240 | + "data": { |
| 241 | + "application/vnd.jupyter.widget-view+json": { |
| 242 | + "model_id": "", |
| 243 | + "version_major": 2, |
| 244 | + "version_minor": 0 |
| 245 | + }, |
| 246 | + "text/plain": [ |
| 247 | + "Step: 0%| | 0/5 [00:00<?, ?it/s]" |
| 248 | + ] |
| 249 | + }, |
| 250 | + "metadata": {}, |
| 251 | + "output_type": "display_data" |
| 252 | + }, |
| 253 | + { |
| 254 | + "data": { |
| 255 | + "application/vnd.jupyter.widget-view+json": { |
| 256 | + "model_id": "", |
| 257 | + "version_major": 2, |
| 258 | + "version_minor": 0 |
| 259 | + }, |
| 260 | + "text/plain": [ |
| 261 | + "Step: 0%| | 0/5 [00:00<?, ?it/s]" |
| 262 | + ] |
| 263 | + }, |
| 264 | + "metadata": {}, |
| 265 | + "output_type": "display_data" |
| 266 | + } |
| 267 | + ], |
| 268 | + "source": [ |
| 269 | + "from tqdm.auto import trange\n", |
| 270 | + "\n", |
| 271 | + "for i in trange(4, desc='Overall'):\n", |
| 272 | + " for j in trange(5, desc='Step', leave=False):\n", |
| 273 | + " sleep(0.2)" |
| 274 | + ] |
| 275 | + }, |
| 276 | + { |
| 277 | + "cell_type": "code", |
| 278 | + "execution_count": null, |
| 279 | + "id": "8e0f8ac5", |
| 280 | + "metadata": {}, |
| 281 | + "outputs": [], |
| 282 | + "source": [] |
| 283 | + } |
| 284 | + ], |
| 285 | + "metadata": { |
| 286 | + "kernelspec": { |
| 287 | + "display_name": "Python 3 (ipykernel)", |
| 288 | + "language": "python", |
| 289 | + "name": "python3" |
| 290 | + }, |
| 291 | + "language_info": { |
| 292 | + "codemirror_mode": { |
| 293 | + "name": "ipython", |
| 294 | + "version": 3 |
| 295 | + }, |
| 296 | + "file_extension": ".py", |
| 297 | + "mimetype": "text/x-python", |
| 298 | + "name": "python", |
| 299 | + "nbconvert_exporter": "python", |
| 300 | + "pygments_lexer": "ipython3", |
| 301 | + "version": "3.9.5" |
| 302 | + } |
| 303 | + }, |
| 304 | + "nbformat": 4, |
| 305 | + "nbformat_minor": 5 |
| 306 | +} |
0 commit comments