{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import panel as pn\n",
    "\n",
    "from panel.widgets import Tqdm\n",
    "\n",
    "pn.extension()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The ``Tqdm`` indicator wraps the well known [`tqdm`](https://github.com/tqdm/tqdm) progress indicator and displays the progress towards some target. You can use it in the notebook or in your Panel web app.\n",
    "\n",
    "[![Tqdm](https://raw.githubusercontent.com/tqdm/tqdm/master/images/logo.gif)](https://github.com/tqdm/tqdm)\n",
    "\n",
    "#### Parameters:\n",
    "\n",
    "* **``layout``** (Column or Row): The layout of the `progress` indicator and the `text_pane`.\n",
    "* **``max``** (int): The maximum progress value.\n",
    "* **``progress``** (Progress): The Progress indicator to display the progress on.\n",
    "* **``text``** (int): The current text being output by tqdm.\n",
    "* **``text_pane``** (Str): The Pane displaying the progress `text`.\n",
    "* **``value``** (int or None): The current value towards the progress.\n",
    "* **``write_to_console``** (bool): Whether or not to also write to the console, only works on server.\n",
    "\n",
    "For details on other options for customizing the component see the [layout](../../how_to/layout/index.md) and [styling](../../how_to/styling/index.md) how-to guides. For a general introduction to `tqdm` and lots of examples checkout the [tqdm github page](https://github.com/tqdm/tqdm).\n",
    "\n",
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To use the `Tqdm` indicator instantiate the object and then use the resulting variable just like you would use `tqdm.tqdm`, i.e. you can iterate over any iterable:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tqdm = Tqdm()\n",
    "\n",
    "def run_loop(*events, timeout=0.2):\n",
    "    for i in tqdm(range(0,10), desc=\"My loop bar\", leave=True, colour='#666666'):\n",
    "        if pn.state._is_pyodide:\n",
    "            # time.sleep does not work in pyodide\n",
    "            np.random.random((10**6, 30))  \n",
    "        else:\n",
    "            time.sleep(timeout)\n",
    "        \n",
    "tqdm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Most of the [parameters supported by tqdm](https://github.com/tqdm/tqdm#parameters) can be passed to the call method of the `Tqdm` indicator. \n",
    "\n",
    "Click the button below to see the progress bar update (if you viewing this on a live kernel):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "button = pn.widgets.Button(label=\"Run Loop\", color=\"success\")\n",
    "button.on_click(run_loop)\n",
    "button"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Nesting\n",
    "\n",
    "When nesting `Tqdm` indicators using the `margin` parameter allows visually indicating the level of nesting."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tqdm_outer = Tqdm()\n",
    "tqdm_inner = Tqdm(margin=(0, 0, 0, 20))\n",
    "\n",
    "def run_nested_loop(*events, timeout=0.05):\n",
    "    for i in tqdm_outer(range(10)):\n",
    "        for j in tqdm_inner(range(10)):\n",
    "            if pn.state._is_pyodide:\n",
    "                # time.sleep does not work in pyodide\n",
    "                np.random.random((10**6, 30))  \n",
    "            else:\n",
    "                time.sleep(timeout)\n",
    "            \n",
    "run_nested_loop(timeout=0.01)\n",
    "\n",
    "pn.Column(tqdm_outer, tqdm_inner)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "button = pn.widgets.Button(label=\"Run Nested Loop\", color=\"success\")\n",
    "button.on_click(run_nested_loop)\n",
    "button"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
