Search code examples
pythonmatplotlibvisual-studio-codevisual-studio-debugging

Show matplotlib figures during debug in visual studio code (using ssh conection)


I am working with visual studio code (python) using ssh to access a remote server (where the code is located). I am able to write code, run and debug without any problem. However I am not able to generate matplotlib figures during debug in the same way as i can do it without the ssh connection.

I've tried to follow several suggestions from internet and other post here but none of them is fully working.

  • I've tried the option of " Jupiter> Debug current file in interactive window " but it does not work. I am not sure why, but the debug in the interactive window is not responding and i can not work during debug.

  • I've tried python debug and the only matplotlib backend that works is (matplotlib.use("WebAgg"). However for this backend it only works with one figure and the debug is block after the plot.

  • I have also tried to use X.Org (in my case xquartz over mac) but the figures are really slow and it is not useful.

Any idea on how to plot figures during debug in the same way as in local development?

Thanks!


Solution

  • I asked this question in GitHub and got the following answers:

    One way to do this is to do the 'Debug your current file in the interactive window'.

    There's a bunch of caveats though.

    1. You need to have ipykernel installed into the python environment you're using. If you run that command it should have asked you to do so.
    2. It's likely easier to use if you put cell markers around pieces of code # %%. This would also allow you to prerun bits of your script before you need to debug the part that's causing problems. This is what it looks like when you do that:

    enter image description here

    1. When debugging in the interactive window, your code in your script is running as an IPython cell. If you split it up, it might be multiple cells or it could be one large cell (for the whole file).
    2. As you step, the cell execution is moved forward one line at a time. However the execution is paused in between steps. This means anything you type in the interactive window won't be executed. Instead you have to run extra code in the 'debugger console'. Shown below:

    enter image description here

    1. This debugger console is where you can run normal matplotlib commands.