Aralox
Aralox

Reputation: 1459

How to add custom Visual Studio debug view of Python object?

I'd like to add a useful visualization of a Python openpyxl ReadOnlyCell object during debugging in Visual Studio 2015 (Python tools). I read into natvis files, but they seem to be only for C++ projects. For example, the screenshot below shows what a ReadOnlyCell looks like in the Autos window. I'd like to display the value property (e.g. 6000 for cell 0 below) in the column, without having to navigate into each cell individually.

There seems to be a way to add Python object visualization by editing the PythonDkm.natvis file. I have found the file, but I don't understand how to add a custom class to the it. Any help would be appreciated!

enter image description here

Upvotes: 4

Views: 839

Answers (1)

Zooba
Zooba

Reputation: 11438

You should define __repr__ on your ReadOnlyCell class. That is what is displayed first, and then you can expand to see the rest of the values.

For example:

class ReadOnlyCell:
    ...
    def __repr__(self):
        return f"{type(self).__name__}(value={self.value})"

Would be displayed as:

Name     Value                           Type
[0]      ReadOnlyCell(value=6000)        ReadOnlyCell

Aside: the PythonDkm.natvis file is for mixed Python/C debugging. It contains the natvis definitions for viewing raw Python objects, and has no impact on regular Python debugging.

Upvotes: 2

Related Questions