Package: py-nbtools
NBToolsCollection of tools for monitoring running Jupyter Notebooks and interacting with them.The installation should be as easy as:pip install py-nbtoolsNBstatThe main tool of this package isnbstat/nbwatchcommand line utility. It is added at installation and shows the detailed resource utilization for each process of each running Jupyter Notebook. A gif is worth a thousand words:While in thewatchmode, you can hit buttons to modify the displayed view:tab— swaps views, fromnbwatchtodevicewatchand back.b— toggles bar representation for some of the resources: in addition to its value, show colored bar.m— toggles moving average column for some of the resources: values are averaged across the latest iterations.s— toggles table separators.We also add thedevicestatanddevicewatchcommands that show transposed view with the same information and parameters.For more information, check out the fulluser documentation:explanation of different table views, command line options and ready-to-use snippets.Troubleshooting: PID namespaces, user permissions and zombie processesAknown problemof NVIDIA drivers is thatnvidia-smireports PIDs of processes on devices in the global namespace, not in the container namespace, which does not allow to match PIDs of container processes to their device PIDs. There are a few workarounds:pass--pid=hostflag todocker run.patch NVIDIA driver to handle PID namespaces correctly.[Linux only] fallback on manually inspecting/proc/PID/files to identify the host PID for each process inside of the container.Whilenbstatprovides several fallbacks forLinuxcontainers (and intend to provide support for more environments over time), the bullet-proof way is to use--pid=hostoption fordocker run. Adding it resolves most of the issues immediately.One more thing that sometimes happens to NVIDIA devices is zombie processes: by incorrectly terminating a GPU-using process you can end up in a situation where device memory is held by not-existing process. As far as I know, there are no ways of killing them without rebooting, andnbstatjust marks such processes with red color.In order to inspect certain properties of processes, we rely on having all necessary permissions already provided at command run.nbstathas some fallbacks for some attributes, and I currently work on improving error handling in cases of denied access to files.ContributeIf you are interested to contribute, check out thedeveloper/contributor page.It contains detailed description about inner workings of the library, my design choices and motivation behind them, as well as discussion of complexities along the way.pylint_notebookShamelessly taken frompylint page:Function that checks for errors in Jupyter Notebooks with Python code, tries to enforce a coding standard and looks for code smells. It can also look for certain type errors, it can recommend suggestions about how particular blocks can be refactored and can offer you details about the code's complexity.Using it as easy as:fromnbtoolsimportpylint_notebookpylint_notebook(path_to_ipynb,# If not provided, use path to the current notebookdisable='invalid-name',# Disable specified Pylint checks. Can be a list.enable='import-error')# Enable  specified Pylint checks. Can be a list.Under the hood, it converts.ipynbnotebook to.pyscript, creates a custom.pylintrcconfiguration, runs thepylintand removes all temporary files. Learn more about its usage in thetutorial.run_notebookProvides aeval-like interface for running Jupyter Notebooks programmatically. We use it for running interactive tests, that are easy to work with: in case of any failures, one can jump right into fixing it with an already set-up environment.fromnbtoolsimportrun_notebookrun_notebook(path_to_ipynb,# Which notebook to runout_path_ipynb,# Where to save resultinputs={'learning_rate':0.05,},# Pass variables to notebookoutputs=['accuracy'])# Extract variables from notebookset_gpusSelect free device(s) and setCUDA_VISIBLE_DEVICESenvironment variable so that the current process sees only them.Eliminates an enormous amount of bugs and unexpected behaviors related to GPU usage.fromnbtoolsimportset_gpusset_gpus(n=2,# Number of devices to set.min_free_memory=0.7,# Minimum amount of free memory on device to consider it free.max_processes=3)# Maximum amount of  processes  on device to consider it free.Other functionsfromnbtoolsimport(in_notebook,# Return True if executed inside of Jupyter Notebookget_notebook_path,# If executed in Jupyter Notebook, return its absolute pathget_notebook_name,# If executed in Jupyter Notebook, return its namenotebook_to_script)# Convert Jupyter Notebook to an executable Python script.# Works well with magics and command line executions.GoalsThis library started as a container of tools, that I came across / developed in my years as an ML researcher. As some of the functions survived multiple refactoring iterations, I decided to share the library so it is easier to perfect them and test in different environments.Another goal of the project is to show how to communicate with Jupyter API on real world examples: instead of reading through a number of stackoverflow threads, you can find the same information collected in one place and get a rough understanding of what is possible with it and what is not.AcknowledgementsThenbstatmodule builds ongpustatpackage. Using thegpustatfor years gave me an idea about possible improvements, which are implemented in this library. While the implementation is different, reading through the code ofgpustatwas essential for development.Animated GIFs are created by usingTerminalizer: aside from the usual problems with installation, the tool itself is amazing.