How to debug a JupyterDash app using VSCode? - python

This is going to be a self-answered post, but that only means that I've got an idea of a solution, and that I'm very eager to hear about more efficient and stable approaches to the challenge in question.
I would like to know how you can combine the powers of JupyterDash and VSCode in order to run a debugging process for an .ipynb file that involves all of the following steps:
stepping into code,
running code line by line,
inspecting variables,
setting breakpoints,
inspecting callbacks, and
interactively editing variables.
I believe I've tried every imaginable combination of:
Step into code with F10
Run > Start Debugging (F5) from the VSCode menu
Run and Debug (ctrl+Shift+D) from the Jupyter Notebook menu
Inspecting variables through JUPYTER:VARIABLES in the VSCode Debug Console
Still I don't feel I've found a work-flow that is 100% satisfactory when it comes to interactivity and stability. And perhaps this isn't the best approach at all? Suggestions that do not include JupyterDash are therefore more than welcome.`
System info:
Python 3.9.6
VScode 1.60.2
Plotly 5.1.0
JupyterDash 0.4.0

I'll base this suggestion on the code presented here to produce a basic dash app with an interactive figure and a dropdown that triggers a callback:
Part 1 & 2 - Stepping into code and running code line by line
By default for a an .ipynb file, F10 in VSCode will trigger Step into function and place a yellow arrow at the first line of a cell. You can then step through your code line by line by hitting F10 again.
Figure 1.1 - Stepping into code with F10
Part 3. inspecting variables
After stepping through df = px.data.tips(), you can find the variable under JUPYTER:VARIABLES in the Debug Console:
Figure 3.1 - Debug console and variables
You can also inspect the contents of df by clicking the icon highlighted above to Show variable in data viewer:
Figure 3.2 - Data viewer
Part 4 - Setting breakpoints
If you've got some data manipulation going on within a callback you can set a breakpoint inside the callback:
Figure 4.1 - Set breakpoint.
Part 5 - Inspecting callbacks
In order to make the process move on to that particular breakpoint, you can hit F5 or even trigger the callback through the Dash App that this code sample produces. For example by choosing aggsunset from the dropdown menu:
Figure 5.1 - Make a selection to trigger callback debugging
Now you should notice that nothing happens in the figure itself. And if you go back to VSCode you'll see that the debugging process has moved on to your breakpoint.
Figure 5.2 - Code halting a breakpoint after callback is triggered
As you can see, a new variable is defined withing the callback as df2 = df.copy(). Strange as it is though, now you can only see the local variables of the callback:
Figure 5.2 - Jupyter Variables while debugging
But one neat thing here is that you can inspect the input from the dropdown in your app, which is:
colorscale = 'agsunset'
What's even cooler is that every time you change your dropdown value and trigger the callback, the value is updated under JUPYTER:VARIABLES:
Section 6 - Interactively editing variables
Figure 6.1 - Changing variables while debugging callbacks
If you move on to the DEBUG CONSOLE tab, you can take a closer a look at df2 with df2.tail():
Figure 6.2 - Inspect new variables
And you can even add a new column with df2['new_var] = 1:

Related

Why I can't able to do step in, step out in pycharm?

I am not able to click(I can say it's diabled) step in, step out, step over option in pycharm debugger
I don't know if this is exactly what is causing your issues but here is one possibility:
All debugging options are grayed out in the menu if your cursor is on an empty line or a line that is a comment. Try putting your cursor on an actual line with code. The "Toggle Line Breakpoint" option in the "Run" menu should now become active. After starting the script with "Run -> Debug", the breakpoint will be triggered (assuming the code reaches it) and the other debug options such as "Step over" become available.
As mentioned in this answer: https://stackoverflow.com/a/48665161/2891209
Depending on what you're trying to debug it is possible for step-in to not show you code which isn't pure python. Pycharm will do the best it can of course, but if the function you're trying to debug isn't native python be prepared for anything...
If you're trying to step into a python wrapped C library function
Sometimes the debugger will skip over that code completely.
Sometimes you can walk the frames but the editor can't show you lines of code.
Sometimes you'll be able to step into an empty skeleton of a function.
It really depends on how the library was created.

How to run only block of code in PyCharm like in Spyder?

In Spyder I can run only a part of code without running everything. I know that in PyCharm I can click right mouse button and "Execute Selection in Console", but it will be new execution without values and variables which declared before this part of code.
So, very often I need to run only few last lines of my code, with parametres and options that I already have.
you can try to use the interactive interpreter while in debug mode.
I find it to be very useful when trying to run code snippets in the program.
view the screenshot below.
You can use the "Run cell" functionality and the cell will be executed in the Python Console (the same way it does when right clicking "Execute Selection in Console"). To enable that I am aware of two options :
In settings (Ctrl+Alt+S) install the "PyCharm cell mode" plugin.
Then use ## to create code sections.
https://plugins.jetbrains.com/plugin/7858-pycharm-cell-mode
Create a new project in scientific mode (only available in PyCharm professional)
You can create code cells with #%%.
https://www.jetbrains.com/help/pycharm/matplotlib-support.html
In both cases, it creates cells that you can execute with the green "play" button like shown below :

Drop frame while debugging?

I'd like to know if drop frame is available in PyCharm / Intellij with Python plugin.
Here's what the button looks like (it doesn't show up on the debug toolbar so I assume it's just not available for PyCharm) -->
How to step one step back in IntelliJ?
What I'm trying to do is to re-play a function when I see a problem, to inspect it more. ("come back in time")
(I've used this before in Eclipse with Java, it would replay the current scope if you edited it and saved changes. of course the global scope remains changed after running a function many times, but for most cases it's very useful)
Thanks for telling!
As of version 2017.3.3, there is no drop frame feature as that in IntelliJ. The pycharm docs referred above seems wrong.
Since you want to 're-ply a function', 'Evaluate Expression' works great for that purpose. I use a lot when I debug. It basically allows to run any code at runtime with the variables and functions in that context.
It is the icon on the most right on the debug window.
Put a break point on the line before the line where goes wrong, debug the program, when it tops on the break point, click on 'Evaluate Expression' icon, copy the function with issues, paste it and run it.
PyCharm 2017.2 seems to have added the functionality you're looking for. The documentation describes a "Drop frame button" on the Debug Stepping Toolbar:
Interrupts execution and returns to the initial point of method execution. In the process, it drops the current method frames from the stack.

OpenERP 6, Aptana - debugger doesn't stop at breakpoint in QR Bar Code Label code

I am trying to debug code for QR Bar Code Labels in OpenERP 6 using Aptana Studio 3. I put a breakpoint in "pyqr" module, file "myfile.py", function "generate_image()", as per attached picture:
Now, when I run OpenERP server from Aptana IDE ("openerp-server.py" -> Debug As -> Python Run) and navigate to Manufacturing Orders where I can click on one of the right hand buttons "Large Label" or "Medium Label" or "Small Label", the debugger doesn't stop at the breakpoint and yet the label is printed in opened PDF file.
I have performed the following tests to check if the code in "myfile.py" executes. I have put "print" statement in "generate_image()" function, and it did not print anything in console. I put "import pdb" and "pdb.set_trace()" and the execution did not stop there. I added a message box in "generate_image()" function and message box did not display, yet the QR bar code label was created. It looks like that "myfile.py" code is not executed at all adding to mystery which code is executed that creates QR Bar Code labels.
How I can make debugger stop at this breakpoint? What am I missing?
To be able to debug in your IDE, I assume that you are running the Odoo server from source and start it from inside the IDE.
I'm not sure what is your actual setup, but maybe these pointers can help.
Try putting the breakpoint on a line of the method, instead of on it's definition.
Are you sure that the code is being executed? Try placing a print statement in it to confirm. Or try adding a import pdb; pdb.set_trace() line as a way to force a breakpoint.

Is there "Edit and Continue" in PyCharm? Reload code into running program like in Eclipse / PyDev?

Hi all Python developers!
In Eclipse with PyDev it is possible to edit a Python file while debugging. On save, the PyDev debugger will reload the updated code into the running program and uses my new code. How can I do the same thing in JetBrains PyCharm (using Community Edition)?
Eclipse / PyDev writes an output like this when I do that:
pydev debugger: Start reloading module: "MyWidget" ...
pydev debugger: Updated function code: <function close at 0x055F4E70>
pydev debugger: reload finished
I searched settings and web and could not find any hint. Very glad about any idea. Thx.
Edit: I found out in Eclipse/PyDev one has to be in debug mode to be able to use this feature. I tested in PyCharm, but there was no reload done.
PyCharm does not support edit and continue in either the community edition or the professional edition but here is a workaround that I have found while debugging.
Since you can run arbitrary code in the console and/or the expression evaluator, in a lot of cases, you can execute changes to the code without having to restart the application. This isn't exactly like edit-and-continue (which is a feature I really like in Visual Studio and should be part of Pycharm) but it goes a long way towards avoiding having to restart the program from scratch after a change to see if the new code works as expected.
Let me illustrate a couple of the techniques I use:
Let's say you have the following code (with a couple of typos/bugs to illustrate the techniques)
test_value = [10,9,8,7,6,55,4,3,2,1]
for i in range(0,10):
if test_value[i] == i:
print "found the value: " + i
If you run this code, first it errors because you can't print string plus integer but also I wanted to match on 5, not have 55 in the array. So here we go.
Set a break point on the for statement like this and run the code in the debugger.
When it breaks into the debugger, you realize that it should be 5 not 55. Rather than restarting, you can change line 1 to test_value = [10,9,8,7,6,5,4,3,2,1] then select the line, right click and choose Execute Line in Console... which will change the value of test_value to be the array with a 5. Now, the if statement on line 4 becomes true on the value 5. This will then trigger the syntax error on line 5.
Now if you want to make sure you have the syntax correct you can change line 5 to print "found the value: " + str(i), select the line and choose Evaluate Expression... from the right button context menu. When you click Evaluate, the result will show up either in the dialog (or in this case, since it is a print command, in the console)
Now that I've fixed these two issues, I can run the code successfully on the second pass rather than possibly multiple passes it might have taken if I didn't use these techniques. These techniques really pay off if you find a bug deep in the code where it took a while to set up.
Obviously, this is a very contrived example, but hopefully this shows how you can use both Evaluate Expression... and Execute Line in Console... to your advantage while debugging without having to restart your application each time you find a bug in the code.
Also, if you happen to be using Django, PyCharm (professional) will re-launch the server if you make changes to the code. So if you are looking at your web page and notice a problem, you can make a change to the code and switch back to the web page and as you do, either the running application or the debugged application will re-launch and the new code will be running when you refresh the page. Again, not really edit-and-continue but a pretty rapid way to make a change and test.
After all I found a useful and acceptable workaround for my question. It works in PyCharm Community Edition 3.1.2 and I assume it will do in commercial edition as well. I tested on a mid-scale project using Python 2.7.6, PySide (Qt) with one main window and 20+ widgets, tabs, whatever. Follow these steps...
Work in PyCharm on a python project :-)
Execute your code in Debug mode (did not tried Release so far)
Edit some code in one your modules imported during the life of your program
Make your program pause. To achieve this, you can click the "Pause" button of in PyCharms Debug view and then any place in your applications main window where it would need to do something (for example on a tab header). If you have a long a running task and no UI, you may place a breakpoint in a place your program often comes by.
In the Debug view, switch to the Console tab. There is a button on the left Show command line. Click this.
In the console, type in reload(MyModifiedModule) if this call fails, write import MyModifiedModule and try again.
Click resume in PyCharm.
Try the code you fixed.
There are some restrictions on this... It won't fix changes in your main method or main window, cause it won't be created again. In my tests I could not reload widgets from Qt. But it worked for classes like data containers or workers.
May the force be with you as you try this and do not hesitate to add your experiences.
I have the commercial version of PyCharm and just tried testing a simple python script. The script is the following:
for i in range(0,100):
print i
I ran the code in debug mode and placed a break point at the "print i" statement. When the debugger stopped during the first iteration I changed the code to look like this:
for i in range(0,100):
print i
print 'hello'
PyCharm did not reload/re-compile the altered script. Given this simple test my best guess would be that PyCharm does not dynamically reload .py files.
You can add hot reloading features by installing Reloadium plugin.
https://plugins.jetbrains.com/plugin/18509-reloadium
Example use (gif)
It also works without pycharm.
More details:
https://github.com/reloadware/reloadium

Categories

Resources