• Magdalena Konkiewicz

9 things you did not know about jupyter notebook


https://pixabay.com/photos/office-desk-computer-technology-1834294/#

Introduction


As a Data Scientist, you will use jupyter notebook on an everyday basis. It is crucial that you get as familiar with this tool as possible.


In this article, I will share some Jupyter notebook tips that I use when working with this tool. These tips will help you to get more familiar with the notebook itself and will boost your productivity.


Let's get started.



1. You can run shell commands from jupyter notebook


Many of you will be familiar with running python code in Jupyter notebook and using markdown to create descriptions and text. Some may not know that you can run shell commands from the notebook itself.


To do it, you input the command in a code cell. The format of the command is the same as if it was in the shell but it is additionally proceeded by the exclamation mark (!).


For example, you can use install packages with pip.


!pip install pandas

Or you can check what version of the package you have installed.


!pip show pandas 

You can also use standard shell command such as:


!pwd
!ls

All of these without opening the shell itself. This means no time lost by switching between notebook and terminal!



2. Jupyter notebook has magic commands.


If you play with shell commands you will notice that some of them cannot execute. An example of this occurring is 'cd' command. In order to confirm this, you can run the following code in jupyter notebook.


!pwd
!cd <directory name>
!pwd

Note that the first and second 'pwd' command will yield the same path. The reason for this is the fact that these shell commands are executed in the temporary subshell.


In order to effectively change the directory, you will need a magic command.


%cd <directory>

Magic commands are special commands to help you with running and analyzing data in your notebook. They either proceeded by % if they are on one line of code or by %% if they are written on several lines.


One of the most useful magic commands is a command that lists all magic commands.



%lsmagic

Explaining all magic commands would require the whole new article so let's limit ourselves to just a few examples.


You can run python file from your jupyter notebook using:


%run <file name>

Or you can time the execution of the cell with the following code.


%% time

<your code>


You probably have seen this magic command but did not realize what it was.


%matplotlib inline

The above is the magic command that accompanies almost every notebook. It renders your graphs within the notebook.



3. You can use shortcuts to navigate faster through your notebook.


Have you been navigating thought the notebook using graphical user interface?


Stop!

It's time to learn jupyter notebook shortcuts. You can use "Enter" and "Esc" to switch between command and edit mode. Once you are in command mode you can use the following shortcuts:


  • a add a new cell above the current one,

  • b add a new cell below the current one,

  • d + d (d pressed twice) deleting current cell,

  • m change cell from code to markdown,

  • y change cell from markdown to code,

  • up and down arrows use arrows to change the current cell,

  • shift + enter (pressed together) run code in the cell.


Still not sure how to do it? I have written an article in the past that explains it better:

Jupyter Notebook Keyboard Shortcuts for Beginners.



4. You can suppress the output of a final function with a semicolon.


If you have been working with Jupyter notebook for a while you probably have noticed that it automatically renders the output of the last function that was executed by the cell. This is usually the behavior that we want, but not in all cases!


It is especially annoying when you render graphs in the notebook. Let's see the example or plotting sine function.


%matplotlib inline

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

# Data for plotting sin function
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

plt.plot(t, s);

If you run this in Jupyter notebook you will get an output similar to this:


Not that I have marked in red the output of the plot. This info is usually not relevant for our analysis and we can suppress it by adding a semicolon to the last line of the code block.


plt.plot(t, s);


5. Jupyter notebook has built-in autocomplete functionality.


Some of you may know this already, but for those who do not this will be a very useful feature. In order to use it, you can start typing the code and press the "Tab" button in order to see the autocomplete suggestions.


If you want to learn more details about autocomplete check out this article: Jupyter notebook autocompletion.



6. You can export the Jupyter notebook as an HTML file.


You may have noticed that Jupyter notebooks are saved as .ipynb files. If you send this to a fellow Data Scientist or a programmer there will probably be able to open it with no problems.


But what happens you want to share your notebook with non-technical people? You can send them HTML version and they will be able to see the rendered version of your notebook.


It is very simple. Just navigate to File -> Downloas as -> .html.





7. You can create presentations from Jupyter notebook.


Have you been using Microsoft PowerPoint or Google slides to create presentations? While those are good options there may be better ways to create a technical presentation.


You can create a presentation using the Jupyter notebook itself. The advantage of this is that you can reuse the content that you have used for the notebook itself. It allows you to choose which cells you want to use for the presentation, how to structure them, and which slides to skip.


Once your notebook is ready you can create a presentation by going to View -> Cell Toolbar -> Slideshow.



Once you have done this each cell should have a Slide Type option in the right-hand corner of the cell. You can choose the sell to be slide, sub-slide, fragment, note, or skip it.



Once you decide on each slide type you can render your presentation by running the following command in the terminal window:


jupyter nbconvert <notebook name> --to slides --post serve

Or by running the following command from the notebook itself.


!jupyter nbconvert <notebook name> --to slides --post serve

This should render the presentation in the new tab that is run in your localhost.


You are able to navigate through the presentation using the arrows at the bottom right corner.


It takes time to get used to Jupyter notebook presentations so I suggest that you play with it a bit and experiment with different slide types in order to learn how to use them effectively. Once you get a grasp of it, jupyter notebooks may become your favorite tool to create technical presentations.



8. You can use LaTeX for formulas in Jupyter notebooks.


This is a simple trick to be able to type mathematical formulas in a Jupyter notebook. You can use LaTeX syntax to create a formula and then embed it in the markdown cell.


Let’s have a look at some examples. There are actually several ways you can do that depending on where you want your formula to be rendered.

If you want the mathematical code to be in line with the text you can use a single dollar sign.


This is an example of $\sqrt{2}$

Or if you want the formula to be rendered in the middle of the cell you can use two dollar signs ($$) as shown below.


$$\sqrt{2}$$


Have a lookat this more complicated formula and see how beautifully it is rendered in the notebook!


$$\sum_{i=1}^{n}i=\frac{n(n+1)}{2}$$



9. You can install extensions to further enhance Jupyter notebook capabilities.


Do you think Jupyter notebook's capabilities are not enough? You can actually install extensions in order to get access to some additional features.


A while ago I wrote an article where I have presented my Top 9 Jupyter Notebook extensions. Read it if you want to learn how to enable extensions in your notebook and how to use the following ones:

  • Spellchecker,

  • Table of contents,

  • Collapsible headings,

  • Autopep8,

  • ExecuteTime,

  • Toggle all line numbers,

  • Variable Inspector,

  • Hide code,

  • Skip-traceback.



Summary


Those were my favorite tips in respect to the practical use of Jupyter notebooks. It is an amazing tool and it is a shame that sometimes we do not use its all capabilities.


I hope you have learned something new and you will be able to replicate some of the tips while you lunch jupyter notebook next time.



288 views0 comments

Recent Posts

See All