jupyter / nbgrader

A system for assigning and grading notebooks
https://nbgrader.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.3k stars 317 forks source link

AutoTest hits an exception if the kernel has no `text/plain` response #1886

Open trevorcampbell opened 6 months ago

trevorcampbell commented 6 months ago

When generating a notebook with autotest directives, cells that don't produce a text/plain output will cause AutoTest to crash with output:

      File "/opt/conda/lib/python3.11/site-packages/jupyter_client/client.py", line 551, in _async_execute_interactive
        output_hook(msg)
      File "/opt/conda/lib/python3.11/site-packages/nbgrader/preprocessors/instantiatetests.py", line 430, in _execute_code_snippet_output_hook
        self.execute_result = self.sanitizer(content["data"]["text/plain"])
                                             ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
    KeyError: 'text/plain'

The offending cell that was the source of the key error in my case just rendered an IRdisplay:

IRdisplay::display_html('<iframe width="560" height="315" src="https://www.youtube.com/[youtube video link]" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>')

The fix for this should be fairly easy. When the kernel returns a response, need to check that the content dict has a text/plain entry before trying to use it. If it doesn't, then there's nothing to handle and AutoTest should move on, unless it's specifically expecting a response in which case throw an exception with a more informative error message.