Optimizing Jupyter Notebook: Tips, Tricks, and nbextensions | by Shadab Hussain | Towards Data Science

Jupyter Notebooks are a web-based and interactive tool that the machine learning and data science community uses a lot. They are used for quick testing, as a reporting tool or even as highly sophisticated learning materials in online courses.
So here in this blog, I’m going to list down a few of the shortcuts, magic commands and nbextensions.
Press Ctrl+Shift+p or click on the small keyboard icon from the menu bar to get a list of command palette
Shortcuts in both command and edit modes:
Shift + Enter run the current cell, select below
Ctrl + Enter run selected cells
Alt + Enter run the current cell, insert below
Ctrl + S save and checkpoint
Shortcuts when in edit mode:
Esc take you into command mode
Tab code completion or indent
Shift + Tab tooltip
Ctrl + ] indent
Ctrl + [ dedent
Ctrl + A select all
Ctrl + Z undo
Ctrl + Shift + Z or Ctrl + Y redo
Ctrl + Home go to cell start
Ctrl + End go to the cell end
Ctrl + Left go one word left
Ctrl + Right go one word right
Once in command mode, press H (help) to get a list of keyboard shortcuts:
I’m listing down a few of the most commonly used shortcuts. Make sure you are in command mode by hitting Esc:
Scroll up and down your cells with your Up and Down keys.
Press A or B to insert a new cell above or below the active cell.
M will transform the active cell to a Markdown cell.
Y will set the active cell to a code cell.
X will cut the selected cell
C will copy the selected cell
V will paste the cell which is being copied/cut
Shift + V paste cells above
S will save the notebook
F will find/replace
O will toggle output
D + D (D twice) will delete the active cell.
Z will undo cell deletion.
For selecting multiple cells at once, hold Shift and press Up or Down
Shift + Space scroll notebook up
Space scroll notebook down
With multiple cells selected:
Press Shift + M to merge your selection
To split the active cell at the cursor, press Ctrl + Shift + - in edit mode
You can also click and Shift + Click in the margin to the left of your cells to select them
Copying and pasting cells BETWEEN notebooks:
Notebook 1: — Select multiple cells by holding down Shift and hit Ctrl+c to copy
Notebook 2: — Hit Esc to enter Command mode Ctrl + v to paste
Installing packages in the current kernel using ! pip install <package>
Using Python variables in shell commands by prepending a $ symbol:
Magic Commands are shortcuts that significantly extend a notebook’s capabilities
Sharing a code sample from your notebook:
Use the %pastebin magic function to select a range of cellsJupyter gives you a secret URL to share
Use the %pastebin magic function to select a range of cells
Jupyter gives you a secret URL to share
To have a list of defined variables, use %whos or %who_ls
%whos displays the variable type plus some extra info: size, contents, etc.
%who_ls only displays the variables name
Working with external files within Jupyter Notebook:
%pycat file.py ➡ opens script in the pager
%load file.py ➡ inserts the script into a cell
%run file.py ➡ runs script
%run file.ipynb ➡ runs notebook
%notebook filename ➡ exporting the current IPython history to a notebook file
Getting, setting or listing environment variables:
%env ➡lists all environment variables
%env var ➡ get value for var
%env var val➡ set value for var
Running commands in the shell:
%system ➡ to use shell (mostly used to get current directory, date, etc)
Autosaving notebook to its checkpoint using %autosave :
Executing different languages:
%%HTML ➡ to execute HTML code
%%perl ➡ to execute Perl in a subprocess
%%javascript or %%js ➡ to execute the block of Javascript code
%%python3 ➡ to execute code with python3 in a subprocess
%%ruby ➡ to execute Ruby code
Other magic commands:
%history ➡ print input history
%lsmagic ➡ list currently available magic functions
%magic ➡ print information about the magic function system
%matplotlib ➡ setting up matplotlib to work interactively
%pwd ➡ returns current working directory
%quickref ➡ shows a quick reference sheet
%time ➡ time execution of a Python statement or expression (it can be used both as a line and cell magic)
The benefits of this extension are that it changes the defaults.
To install nbextensions, execute below commands in Anaconda Prompt:
conda install -c conda-forge jupyter_contrib_nbextensionsconda install -c conda-forge jupyter_nbextensions_configurator
Alternatively, you can also install nbextensions using pip:
pip install jupyter_contrib_nbextensions
Run pip show jupyter_contrib_nbextensions to find where notebook extensions are installed
Run jupyter contrib nbextensions install to install the new extension
After installing, restart the Jupyter notebook, and you can observe a new tab Nbextensions added to the menu:
The same nbextension can also be located in the Edit menu:
Now, let's see a few of the nbextensions:
Hinterland - It enables code autocompletion menu for every keypress in a code cell, instead of only enabling it with tab
Hinterland - It enables code autocompletion menu for every keypress in a code cell, instead of only enabling it with tab
2. Split Cells Notebook - Enable split cells in Jupyter notebooks
Enter command mode (Esc), use Shift + s to toggle the current cell to either a split cell or full width.
3. Table of Contents - The toc extension enables to collect all running headers and display them in a floating window, as a sidebar or with a navigation menu. The extension is also draggable, resizable, collapsable, dockable and features automatic numerotation with unique links ids, and an optional toc cell.
4. Autopep8 - Use kernel-specific code to reformat/prettify the contents of code cells
5. Snippets - Adds a drop-down menu to insert snippet cells into the current notebook.
We can convert to Dark mode from default theme of Jupyter Notebook.
For achieving this, we need to install jupyterthemes :
jt -l will give the list of available themes
jt -t <theme name>will change the theme. Let’s try changing it to the dark mode by using the chesterish theme.
jt -r will restore it to the default theme
There are many other things we can do with Jupyter notebooks that we haven’t covered. Let’s keep it for the next blog.
Build your first Machine Learning Model using TensorFlow
Use cases of different Machine Learning Algorithms
Steps To Activate Free Access To Datacamp, Pulralsight, LinkedIn Learning, Etc Through Visual Studio Dev Essentials Program
My MozFest Experience and First Talk
Questions? Comments? Feel free to leave them in the comment section, also you can follow and connect with me on LinkedIn. If you like this blog, you can buy me a coffee.