Created
May 1, 2021 10:35
-
-
Save erdmann/713f0d3e28407ad94d73714e8af7083a to your computer and use it in GitHub Desktop.
Using ipywidgets and asyncio, it's easy to tap into the existing asynchronous event loop in Jupyter to make a cell that periodically monitors the output of a shell command (such as nvidia-smi) while you go about your work.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import asyncio\n", | |
"import subprocess\n", | |
"import ipywidgets" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def watch(cmd, interval=1, out=None):\n", | |
" '''Monitor the output of a shell command in a Jupyter Notebook asynchronously.\n", | |
" '''\n", | |
" out = out or ipywidgets.Output()\n", | |
"\n", | |
" async def run():\n", | |
" while True:\n", | |
" stdout = subprocess.run(cmd.split(), capture_output=True).stdout.decode()\n", | |
" with out:\n", | |
" display(\n", | |
" ipywidgets.HTML(\n", | |
" f'<pre style=\"font-size: x-small; line-height: 1em\">{stdout}</pre>'\n", | |
" )\n", | |
" )\n", | |
" out.clear_output(wait=True) # clear upon receiving next output\n", | |
" await asyncio.sleep(interval)\n", | |
"\n", | |
" task = asyncio.get_event_loop().create_task(run())\n", | |
" return out, task" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "806fe2fe65c84666af671c0cc59d251b", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"Output()" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"out, task = watch('nvidia-smi') # run task.cancel() to stop.\n", | |
"out" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# out, task = watch('df -H')\n", | |
"# out, task = watch('ls -lt')\n", | |
"# etc." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"@webio": { | |
"lastCommId": null, | |
"lastKernelId": null | |
}, | |
"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.8.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment