Optimizing Jupyter Notebook: Tips, Tricks, and nbextensions

Optimizing Jupyter Notebook: Tips, Tricks, and nbextensionsShadab HussainBlockedUnblockFollowFollowingMar 17Jupyter 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.

ShortcutsPress Ctrl+Shift+p or click on the small keyboard icon from the menu bar to get a list of command paletteShortcuts in both command and edit modes:Shift + Enter run the current cell, select belowCtrl + Enter run selected cellsAlt + Enter run the current cell, insert belowCtrl + S save and checkpointShortcuts when in edit mode:Esc take you into command modeTab code completion or indentShift + Tab tooltipCtrl + ] indentCtrl + [ dedentCtrl + A select allCtrl + Z undoCtrl + Shift + Z or Ctrl + Y redoCtrl + Home go to cell startCtrl + End go to the cell endCtrl + Left go one word leftCtrl + Right go one word rightOnce 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 cellC will copy the selected cellV will paste the cell which is being copied/cutShift + V paste cells aboveS will save the notebookF will find/replaceO will toggle outputD + 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 DownShift + Space scroll notebook upSpace scroll notebook downWith multiple cells selected:Press Shift + M to merge your selectionTo split the active cell at the cursor, press Ctrl + Shift + – in edit modeYou can also click and Shift + Click in the margin to the left of your cells to select themCopying and pasting cells BETWEEN notebooks:Notebook 1: — Select multiple cells by holding down Shift and hit Ctrl+c to copyNotebook 2: — Hit Esc to enter Command mode Ctrl + v to pasteInstalling packages in the current kernel using !.pip install <package>Using Python variables in shell commands by prepending a $ symbol:Magic CommandsMagic Commands are shortcuts that significantly extend a notebook’s capabilitiesSharing a code sample from your notebook:Use the %pastebin magic function to select a range of cellsJupyter gives you a secret URL to shareTo 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 nameWorking 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 fileGetting, setting or listing environment variables:%env ➡lists all environment variables%env var ➡ get value for var%env var val➡ set value for varRunning commands in the shell:%system ➡ to use shell (mostly used to get current directory, date, etc)Autosaving notebook to its checkpoint using %autosave :Autosaving every 120 seconds (2 minutes)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 codeOther 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)nbextensionsThe 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_configuratorAlternatively, you can also install nbextensions using pip:pip show jupyter_contrib_nbextensionsRun pip show jupyter_contrib_nbextensions to find where notebook extensions are installedRun jupyter contrib nbextensions install to install the new extensionAfter 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 tab2.

Split Cells Notebook – Enable split cells in Jupyter notebooksEnter 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 cells5.

Snippets – Adds a drop-down menu to insert snippet cells into the current notebook.

Jupyter Notebook ThemeWe 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 themesjt -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 themeThere are many other things we can do with Jupyter notebooks that we haven’t covered.

Let’s keep it for the next blog.

Other blog posts by meUse cases of different Machine Learning AlgorithmsSteps To Activate Free Access To Datacamp, Pulralsight, LinkedIn Learning, Etc Through Visual Studio Dev Essentials ProgramMy MozFest Experience and First TalkLinkedInHere is my LinkedIn profile in case you want to be connected with me.

Thank you for the read, give claps if you like it and share your feedback/tips/tricks in comments.

.

. More details

Leave a Reply