Debug window in jupyter? - python

I just started using jupyter for a python project. I constantly find myself adding an extra cell just to perform some basic try&error debugging. This way I omit the whole code of the cell is being executed but it still doesn't feel like the right way to do it.
Does Jupyter provide something like a static kernel terminal, for example always visible at the bottom of the screen, where I can simply paste code and execute runtime variables?
By the way: I did search but didn't find anything looking for static console, and terminal. Maybe I'm just looking in the wrong direction.
Thanks!

The jupyter console command will provide you with an interpreter environment that you can experiment with code running within the Jupyter environment outside of a notebook.
It's not exactly what you're looking for but may provide a better environment for testing and developing code that you can then paste into the appropriate notebook.

Related

Why is my function not working in vscode when it works in jupyternote book?

I am new to python and have recently started to study it full-time. I have been using vscode to type out my code I would run the script and it would show my output in the terminal window.
However, I cannot get a lot of the functions I create to display an output in the terminal window. However, as a comparison check, I ran the same code in a Jupyter notebook and it worked perfectly fine. I also tried running this code on another laptop in vscode running it as a python file. But nothing has worked.
Here is the code running as a python file in vscode:
Running as a python file
Here is the code running in a jupyter notebook in vscode:
Running in a jupyter notebook
I apologise if I have been unclear. Do feel free to ask any questions to get a better understanding. I am a noob at this, so any help would be really encouraging. Thanks :)
Jupyter display in the console the value of the last line of code in a cell.
In normal Python scripts this does not happen. If you want to see the output in the console you need to use the print() function or similar.
so in your case:
print(capitalize_letters("mcdonald"))
or
capitalized_word = capitalize_letters("mcdonald")
print(capitalized_word)
If you want to get a value from a function and use it elsewhere, you should return that value at the end of the function definition. That way when you call the function at some point in your code and you can assign it to a variable which would store the output of the function.
So you would need to use
print(capitalize_letters("macdonald"))

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!
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.
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.
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:
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).
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:
This debugger console is where you can run normal matplotlib
commands.

PyCharm - how do I debug (like in Jupyter Notebook) without having to re-run the full script every-time that I modify code?

I'm new to coding and to PyCharm. I've learned with Jupyter and trying to get my head around PyCharm debugging (using single/multiple breakpoints).
What I liked about Jupyter is that when you put your code into different blocks, run them to output lists which you'll then be working with (the lists - as long as you haven't done anything to permanently affect them, but in that case, I would do new_list = og_list.copy() and play with the new list, to avoid re-running all of the code above) to create/test your next lines of codes, I can't figure out a way to proceed in the same way into PyCharm/Debugging.
In PyCharm, the way that I am using it, it seems that every time that I am changing a line of code to re-test/debug, I need to run the full code once again. I know I must be doing it wrong. I've played a few videos now and read a few posts online but I still can't seem to get it. Anyone here available to help?
Are you aware that you could write jupyter notebooks in PyCharm? - The cell functionality of jupyter notebook is only given in jupyter notebook and ipython as far as I know.
You may also open a iPython-shell in PyCharm.
Also, remember that you can easily convert a jupyter notebook to a .py-File, so nothings prevents you from writing code in jupyter notebook.

Trace Statement Execution in Jupyter Notebooks?

I am currently writing some Python code in a Jupyter notebook and I wonder if there is any possibility to track the execution of statements directly in Jupyter? I know there is this module called "trace": https://docs.python.org/3/library/trace.html#trace.Trace.results But it seems to me that with "trace" it is only possible to show the flow of execution in the console or to export it to a new file. However, I would like to display it directly in Jupyter Notebooks. Is this possible - with or without the mentioned module - and if yes how?
Thanks for your help.
For something along the lines of debugging code, you may be interested in the cell magic %%debug, discussed here to open a debugger in the notebook and use pdb commands. Or PixieDebugger if you need something fancier.
You may also be interested in the variable inspector extension for JupyterLab. There is (old) an animated example of using it here. You can easily try it out by clicking on launch binder badge there and starting a notebook. Then right-click in the open notebook and select Open Variable Inspector from the list. The animation will show you how to drag the tabs to arrange them side by side on your screen. Now as you run code, you'll see the variables updated.

Pycharm: Using Jupyter instead of prompt for debugging

When debugging a .py file within pycharms debug mode i can interact using a python prompt when hitting a breakpoint. Is there any way to use the current state of pycharm within a jupyter notebook istead of the python prompt?
It would make debugging quite a bit easier since you could reuse code snippets for debugging purposes.
I couldn't find anything about it, but for me it feels like it could be a thing.
Thanks for any help!
Based on this post, the functionality you are looking for does not exist. The key piece here is that you're trying to import an existing python interpreter into a new IPython kernel. However, from the post linked, you can attach a notebook to an existing IPython kernel with a little bit of work.
A possible solution would be for you to switch to using an IPython interpreter in PyCharm, then attach to that exising kernel in a notebook when needed (as described in the section above).

Categories