#!/usr/bin/env python # coding: utf-8 # In[ ]: import time import numpy as np import pandas as pd import panel as pn from panel.widgets import Tqdm pn.extension() # 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. # # [![Tqdm](https://raw.githubusercontent.com/tqdm/tqdm/master/images/logo.gif)](https://github.com/tqdm/tqdm) # # #### Parameters: # # * **``layout``** (Column or Row): The layout of the `progress` indicator and the `text_pane`. # * **``max``** (int): The maximum progress value. # * **``progress``** (Progress): The Progress indicator to display the progress on. # * **``text``** (int): The current text being output by tqdm. # * **``text_pane``** (Str): The Pane displaying the progress `text`. # * **``value``** (int or None): The current value towards the progress. # * **``write_to_console``** (bool): Whether or not to also write to the console, only works on server. # # For layout and styling related parameters see the [customization user guide](../../user_guide/Customization.ipynb). For a general introduction to `tqdm` and lots of examples checkout the [tqdm github page](https://github.com/tqdm/tqdm). # # ___ # 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: # In[ ]: tqdm = Tqdm() def run_loop(*events, timeout=0.2): for i in tqdm(range(0,10), desc="My loop bar", leave=True, colour='#666666'): time.sleep(timeout) run_loop(timeout=0.01) tqdm # Most of the [parameters supported by tqdm](https://github.com/tqdm/tqdm#parameters) can be passed to the call method of the `Tqdm` indicator. # # Click the button below to see the progress bar update (if you viewing this on a live kernel): # In[ ]: button = pn.widgets.Button(name="Run Loop", button_type="success") button.on_click(run_loop) button # ## Nesting # # When nesting `Tqdm` indicators using the `margin` parameter allows visually indicating the level of nesting. # In[ ]: tqdm_outer = Tqdm() tqdm_inner = Tqdm(margin=(0, 0, 0, 20)) def run_nested_loop(*events, timeout=0.05): for i in tqdm_outer(range(10)): for j in tqdm_inner(range(10)): time.sleep(timeout) run_nested_loop(timeout=0.01) pn.Column(tqdm_outer, tqdm_inner) # In[ ]: button = pn.widgets.Button(name="Run Nested Loop", button_type="success") button.on_click(run_nested_loop) button # ## Pandas # # To use the tqdm pandas integration you can activate it by calling `tqdm.pandas` with all the configuration options. Once activated the `progress_apply` method is available on a `pandas.DataFrame`: # In[ ]: tqdm_pandas = Tqdm(width=500) # Register Pandas. This gives DataFrame.progress_apply method tqdm_pandas.pandas(desc="Pandas Progress") df = pd.DataFrame(np.random.randint(0, 100, (100000, 600))) def run_df(*events): df.progress_apply(lambda x: x**2) run_df() tqdm_pandas # In[ ]: pandas_button = pn.widgets.Button(name="Run Pandas Apply", button_type="success") pandas_button.on_click(run_df) pandas_button