{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import time\n",
    "from tqdm.auto import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.normal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e6bc35acd9d54af68261dc1ea0cd2e86",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(IntProgress(value=0, max=5), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "0\n",
      "a\n",
      "1\n",
      "aa\n",
      "2\n",
      "aaa\n",
      "3\n",
      "aaaa\n",
      "4\n",
      "\n"
     ]
    }
   ],
   "source": [
    "bar = tqdm(range(5))\n",
    "for i in bar:\n",
    "    time.sleep(1)\n",
    "    tqdm.write('a'*i)\n",
    "    bar.set_description(f'loss={np.random.normal()/10:.4f}')\n",
    "    bar.set_postfix(loss=f'after {i}')\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# STACKOVERFLOW\n",
    "\n",
    "import inspect\n",
    "import tqdm\n",
    "# store builtin print\n",
    "old_print = print\n",
    "def new_print(*args, **kwargs):\n",
    "    # if tqdm.tqdm.write raises error, use builtin print\n",
    "    try:\n",
    "        tqdm.tqdm.write(*args, **kwargs)\n",
    "    except:\n",
    "        old_print(*args, ** kwargs)\n",
    "# globaly replace print with new_print\n",
    "inspect.builtins.print = new_print"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
