{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "62042eef32c9ccdf",
   "metadata": {},
   "source": [
    "# 😙 tqdm\n",
    "\n",
    "`tqdm`是一个 Python 包，它提供了一个简单的进度条，用于在 Python 脚本中显示迭代器的进度。\n",
    "\n",
    "使用`pip`安装："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "21d8f5e945969f87",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-12T05:50:15.362840300Z",
     "start_time": "2023-11-12T05:50:12.248444200Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Requirement already satisfied: tqdm in c:\\users\\mastermao\\anaconda3\\envs\\mastermaopy311\\lib\\site-packages (4.65.0)\n",
      "Requirement already satisfied: colorama in c:\\users\\mastermao\\anaconda3\\envs\\mastermaopy311\\lib\\site-packages (from tqdm) (0.4.6)\n"
     ]
    }
   ],
   "source": [
    "!pip install tqdm"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1bf53f4d8de6309",
   "metadata": {},
   "source": [
    "## 壹丨常见用法\n",
    "\n",
    "### 1. 迭代器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "initial_id",
   "metadata": {
    "tags": [],
    "ExecuteTime": {
     "end_time": "2023-11-12T05:50:27.980471900Z",
     "start_time": "2023-11-12T05:50:27.954438Z"
    }
   },
   "outputs": [],
   "source": [
    "import tqdm\n",
    "import time\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e0180ad7327bcc6e",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-12T05:50:33.791682800Z",
     "start_time": "2023-11-12T05:50:32.748107900Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:01<00:00,  9.89it/s]\n"
     ]
    }
   ],
   "source": [
    "for i in tqdm.tqdm(range(10)):\n",
    "    time.sleep(0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3bdd201050844ea5",
   "metadata": {},
   "source": [
    "### 2. 手动修改输出格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "24ae40f5fd760b6d",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-12T05:50:45.853117700Z",
     "start_time": "2023-11-12T05:50:43.823030800Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "processing d: 100%|██████████| 4/4 [00:02<00:00,  1.99it/s]\n"
     ]
    }
   ],
   "source": [
    "out = tqdm.tqdm(['a', 'b', 'c', 'd'])\n",
    "for item in out:\n",
    "    time.sleep(0.5)\n",
    "    out.set_description(f'processing {item}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1c6b4bb08a592a32",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-12T05:51:21.592068900Z",
     "start_time": "2023-11-12T05:51:20.547513900Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "GEN 9: 100%|██████████| 10/10 [00:01<00:00,  9.73it/s, gen=20, loss=0.965, lst=[1, 2], str=h]\n"
     ]
    }
   ],
   "source": [
    "with tqdm.trange(10) as t:\n",
    "    for i in t:\n",
    "        # 设置进度条左边显示的信息\n",
    "        t.set_description(f\"GEN {i}\")\n",
    "        # 设置进度条右边显示的信息\n",
    "        t.set_postfix(loss=random.random(), gen=random.randint(1, 99), str=\"h\", lst=[1, 2])\n",
    "        time.sleep(0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5e5678253a3b451f",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-12T05:51:32.038659900Z",
     "start_time": "2023-11-12T05:51:26.933210Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Batch     49.5\n"
     ]
    }
   ],
   "source": [
    "with tqdm.tqdm(total=100, bar_format=\"{postfix[0]}{postfix[1][value]:>9.3g}\", postfix=[\"Batch\", dict(value=0)]) as t:\n",
    "    for i in range(100):\n",
    "        time.sleep(0.05)\n",
    "        t.postfix[1][\"value\"] = i / 2\n",
    "        t.update()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25975ba5ff9af71a",
   "metadata": {},
   "source": [
    "### 3. 指定更新size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3f53fb3fbe1ccf27",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-12T05:51:42.216184400Z",
     "start_time": "2023-11-12T05:51:41.188200500Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:01<00:00,  9.88it/s]\n"
     ]
    }
   ],
   "source": [
    "with tqdm.tqdm(total=10) as pbar:\n",
    "    for i in range(10):\n",
    "        time.sleep(0.1)\n",
    "        pbar.update(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "35047bbbe0a2f73d",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-12T05:51:45.201700200Z",
     "start_time": "2023-11-12T05:51:44.172765700Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:01<00:00,  9.88it/s]\n"
     ]
    }
   ],
   "source": [
    "pbar = tqdm.tqdm(total=10)\n",
    "for i in range(10):\n",
    "    time.sleep(0.1)\n",
    "    pbar.update(1)\n",
    "pbar.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1452e627de77c08",
   "metadata": {},
   "source": [
    "### 4. 使用trange替代range"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c2137d99dc258a67",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-12T05:51:50.482765600Z",
     "start_time": "2023-11-12T05:51:49.458553200Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:01<00:00,  9.87it/s]\n"
     ]
    }
   ],
   "source": [
    "for i in tqdm.trange(10):\n",
    "    time.sleep(0.1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
