Pytest-like verbose asserts in Jupyter Lab

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> def test_function():
> assert f() == 4
E assert 3 == 4
E + where 3 = f()
test_assert1.py:6: AssertionError
</code>
<code> def test_function(): > assert f() == 4 E assert 3 == 4 E + where 3 = f() test_assert1.py:6: AssertionError </code>
    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.

4

Rich adds a nice traceback that plays great with Jupyter.
Install Rich.

Then in your notebook put as the top cell the following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from rich.traceback import install
install(show_locals=False)
</code>
<code>from rich.traceback import install install(show_locals=False) </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from rich.traceback import install
install(show_locals=False, extra_lines=5)
</code>
<code>from rich.traceback import install install(show_locals=False, extra_lines=5) </code>
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.

For anyone not using jupyter notebook, the answer is pdb.pm() — the post mortem debugger.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import pdb; pdb.pm()
</code>
<code>import pdb; pdb.pm() </code>
import pdb; pdb.pm()

However, this does not play nice with Jupyter Notebooks though (at least not the setup I was using). I think it can just about handle input(), but any other REPL-like behaviour else is too complex for it to handle.

In which case the following function might help:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>In [1]: import sys
...:
...: def get_last_exc_locals():
...: try:
...: tb = sys.last_traceback
...: except AttributeError:
...: print('last_traceback not found -- no recorded exception'); return
...: while tb.tb_next:
...: tb = tb.tb_next
...: return tb.tb_frame.f_locals
...:
In [2]: def g(): return 0
...: def f(x):
...: y = g()
...: assert x > y
...:
...: f(-1)
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
Cell In[2], line 6
3 y = g()
4 assert x > y
----> 6 f(-1)
Cell In[2], line 4, in f(x)
2 def f(x):
3 y = g()
----> 4 assert x > y
AssertionError:
In [3]: get_last_exc_locals()
Out[3]: {'x': -1, 'y': 0}
</code>
<code>In [1]: import sys ...: ...: def get_last_exc_locals(): ...: try: ...: tb = sys.last_traceback ...: except AttributeError: ...: print('last_traceback not found -- no recorded exception'); return ...: while tb.tb_next: ...: tb = tb.tb_next ...: return tb.tb_frame.f_locals ...: In [2]: def g(): return 0 ...: def f(x): ...: y = g() ...: assert x > y ...: ...: f(-1) --------------------------------------------------------------------------- AssertionError Traceback (most recent call last) Cell In[2], line 6 3 y = g() 4 assert x > y ----> 6 f(-1) Cell In[2], line 4, in f(x) 2 def f(x): 3 y = g() ----> 4 assert x > y AssertionError: In [3]: get_last_exc_locals() Out[3]: {'x': -1, 'y': 0} </code>
In [1]: import sys
   ...:
   ...: def get_last_exc_locals():
   ...:     try:
   ...:         tb = sys.last_traceback
   ...:     except AttributeError:
   ...:         print('last_traceback not found -- no recorded exception'); return
   ...:     while tb.tb_next:
   ...:         tb = tb.tb_next
   ...:     return tb.tb_frame.f_locals
   ...:

In [2]: def g(): return 0
   ...: def f(x):
   ...:     y = g()
   ...:     assert x > y
   ...:
   ...: f(-1)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[2], line 6
      3     y = g()
      4     assert x > y
----> 6 f(-1)

Cell In[2], line 4, in f(x)
      2 def f(x):
      3     y = g()
----> 4     assert x > y

AssertionError:

In [3]: get_last_exc_locals()
Out[3]: {'x': -1, 'y': 0}

I don’t think you’ll be able to easily do what pytest does, as pytest rewrites source code (the .pyc files) on the fly. It does this so it can implement its special handling for when assert statements fail.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật