My code connects with a database and sometimes the database disconnects on me. As result the script ends. I would like to be able to add a line of code that would allow me to restart and run all the cells in Jupyter notebook.
Input:
if condition ==True:
#Kernel restart and run all jupyter cells
I understand there is already a question that may seem similar but it is not. It only creates a button that you can click to restart and run all the cell
How to code "Restart Kernel and Run all" in button for Python Jupyter Notebook?
Thank you
Would a keyboard shortcut suffice?
For JupyterLab users and those using the document-centric notebook experience in the future, see How to save a lot of time by have a short cut to Restart Kernel and Run All Cells?.
For those still using the classic notebook (version 6 and earlier) interface to run Jupyter notebooks:
A lot of the classic notebook 'tricks' will cease to work with version 7 of the document-centric notebook experience (what most people not consider the 'classic notebook interface') that is on the horizon. The version 7 and forward will use tech that is currently underlying JupyterLab, see Build Jupyter Notebook v7 off of JupyterLab components. And so moving towards JupyterLab now will help you in the long run.
Related
I'm starting to work more with Jupyter notebooks, and am really starting to like it. However, I find it difficult to use it with my particular setup.
I have a workstation for running all the notebooks, but for a large part of my day I'm on-the-go with a space-constrained laptop with no power outlets. I'd like to be able to edit (but not run) these notebooks without installing and running the full Jupyter server backend, which I imagine would suck up a lot of power.
My question is: Is it possible for me to edit (and not run) notebooks without running the Jupyter server?
You could use one of the following options
1. ipynb-py-convert
With this module you can do a conversion from .py to .ipynb and vice-versa:
ipynb-py-convert ~/name_of_notebook.ipynb ~/name_of_notebook.py
where according to the documentation the cells are left as they are. To get back a jupyter notebook
ipynb-py-convert ~/name_of_notebook.py ~/name_of_notebook.ipynb
2. Ipython
However, you could also do a conversion to .py when you want to work it with an editor like VS Code or Sublime Text after you have download your .ipynb file with ipython:
ipython nbconvert --to python name_of_your_notebook.ipynb
As I was asking this question, I had opened the notebook locally in Visual Studio Code, but the preview was just the raw text representation of the notebook, so I had assumed that it needed the backend to run.
However, I was about to press submit on the question when I checked back in on it, and the notebook showed up just fine. So one solution is to open it in VS Code and wait a little bit.
Jupyter lab has this feature where I can have a ipython console for every notebook I have opened. Whenever I run a cell inside this notebook, the console will have all the variables defined and modules imported corresponding to notebook. In addition, we can run extra commands and helps in debugging at times. Is there a similar feature in VS code? I really like it and would like to move completely to vs code. Python interactive command line in vscode is the closest to this that I found. However, it is not attached to the notebook and I have to run all the code inside the notebook which is a bit tedious.
I believe this would work Connecting a terminal to an existing kernel
However, you're likely looking for a way to do this within VS code. You might be able to do this by running %connect_info in a cell, starting a terminal, and then running the appropriate jupyter command.
Something like so:
jupyter console --existing kernel-2c0993da-95c7-435a-9140-118c10d33e1a.json
If you're refering to .py files you can do that the same way you would in pycharm.
First, you need to put a breakpoint in the code:
Them you run the code with the debugger:
Then, when the code reaches the breakpoint, you will be able to play with the variables, like the Jupyter terminal:
I also like to have a JupyterLab-style console open that is connected to a notebook. This is my workaround in order to achieve this in Visual Studio Code (at least it works when my kernel is a remote Jupyter session).
Suppose your notebook is called hello.ipynb.
Create a dummy file called hello.py.
Open hello.py, right-click in the code window and choose Run Current File in Interactive Window. This opens the JupyterLab-style console.
Change the kernel for the interactive window to the same kernel that the notebook hello.ipynb is using.
(Optional) Close the hello.py tab since it is not needed.
Now I have an interactive window sharing everything with the notebook.
I am using Jupyter notebook on Google Cloud Platform VM instance.
I finish work, stop the instance and restart vm and Jupyter Notebook the very next morning, I have to rerun all the codes from the top which is annoying because I have to load all the dataset and that takes good 30 minutes.
I googled around and found that these codes would work, but even though I have it on the top of my notebook still the same problem occurs.
%reload_ext autoreload
%autoreload 2
Is there any way to keep everything in the jupyter notebook so that I can just pick up and run it?
For anyone interested,
I used python library called 'dill'
dill.dump_session('notebook_env.db')
dill.load_session('notebook_env.db')
I have modified a function in a file in Spyder (and save it). Now, I rerun a cell that calls that function on my Jupyter Notebook and the modification that I made on my Spyder file does not seem to have effects on my Notebook, still mentioning an error that I had previously.
The only solution I have found to avoid this is to close the Notebook (by ctrl+C and deactivating command on Anaconda prompt and rerun the Notebook).
Of course, it's not so convenient... Is it possible to make it more efficiently ?
You can restart the kernel in jupyter instead of exiting and relaunching the app.
Then you need to re-execute the cells with the import statements.
(use restart and clear output)
There is also a jupyter magic function to reload modules documented here:
%load_ext autoreload
%autoreload 2
I'm looking for a way to turn OFF autosave in iPython notebook. I've seen references via Google/Stack Overflow searches on how to turn ON autosave but I want the opposite (to turn OFF autosave). It would be preferential if this was something that could be set permanently rather than at the top of each notebook.
This will disable autosave once you're in IPython Notebook in the browser: %autosave 0.
Update: There is now a UI feature in JupyterLab: https://github.com/jupyterlab/jupyterlab/pull/3734
If you add this to your custom.js, it will disable autosave for all notebooks:
$([IPython.events]).on("notebook_loaded.Notebook", function () {
IPython.notebook.set_autosave_interval(0);
});
custom.js is found at $(ipython locate profile)/static/custom/custom.js. You can use the same thing to increase or decrease the autosave interval. The value is in milliseconds, so an interval of 30000 means autosave every thirty seconds.
The original solution from MinRK is outdated, partly because IPython/Jupyter keeps changing so much. I can't find proper documentation for this, other than an indirect reference here, but according to this forum post, the solution now seems to be to edit or create the file ~/.jupyter/custom/custom.js, and add the line:
Jupyter.notebook.set_autosave_interval(0); // disable autosave
This works for me. You can confirm if it works by looking for the brief "Autosave disabled" box in the top right corner of the Jupyter notebook on startup. The full solution in the forum post did not work for me, probably because it is no longer completely valid, and errors in the custom.js file seem to occur silently.
Step-by-Step solution for Jupyter Notebook 5.5.0 on Windows (will probably work for other envs/versions as well)
Find the Jupyter configuration folder:
from jupyter_core.paths import jupyter_config_dir
jupyter_dir = jupyter_config_dir() # C:\users\<user_name>\.jupyter on my machine
create sub-folder custom, and create file custom.js within it:
i.e. 'C:\users\<user_name>\.jupyter\custom\custom.js'
Put the following line in custom.js:
IPython.notebook.set_autosave_interval(0);
Save file and restart the Jupyter Notebook server (main app).
When opening a notebook you should see "Autosave disabled" briefly appearing in the right side of the menu bar:
Edit: The autosave interval on notebook load does not appear to work any more in recent version of Jupyter Notebook (jupyter notebook --version at 6.0.1). So I'm back to the custom.js solution:
mkdir -p ~/.jupyter/custom
echo "Jupyter.notebook.set_autosave_interval(0);" >> ~/.jupyter/custom/custom.js
As pointed out by Thomas Maloney above, JupyterLab now has a command for that (Uncheck Autosave Documents in the Settings menu).
In Jupyter Notebook, I think the autosavetime extension is easier to use than the custom.js file. The autosavetime extension is part of the Jupyter notebook extensions and can be installed with
pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install
jupyter nbextension enable autosavetime/main
Once it is installed, restart jupyter notebook and go to nbextensions_config in the Edit menu. Select the autosavetime extension, and turn off autosave as follows:
check the box Set an autosave interval on notebook load. If false, the default is unchanged.,
enter 0 for Autosave interval (in minutes) which would be set on notebook load.
To test the modification: open or create a Python notebook and execute, in a new cell,
%%javascript
element.text(Jupyter.notebook.autosave_interval);
If the result is 0, you have successfully turned the autosave off. Congratulations!
As of Jupyter 4.4 (2019), a working solution is to add this to your custom.js file:
require(['base/js/namespace', 'base/js/events'], function (Jupyter, events) {
Jupyter.notebook.set_autosave_interval(0);
console.log("Auto-save has been disabled.");
});
Without the require block the javascript will execute prior to the Jupyter object being available, resulting in an error.
Just to be clear, custom.js should reside at ~/.jupyter/custom/custom.js -- you must create the custom directory if it does not exist.