I'm using Jupyter lab and sometimes I want to run some assertions on my data. For example, that all the folds have at least 10 test items.
Nevertheless, when the assert fails, I have to modify the notebook and run it again just to print more information.
Is it possible to get verbose asserts like the ones pytest uses:
def test_function():
> assert f() == 4
E assert 3 == 4
E + where 3 = f()
test_assert1.py:6: AssertionError
Perhaps using a jupyter lab plugin or some other Python package.
Rich adds a nice traceback that plays great with Jupyter.
Install Rich.
Then in your notebook put as the top cell the following:
from rich.traceback import install
install(show_locals=False)
Run that.
(You can do show_locals=True
if you prefer.)
Then run code in the other cells. The traceback will be better.
You can see examples & read more about an earlier stage of implementation here in Will McGugans's February 23, 2020 post 'Better Python tracebacks with Rich'.
He says there about the lines of code shown:
"It's configurable, but I find that 7 lines of code are enough to make it relatable to the file in my editor, and give me a better understanding of the context that lead to the exception."
It looks like you can specify the number of surrounding context lines shown to be larger, if you prefer, like so:
from rich.traceback import install
install(show_locals=False, extra_lines=5)
Use of extra_lines
based on here and looking there may give you idea of additional settings you'd like to customize about the tracebacks displayed in your running notebook.