Skip to content

Instantly share code, notes, and snippets.

@parente
Last active May 11, 2019 00:09
Show Gist options
  • Save parente/1f203bd0d7c62a09da3173390bb985fc to your computer and use it in GitHub Desktop.
Save parente/1f203bd0d7c62a09da3173390bb985fc to your computer and use it in GitHub Desktop.
progress example
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's write a little Python file that we'll, first, import as a module in this notebook, and, second, run as a shell script."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%writefile example.py\n",
"import time\n",
"from tqdm.auto import trange\n",
"\n",
"def run():\n",
" for j in trange(0,100):\n",
" time.sleep(0.01)\n",
" \n",
"if __name__ == '__main__':\n",
" run()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`tqdm.auto` uses `ipywidget` progress bars when they are available in the notebook environment. These should never \"leak\" across cells because progress messages are sent directly to the widgets they are meant to update."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import example\n",
"example.run()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In a regular shell environment, `tqdm.auto` uses text to render progress bars. These tend to \"leak\" across cells in a notebook because (without getting into gory details) IPython is unaware of notebook cells and can't direct output to the \"right one\"."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!python example.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If `tqdm` and `ipywidgets` aren't an option (e.g., your notebook environment doesn't have `ipywidgets` plus its frontend Jupyter Notebook or JupyterLab extension installed), there's also a built-in IPython progress bar type.\n",
"\n",
"It's much lower level than `tqdm` and you'll likely need to wrap it for easier use. Also, it doesn't work outside the notebook environment, so you'll need to fall back on a text progress bar display (i.e., hello again, tqdm!) when you detect you're not in a notebook.\n",
"\n",
"I would strongly recommend `tqdm.auto` before falling back to this level."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"from IPython.display import ProgressBar"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pb = ProgressBar(100)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pb.display()\n",
"for i in range(pb.total):\n",
" pb.progress = i\n",
" pb.update()\n",
" time.sleep(0.01)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"More on what you can do with IPython + Jupyter display handle support here: https://mindtrove.info/jupyter-tidbit-display-handles/"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment