treebeardtech / nbmake

📝 Pytest plugin for testing notebooks
https://pypi.org/project/nbmake/
Apache License 2.0
184 stars 18 forks source link

Ignore Specific Cells when Running nbmake with PyTest #59

Closed alexheat closed 2 years ago

alexheat commented 2 years ago

Is your feature request related to a problem? Please describe. It is possible to configure a notebook so that some cells are ignored during the automated test? For example at the bottom of my notebook I have some code that will train a deep learning model and I want to skip that during the testing. The feature is discussed hear but I don't see how to do it https://github.com/treebeardtech/nbmake-action/issues/58

Describe the solution you'd like Ability to decorate a notebook cell so that it is note executed by nbmake+pytest, but otherwise looks normal when run interactively.

Describe alternatives you've considered Creating 2 versions of the notebook, one with and without the troublesome code. But that will be more difficult to maintain.

Additional context Add any other context or screenshots about the feature request here.

alex-treebeard commented 2 years ago

Hi @alexheat, how would you like to decorate a cell to be not executed?

A better alternative to 2 notebooks may be if not 'CI' in os.environ: train() depending on your presentation standards.

alexheat commented 2 years ago

Hello @alex-treebeard , I was imagining to add metadata (json) to a cell like "nbmake_ignore: True" and then it would be ignored.

I did not quite understand this suggestion 'if not 'CI' in os.environ: train()' Would that be something that I add into the code cell? os.environ: train() returns an error. This is whole output of os.environment on Google collab. Will one of these fields identify if the notebook is run from nbmake? (If so this could work but I would prefer something that is invisible because the notebook is part of my public product documentation and I want to make it look as simple as possible.

'environ({'NO_GCE_CHECK': 'True', 'GCS_READ_CACHE_BLOCK_SIZE_MB': '16', 'CLOUDSDK_CONFIG': '/content/.config', 'CUDA_VERSION': '11.1.1', 'PATH': '/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/tools/node/bin:/tools/google-cloud-sdk/bin:/opt/bin', 'HOME': '/root', 'LD_LIBRARY_PATH': '/usr/local/nvidia/lib:/usr/local/nvidia/lib64', 'LANG': 'en_US.UTF-8', 'SHELL': '/bin/bash', 'LIBRARY_PATH': '/usr/local/cuda/lib64/stubs', 'SHLVL': '0', 'GCE_METADATA_TIMEOUT': '0', 'NCCL_VERSION': '2.7.8', 'NVIDIA_VISIBLE_DEVICES': 'all', 'DEBIAN_FRONTEND': 'noninteractive', 'CUDNN_VERSION': '8.0.5.39', 'LAST_FORCED_REBUILD': '20211202', 'JPY_PARENT_PID': '44', 'PYTHONPATH': '/env/python', 'DATALAB_SETTINGS_OVERRIDES': '{"kernelManagerProxyPort":6000,"kernelManagerProxyHost":"172.28.0.3","jupyterArgs":["--ip=\"172.28.0.2\""],"debugAdapterMultiplexerPath":"/usr/local/bin/dap_multiplexer","enableLsp":true}', 'ENV': '/root/.bashrc', 'GLIBCXX_FORCE_NEW': '1', 'NVIDIA_DRIVER_CAPABILITIES': 'compute,utility', 'TF_FORCE_GPU_ALLOW_GROWTH': 'true', 'LD_PRELOAD': '/usr/lib/x86_64-linux-gnu/libtcmalloc.so.4', 'PYTHONWARNINGS': 'ignore:::pip._internal.cli.base_command', 'NVIDIA_REQUIRE_CUDA': 'cuda>=11.1 brand=tesla,driver>=418,driver<419 brand=tesla,driver>=440,driver<441 brand=tesla,driver>=450,driver<451', 'OLDPWD': '/', 'HOSTNAME': 'e346aadce603', 'COLAB_GPU': '0', 'PWD': '/', 'CLOUDSDK_PYTHON': 'python3', 'GLIBCPP_FORCE_NEW': '1', 'TBE_EPHEM_CREDS_ADDR': '172.28.0.1:8009', 'TBE_CREDS_ADDR': '172.28.0.1:8008', 'TERM': 'xterm-color', 'CLICOLOR': '1', 'PAGER': 'cat', 'GIT_PAGER': 'cat', 'MPLBACKEND': 'module://ipykernel.pylab.backend_inline', 'ENABLE_DIRECTORYPREFETCHER': '1', 'USE_EPHEM': '1', 'PYDEVD_USE_FRAME_EVAL': 'NO'})'

alex-treebeard commented 2 years ago

I was imagining to add metadata (json) to a cell like "nbmake_ignore: True" and then it would be ignored.

I think that's the best long-term solution. I may aim to put something together in January.

In the meantime, my recommended DIY approach for something close to invisible would be to define a cell magic which does not run the cell if running in pytest.

PYTEST_CURRENT_TEST should be present when running in pytest. Presumably you could have a cell magic %%notest which discreetly disables the cell when this variable is present.

Edit: Here is an example of a cell magic

Keen to hear your thoughts.

alexheat commented 2 years ago

Thank you @alex-treebeard. If you add this let me know and I will be happy to test it. nbmake is really helping me because I keep breaking my notebooks.

I am not familiar with making cell magics and I am not quite sure where the cell magic defintion code is supposed to go

alex-treebeard commented 2 years ago

Hi @alexheat please see the latest release with features which should satisfy this use case.

Feedback welcome, and feel free to close if this solves your problem.