IPython-style ipdb debugging in Jupyter notebook? - python

To debug in Jupyter notebook, I use:
from IPython.core.debugger import set_trace; set_trace()
However, it only shows a command input box with no support for command history, autocomplete, or color...
I tried other debug options, but they do not work at all, like:
from IPython import embed; embed()
import pdb; pdb.set_trace()
import ipdb; ipdb.set_trace()
import pudb; pudb.set_trace()
Is there a way to have IPython supported debugger in Jupyter notebook?

No, it is not available (yet). It is on the wishlist on the IPython repository. We are a small team, there is a lot of interest but is is a lot of work to implement – not necessarily difficult, but the work need to be done. There is a bit of design to do ahead of time.
Any help on the IPython repository to cleanup issue, review PRs and other task may help the developers to get to it at some point. Feel free to open an issue on the IPython repository if you do not know how to help and need guidance.

Related

Jupyter Notebook: Autocomplete has too many suggestions

When using autocomplete in Jupyter Notebooks it is super nice that you can use autocomplete out of the box, but the autocomplete makes too many suggestions that are not relevant. E.g. when autocompleting inside a function, then I only want relevant parameters to be autocompleted, not 60 random python values.
People have suggested using %config Completer.use_jedi = True, but that turns everything off and is even worse.
You can try installing nbextension for suggestions in jupyter notebook.
for more info plz Click here

Programmatically Execute Cell Jupyter VSCode

I am looking for a way to programmatically replicate the Run Cell Below functionality VS code.
Previously, I used Jupyter through Conda and used the following code:
import ipywidgets as widgets
from IPython.display import display,Markdown,Javascript,HTML
def run_below(ev):
Javascript('IPython.notebook.execute_cells_below()')
button = widgets.Button(description="Click to run cells below")
button.on_click(run_below)
display(button)
This code worked great, but when I tried to plop it into VSCode, the button just does nothing. I don't understand much about how the VSCode Jupyter backend works, but I'm imagining it has something do do with the IPython.notebook module not working correctly in this IDE (or perhaps the IPython.display.Javascript module?). I really have no real idea though.
Does anyone know how I could do this in VSCode's Jupyter implementation?
I have searched for hours on this topic, but have not been able to find a working solution that works. Please let me know if y'all have any ideas.
Environment Info:
Python Version: 3.9.12
VSCode Version: 1.69.0
Jupyter Extension Version: v2022.6.1001902341
It appears that the ability to access the Kernel in VS code is not possible at this time. See the following GitHub issues to see if this has changed at the time of reading:
Similar question migrated to #6918
Issue #6918 that will resolve problem once closed
Not exactly an answer to detailed question but is an answer to the title of the question "Programmatically execute cell jupyter vscode" since I landed on this page searching for how to do this. The following code supports this task - just make sure to hit save CTRL-S if you make changes to cells that are going to be run by the following function since it reads the current version of file from disk
def execute_cell(filepath,cell_number_range=[0]):
import io
from nbformat import current
with io.open(filepath) as f:
nb = current.read(f, 'json')
ip = get_ipython()
for cell_number in cell_number_range:
cell=nb.worksheets[0].cells[cell_number]
#print (cell)
if cell.cell_type == 'code' : ip.run_cell(cell.input)
also for finding name of current notebook in vscode is easy but not easy in jupyterlab
import os
globals()['__vsc_ipynb_file__'] #full name only in vscode
os.path.basename(globals()['__vsc_ipynb_file__']) #basename only in vscode . globals()['_dh'] #dir in most

Error loading preloads: Could not find renderer

I am running jupyter notebook in vscode. But when I try to run the following cell of code I got error.
import os
from torch.utils.data import DataLoader
from torchvision.datasets import ImageFolder
import torchvision.transforms as tt
import torch
import torch.nn as nn
import cv2
from tqdm.notebook import tqdm
import torch.nn.functional as F
from torchvision.utils import save_image
from torchvision.utils import make_grid
import matplotlib.pyplot as plt
%matplotlib inline
The error is
Error loading preloads:
Could not find renderer
I tried but could not find its solution on the internet. How can I resolve this issue?
In my case, I had Jupyter notebook extension installed. So, I did ctrl+shift+P and did Reload window; resolved!
In case you just don't see the bar without any error: disable Jupyter notebook renderers extension, test, and then re-enable it
I am having the same issue with the latest release of the VS code Jupyer extension. The fix for now is to just go to the Jupyter extension tab, then the button that says "Uninstall," click the down arrow and hit "Install Another Version" and just install the version from > 1 mo ago and it should work fine. Hopefully they'll resolve this issue in the latest version soon.
Install/reinstall Jupyter Notebook Renderers Extension
Reload Window
Open the Command Palette (Ctrl + Shift + P)
Then type: Reload Window
If this still doesn't work, make sure plot presentation is set to:
image/png
Jupyter Notebook Renderer (ms-toolsai.jupyter-renderers)
Change Presentation
Select Renderer
You should now be able to generate a plot in the interactive window. Hovering over the plot will give options to expand or save.
To get a better looking plot when zoomed in enable the "Jupyter: Generate SVGPlots" setting in the "Jupyter" extension and reload the window again. Then change the plot presentation to:
image/svg+xml
Jupyter Notebook Renderer (ms-toolsai.jupyter-renderers)
From the github issue: Renderer extension not being installed -- causes some packages not to work, like plotly. It should have been fixed in new release because the issue was closed on 14 Jan.
Run this code and see if the question goes away:
import plotly.io as pio
pio.renderers.default = 'iframe_connected'
OR
You may try to uninstall Jupyter extension completely by deleting its packages in the folder \...\.vscode\extensions\ms-toolsai.jupyter then reinstall the latest version.
Install extension Jupyter Notebook Renderers and update your VS code to the latest version.
just disable the "jupyter notebook renderers" in the extension section and then Reload it. Lastly, enable it and you're good to go.
When you install the Jupyter extension, Jupyter Notebook Renderers are also installed because it's included in the extension pack. This extension is causing this error so remove the extension from the Jupyter extension details and try running the block again.
If the mentioned solutions do not work try the followings as well:
check if you have all packages installed, for me the problem was that one package was not installed and I saw the same error as you mentioned which is totally irrelevant to missing one package!
if that does not work either, simply check if you are using the correct Python interpreter in the correct environment.
As for me, this happened because of the "Workspace Trust" thing, where normally you would open the file/files by right-clicking the folder from explorer and click "Open with VSCode". Instead, i opened the notebook's file directly with VSCode and the Workspace Trust tab shows up. The Workspace Trust makes almost all of your extension disabled, including the Jupyter's ones.
Normally, you can just trust your workspace/directory in the same session and your extensions will be reloaded without needing of restart, apparently this is not the case as the problem appeared and only disappeared after a VSCode restart.
So, TL;DR : Workspace Trust tab shows up > Trust the workspace > restart VSCode
I think you should try restarting VSCode.
Just uninstall Jupyter Notebook Extension in VScode Extension, Reload the VScode window then install back Jupyter Notebook extension and run the cells again. It sloved my problem

