library(reticulate)
use_python("/Users/chammika/miniconda3/envs/science-work/bin/python", required = TRUE)Using Jupyter (Good)
What to expect in this chapter
You should now know the basics of using a Jupyter Notebook. However, the Jupyter environment offers many more goodies (e.g., extensions, shell magic). In this chapter, I will show you a few of these and help you optimise your environment to make it easier and faster to use Jupyter.
Some tips and tricks
Keyboard Shortcuts
Using keyboard shortcuts will make your workflow smooth and efficient. Here are some essential shortcuts. It will help you to practice using all these keyboard shortcuts.
| Action | Shortcut |
|---|---|
| Run cell | CTRL + ENTER or CMD + ENTER |
| Run cell and move to the next | SHIFT + ENTER |
| Convert cell to code cell | ESC + Y |
| Convert cell to Markdown cell | ESC + M |
| Create new cell | ESC + A (above)ESC + B (below) |
| Copy cell(s) | ESC + C |
| Paste cell(s) | ESC + V |
| Merge cells | SHIFT + M |
| Delete cell | ESC + D + D |
| Show shortcuts | ESC + H |
You will notice that many shortcut commands use the escape button (ESC). This is because typing ESC puts Jupyter into command mode, ready for a command, not code or text.
Things to note
- You can select one or more cells using
SHIFTand the up and down arrow keys. - Be careful with the delete shortcut; it can be ruthless.
- You can view (and set) shortcuts for Jupyter Notebooks by typing
ESC + H.
Shell commands
You can run ‘terminal’ commands (called shell commands) from Jupyter without switching to the terminal. The only thing you need to do is to add a ! in front of the command.
To test out this feature, run the following:
This will print the current working directory.
!cdThis will show you a list of all the files in your folder.
!dir
This will print the current working directory.
!pwdThis will show you a list of all the files in your folder.
!ls
Install Notebook 7
To ensure that all of you benefit from the latest updates and ensure uniformity, I would like you to use Jupyter Notebook version 7. For this, please follow these steps:
Remove the extensions to avoid possible version conflicts.
conda remove jupyter_contrib_nbextensionsInstall Notebook version 7
conda install -c conda-forge notebook=7.0.6 -y
Jupyter Notebooks are transitioning to Notebook version 7. As a consequence, it seems that Jupyter Extensions no longer work properly. Therefore, I have decided to skip the following step (Jupyter Extensions) in your setup.
Note that this will only cause minor differences in your coding experience.
Jupyter Extensions
Installing the extensions
You can add several useful features (e.g., code formatting, TOC, equation numbers) to your Jupyter notebooks by installing jupyter_contrib_nbextensions (documentation).
You can do this in the terminal by invoking the following.
conda install -c conda-forge jupyter_contrib_nbextensionsjupyter contrib nbextension install --userOnce you have finished installing, log out and restart Jupyter. You will then see a new tab, as shown in the image below.

Enabling the extensions
You will also see details of what each extension does by clicking on the name in the list. Finally, you can enable them simply by checking the box on the list. Here are some of the extensions I recommend.
- Equation Auto Numbering
- Table of Contents (2)
- Variable Inspector
- Code folding
- Autopep8
You must install the packageautopep8usingcondafor this to work.
Discover what features these have added by creating a new Jupyter Notebook. You will see several new buttons in the toolbar corresponding to some extensions. For instance, if you run x=10 in a code cell the Variable Inspector will show the details of the variable.
Shell Magic
One of the most useful goodies that come with Jupyter are the magic commands. For example, try the following:
%timeit x=10This will run for a while and then tell you statistics on how long the command takes to execute. The above is called line magic because it works (i.e., does the timing in this case) for only a single line. If you want to apply it to a whole cell, we will use the cell magic command %%timeit.
%%timeit
x=10
y=10
z=10You will only need the timeit in 73. However, there is more magic to be found. Use %lsmagic to discover what else is available.
The Kernel
You will have realised that there is more going on with Jupyter Notebooks than just Python. All the behind-the-scenes work of running a Jupyter Notebook is done by a sub-routine called the Kernel.
You can interrupt, restart, or shut down the kernel using the button in the toolbar. As you start coding with Python, you will need to do these because Jupyter sometimes misbehaves, and you can make blunders that require a reset.