How to debug Jupyter Notebook profile_default/startup File? - python

I am placing a .py file in Jupyter Notebook profile_default/startup folder. It has some problems, I want to debug and see the logs. Is there any way I see the output or logs generated by that file. I am on windows 10. There are a few methods .py file. I am using keyboard module to generate a hotkey whenever the notebook starts. It isn't working for me. Please suggest a good way to at least debug that file. My ipykernel is 5.3.4 and ipython 7.16.1

The easiest way I've found so far is to fire up ipython from your console.
Assuming the error is not specific to how Jupyter starts the kernel, you will see the error with the stacktrace before the ipython shell starts.
If the error is specific to jupyter kernel, use jupyter kernel. It will start the kernel you specify, and drop a log line if there's an error.

Related

Jupyter Notebook Kernel Crashing in VSCODE

I am interested in using VSCode's Jupyter notebook functionality and I am just trying to get to grips with it. I can start the kernel and run the notebook cells in VSCode.
After running particular cells it seems to crash with no warning and the code fails as the variables have been cleared. I have to reselect the kernel in the top right and start running the code again from the beginning until I get to the problem cells, and then the issue repeats itself.
I don't think it's anything to do with the Python code, it's some simple dataframe manipulation. I have tried the same notebook just running it in the browser and it works fine.I am running Jupyter through a venv with Python 3.7.6, MacOS Catalina (10.15.4), VSCode 1.45.1.
Something I noticed that might help indicate what is happening, is when it crashes I can run basic python statements in the notebook cells (without reselecting the kernel). But when I run my cell which tries to import tests from a .py file in the same folder or import data files using a relative path, it can't find them until I reselect the kernel. It seems like the cwd has changed? Can anyone assist with this?

Change the starting folder of Jupyter

I am having issues with changing the starting folder of Jupyter notebooks, as I would like to run the notebooks on a shared server. I am running Windows 10. I have already tried several of the options online:
I have tried changing the "Start in" property of the jupyter
shortcut: now the shortcut just quickly opens the terminal, then
immediately closes again and nothing more happens.
I have tried running the "jupyter notebook --generate-config"
command, both in and outside python: in the first case it tells me
that jupyter is not a recognised command, and in the second that
there's a syntax error at notebook.
I have made a .bat file with the instructions of the shortcut ('''C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\python.exe
C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\cwp.py
C:\Users\USERNAME\AppData\Local\Continuum\anaconda3
C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\python.exe
C:\Users\USERNAME\AppData\Local\Continuum\anaconda3\Scripts\jupyter-notebook-script.py
Z:\Test''') but it
simply opens the terminal in python, and nothing more.
If I try to launch jupyter from the anaconda interface it works, and I can also start it from the terminal, but not in any other way.
The "jupyter notebook --generate-config" method, explained well in this answer from #marneylc, worked, I just had to start in the Anaconda Promt, rather than in cmd.
I hope this is the right way of answering my own question...

How to disable jupyter notebook history

I'm using Jupyter Notebook to code in Python 2.
I'm invoking it as:
c:\python27\scripts\jupyter-notebook --no-browser
At the same time I use IPython console, launched with:
c:\python27\scripts\ipython
The problem I have is that Jupyter history is saved and is mixed with IPython history.
I don't want Jupyter Notebook history at all - is there a way to disable it, while retaining IPython** history?
Platform: win32
Update:
I have tried to use suggested setting digest approach.
But when I enter "c.Session.digest_history_size = 0" to the config, restart notebook, write "print 'next test'" in some cell, restart separate IPython and after pressing up the first thing I get is "print 'next test'".
How can I get rid of it?
See this Jupyter issue on Github for the origin of this solution.
In the Introduction to IPython Configuration using configuration scripts located in your home directory at ~/.ipython/profile_default/ is discussed. This is the relevant directory for the default profile, other similar directories appear if one creates other profiles.
Inside that directory one can include the file ipython_config.py which will run on every usages of IPython. However, the file ipython_kernel_config.py will run upon invocation of an IPython kernel, not when invoking the IPython interpreter itself. One can test this by doing ipython kernel --debug.
Jupyter notebooks use this style of kernel invocation. Therefore including a script ipython_kernel_config.py in the directory ~/.ipython/profile_default/ (assuming the default profile) with the following lines:
# Configuration file for ipython-kernel.
c = get_config()
c.HistoryManager.enabled = False
Should disable the history manager completely when using that style of kernel invocation. Therefore one should not populate command history from Jupyter calls.
Incidentally, the file history.sqlite in that same directory is the command history. So, deleting it or moving it to a different filename will clear the command history buffer.

IPython notebook does not capture all console output

within my code I use a Python package that calls external programs which write their output to the same console that IPython is running in. If I now run the same code from an IPython notebook, the output is not written to the notebook output cells, but into the console that the IPython notebook server is running in.
Do you know if it is possible to capture this output somehow, and then display it within the IPython notebook? I'm on Xubuntu 14.04, by the way.
Thank you very much for your help!
This gives you nice and comprehensive description of your problem and potential solutions. You need to try it out for yourself with your specific setup.

the most rudimentary ipython notebook questions from a first-time user:

Been using the safe and easy confines of PyCharm for a bit now, but I'm trying to get more familiar with using a text editor and the terminal together, so I've forced myself to start using iPython Notebook and Emacs. Aaaaand I have some really dumb questions.
after firing up ipython notebook from terminal with the command 'ipython notebook', it pops up on my browser and lets me code. but can i not use terminal while it's connected to ipython notebook server?
after writing my code in ipython notebook, i'm left with a something.ipynb file. How do I run this file from terminal? If it was a .py file, i know i could execute it by tying python something.py from the command line; but it doesn't work if i type python something.ipynb in the command line. And of course, I assume I hit Control-C to quit out of the running server in terminal first? or do I run the command without quitting that? Or am i doomed to test it in iPython and then copy and paste it to a different txt editor like Emacs and save it in a .py file to run it?
what good is the .ipynb file if i can't run it in terminal or outside the iPython Notebook browser? Could I get my code in a .py from iPython Notebook if I wanted to? (i assume I'll be able to easily run it in terminal by tying something.py then)
thanks in advance. I'm still very much trying to figure out how to use this thing and there aren't many answers out there for questions this elementary.
Yes, you can not use the same terminal. Solutions: open another terminal or run ipython notebook inside screen. If you use Windows you might want to take a look into this question
Notebook documents (ipynb files) can be converted to a range of static formats including LaTeX, HTML, PDF and Python. Read more about converting notebooks in manual
Notebooks are great, because you can show other people your interactive sessions accompanied with text, which may have rich formatting. And if someone can run notebook server he can easily reproduce your computations and maybe modify them. Check out awesome notebook on traveling salesperson problem by Peter Norvig as an example of what you can do with ipynb. Or this notebook. More examples are available here
You can run your IPython notebook process in background.
On Unix platforms you can perform that with (note the leading &):
ipython notebook &
Or after a "normal" run, hit [Control+z] and run the bg command
(some lecture).
you can convert .ipynb file into .py file using nbconvert
with Ipython notebook 2.x (some lecture):
ipython nbconvert --to python mynotebook.ipynb
with Ipython notebook 3.x (some lecture):
ipython nbconvert --to script mynotebook.ipynb
.ipynb contains your script AND results AND formatted text.
You can compare it to a literate script (but using "machine programming language").

Categories