Create a mess I need to clean up, while trying to solve conflicts between Macbook Pro M1 and Tensorflow library

I have a 2021 Macbook pro M1.
It is a well recognized problem that it conflicts with Tensoflow library, see here or here; in particular this last one was exactly the issue I experienced: when I tried to import tensorflow in jupyter notebook via the command
import tensorflow as tf
then the message
The kernel appears to have died. It will restart automatically.
appeared. Then searching in the above linked discussions, I have the feeling that the suggestion given at some point, which points at this link, SEEMS to be useful.
FIRST QUESTION: is this a/the solution for the M1-Tensorflow conflict?
I say "it seems" since before trying that I have been into the kind of tornado of desperate attempts leading a beginner like me to search for hints all around the web and then copy-paste commands taken here and there into the Terminal without understanding them all properly.
On one hand it sounds dumb, I admit, on the other the cost of understanding everything goes well beyond my humble intentions of learning some ML.
So, the final result is that I have a complete mess in my computer; the old libraries like numpy don't work anymore (when I import them inside a Python3 page opened with jupyter notebook with the command import numpy as np, the message
ModuleNotFoundError: No module named 'numpy'
appears), then the pip command doesn't work, if I use the pip3 to install, nothing changes. I read somewhere to use a virtual enviroment, and I followed the instructions even if I wasn't really aware of what I was doing; I downloaded XCode, miniforge3...
Well, I guess that there is somebody out there who can relate with this.
SECOND PROBLEM: I would like to clean-up everything dealing with Python/pip/anaconda and so on and install everything from scratch, possibly following the above link to solve the M1-tensorflow conflict...if it is correct. How can I do that?
Can somebody help me, please? Thanks

%matplotlib inline doesn't work on iPython and Jupyter console

I'm trying out Jupyter console for the first time, but can't get the %matplotlib inline magic to work. Below is a screenshot of an example session:
The plot shows in a separate window after I run Line 6, and Line 7 doesn't do anything.
When I run %matplotlib --list, inline is given as one of the options:
Available matplotlib backends: ['osx', 'qt4', 'qt5', 'gtk3', 'notebook', 'wx', 'qt',
'nbagg', 'agg', 'gtk', 'tk', 'ipympl', 'inline']
When I try to use another backend, say qt5, it gives an error message because I don't have any Qt installed.
ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5, or PySide
package to be installed, but it was not found.
Running %matplotlib?? reads:
If you are using the inline matplotlib backend in the IPython Notebook
you can set which figure formats are enabled using the following::
In [1]: from IPython.display import set_matplotlib_formats
In [2]: set_matplotlib_formats('pdf', 'svg')
The default for inline figures sets `bbox_inches` to 'tight'. This can
cause discrepancies between the displayed image and the identical
image created using `savefig`. This behavior can be disabled using the
`%config` magic::
In [3]: %config InlineBackend.print_figure_kwargs = {'bbox_inches':None}
But I don't know if it's something I can tweak around to solve my issue.
When I try it the magic IPython console, it says inline is an Unknown Backend.
UnknownBackend: No event loop integration for u'inline'. Supported event loops are: qt,
qt4, qt5, gtk, gtk2, gtk3, tk, wx, pyglet, glut, osx
I've also found this issue on github after some googling but I don't even know if it's relevant to my situation (most of their conversation didn't make sense to me lol).
Lastly, I'm not sure if this issue is related at all, but here it is, just in case: when I try to open Vim in Jupyter via the !vim command, it glitches pretty badly, preventing me from even exiting out of Jupyter itself without closing the terminal altogther. Vim works perfectly fine when called inside IPython console, however.
I'm using matplotlib 2.0.0.
If anyone could help me figure this out, that'd be great! Thank you!
You’re running a console which is completely text based and incapable of showing images. Therefore, although inline is available, it's not producing inline output.
I'm not sure why it doesn't throw an error, though, which it does in my case:
You can use %matplotlib inline in a GUI console, like Jupyter QTConsole
or in a jupyter notebook in the browser
%matplotlib without the inline works for me (I'm using osx and IPython 7.0.1)
If you're running an old version of ipython try %pylab inline instead. See notes in this tutorial

Categories