fbpx

How to Code in Python – Using Jupyter Notebook Guide

Starting the Jupyter Notebook Server

Now that you have Jupyter installed, let’s learn how to use it. To get started, all you need to do is open up your terminal application and go to a folder of your choice. I recommend using something like your Documents folder to start out with and create a subfolder there called Notebooks or something else that is easy to remember.

Then just go to that location in your terminal and run the following command:

$ jupyter notebook

This will start up Jupyter and your default browser should start (or open a new tab) to the following URL: http://localhost:8888/tree

Your browser should now look something like this:

Jupyter Notebook Server

Note that right now you are not actually running a Notebook, but instead you are just running the Notebook server. Let’s actually create a Notebook now!

Creating a Notebook

Now that you know how to start a Notebook server, you should probably learn how to create an actual Notebook document.

All you need to do is click on the New button (upper right), and it will open up a list of choices. On my machine, I happen to have Python 2 and Python 3 installed, so I can create a Notebook that uses either of these. For simplicity’s sake, let’s choose Python 3.

Your web page should now look like this:

New Jupyter Notebook

Naming

You will notice that at the top of the page is the word Untitled. This is the title for the page and the name of your Notebook. Since that isn’t a very descriptive name, let’s change it!

Just move your mouse over the word Untitled and click on the text. You should now see an in-browser dialog titled Rename Notebook. Let’s rename this one to Hello Jupyter:

Hello World in Jupyter

Running Cells

A Notebook’s cell defaults to using code whenever you first create one, and that cell uses the kernel that you chose when you started your Notebook.

In this case, you started yours with Python 3 as your kernel, so that means you can write Python code in your code cells. Since your initial Notebook has only one empty cell in it, the Notebook can’t really do anything.

Thus, to verify that everything is working as it should, you can add some Python code to the cell and try running its contents.

Let’s try adding the following code to that cell:

print('Hello Jupyter!')

Running a cell means that you will execute the cell’s contents. To execute a cell, you can just select the cell and click the Run button that is in the row of buttons along the top. It’s towards the middle. If you prefer using your keyboard, you can just press Shift+Enter.

When I ran the code above, the output looked like this:

Running a Jupyter Notebook Cell

If you have multiple cells in your Notebook, and you run the cells in order, you can share your variables and imports across cells. This makes it easy to separate out your code into logical chunks without needing to reimport libraries or recreate variables or functions in every cell.

When you run a cell, you will notice that there are some square braces next to the word In to the left of the cell. The square braces will auto fill with a number that indicates the order that you ran the cells. For example, if you open a fresh Notebook and run the first cell at the top of the Notebook, the square braces will fill with the number 1.

The Menus

The Jupyter Notebook has several menus that you can use to interact with your Notebook. The menu runs along the top of the Notebook just like menus do in other applications. Here is a list of the current menus:

  • File
  • Edit
  • View
  • Insert
  • Cell
  • Kernel
  • Widgets
  • Help

Let’s go over the menus one by one. This article won’t go into detail for every single option in every menu, but it will focus on the items that are unique to the Notebook application.

Learn and Earn More-   October 2019 Professional IT Training: website design, ethical hacking, digital marketing, data science abuja

The first menu is the File menu. In it, you can create a new Notebook or open a preexisting one. This is also where you would go to rename a Notebook. I think the most interesting menu item is the Save and Checkpoint option. This allows you to create checkpoints that you can roll back to if you need to.

Next is the Edit menu. Here you can cut, copy, and paste cells. This is also where you would go if you wanted to delete, split, or merge a cell. You can reorder cells here too.

Note that some of the items in this menu are greyed out. The reason for this is that they do not apply to the currently selected cell. For example, a code cell cannot have an image inserted into it, but a Markdown cell can. If you see a greyed out menu item, try changing the cell’s type and see if the item becomes available to use.

The View menu is useful for toggling the visibility of the header and toolbar. You can also toggle Line Numbers within cells on or off. This is also where you would go if you want to mess about with the cell’s toolbar.

The Insert menu is just for inserting cells above or below the currently selected cell.

The Cell menu allows you to run one cell, a group of cells, or all the cells. You can also go here to change a cell’s type, although I personally find the toolbar to be more intuitive for that.

The other handy feature in this menu is the ability to clear a cell’s output. If you are planning to share your Notebook with others, you will probably want to clear the output first so that the next person can run the cells themselves.

The Kernel cell is for working with the kernel that is running in the background. Here you can restart the kernel, reconnect to it, shut it down, or even change which kernel your Notebook is using.

You probably won’t be working with the Kernel all that often, but there are times when you are debugging a Notebook that you will find you need to restart the Kernel. When that happens, this is where you would go.

The Widgets menu is for saving and clearing widget state. Widgets are basically JavaScript widgets that you can add to your cells to make dynamic content using Python (or another Kernel).

Finally you have the Help menu, which is where you go to learn about the Notebook’s keyboard shortcuts, a user interface tour, and lots of reference material.

Starting Terminals and Other Things

Jupyter Notebook also allows you to start more than just Notebooks. You can also create a text file, a folder, or a Terminal in your browser. Go back to the home page that opened when you first started the Jupyter server at http://localhost:8888/tree. Go to the New button and choose one of the other options.

Learn and Earn More-   Protect yourself from Cyber Espionage: SOUTECH Ventures cyber security tips and techniques

The Terminal is probably the most interesting of the bunch, as it is running your operating systems terminal in the browser. This allows you to run bash, Powershell, and so on in your browser and run any shell command that you might need to there.

Viewing What’s Running

Also on the home page of your Jupyter server (http://localhost:8888/tree) are two other tabs: Running and Clusters.

The Running tab will tell you which Notebooks and Terminals you are currently running. This is useful for when you want to shut down your server but you need to make sure that you have saved all your data. Fortunately, Notebooks auto-save pretty frequently, so you rarely lose data. But it’s good to be able to see what’s running when you need to.

The other nice thing about this tab is that you can go through your running applications and shut them down there.

Adding Rich Content

Jupyter Notebook supports adding rich content to its cells. In this section, you will get an overview of just some of the things you can do with your cells using Markup and Code.

Cell Types

There are technically four cell types: Code, Markdown, Raw NBConvert, and Heading.

The Heading cell type is no longer supported and will display a dialog that says as much. Instead, you are supposed to use Markdown for your Headings.

The Raw NBConvert cell type is only intended for special use cases when using the nbconvert command-line tool. Basically it allows you to control the formatting in a very specific way when converting from a Notebook to another format.

The primary cell types that you will use are the Code and Markdown cell types. You have already learned how code cells work, so let’s learn how to style your text with Markdown.

Styling Your Text

Jupyter Notebook supports Markdown, which is a markup language that is a superset of HTML. This tutorial will cover some of the basics of what you can do with Markdown.

Set a new cell to Markdown and then add the following text to the cell:

Italicized Text in Jupyter Notebook

When you run the cell, the output should look like this:

Italicized Text Output in Jupyter Notebook

If you would prefer to bold your text, use a double underscore or double asterisk.

Headers

Creating headers in Markdown is also quite simple. You just have to use the humble pound sign. The more pound signs you use, the smaller the header. Jupyter Notebook even kind of previews it for you:

Header Markdown in Jupyter Notebooks

Then when you run the cell, you will end up with a nicely formatted header:

Headers in Jupyter Notebooks

Creating Lists

You can create a list (bullet points) by using dashes, plus signs, or asterisks. Here is an example:

Markdown Lists in Jupyter Notebooks

Code and Syntax Highlighting

If you want to insert a code example that you don’t want your end user to actually run, you can use Markdown to insert it. For inline code highlighting, just surround the code with backticks. If you want to insert a block of code, you can use triple backticks and also specify the programming language:

Highlighting Code Syntax

Exporting Notebooks

When you are working with Jupyter Notebooks, you will find that you need to share your results with non-technical people. When that happens, you can use the nbconvert tool which comes with Jupyter Notebook to convert or export your Notebook into one of the following formats:

  • HTML
  • LaTeX
  • PDF
  • RevealJS
  • Markdown
  • ReStructured Text
  • Executable script
Learn and Earn More-   Data Science Tutorial Guide: Nigerian Population Data analysis Case Study

Shortcuts

As a developer, I like to use shortcuts and snippets as much as I can. They just make writing code a lot easier and faster. I like to follow one rule:

If you start doing some action with the mouse, stop and think if there is a shortcut. If there is a one – use it.

When I started using Jupyter Notebook I didn’t know that there are shortcuts for this tool. Several times, I changed my cell type from code to markdown and I didn’t know how. As you can guess this caused me a lot of headache. One day I just saw that there is a Help > Keyboard Shortcuts link in the menu bar. To my surprise, it turned out that Jupyter Notebook has a ton of shortcuts.

In this article, I’ll show you my favorite ones. Note that the shortcuts are for Windows and Linux users. Anyway, for the Mac users, they’re different buttons for CtrlShift, and Alt:

  • Ctrl: command key 
  • Shift: Shift 
  • Alt: option 

First, we need to know that they are 2 modes in the Jupyter Notebook Appcommand mode and edit mode. I’ll start with the shortcuts shared between the two modes.

Shortcuts in both 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

While in command mode (press Esc to activate):

  • Enter take you into edit mode
  • H show all shortcuts
  • Up select cell above
  • Down select cell below
  • Shift + Up extend selected cells above
  • Shift + Down extend selected cells below
  • A insert cell above
  • B insert cell below
  • X cut selected cells
  • C copy selected cells
  • V paste cells below
  • Shift + V paste cells above
  • D, D (press the key twice) delete selected cells
  • Z undo cell deletion
  • S Save and Checkpoint
  • Y change the cell type to Code
  • M change the cell type to Markdown
  • P open the command palette.
    This dialog helps you run any command by name. It’s really useful if you don’t know some shortcut or when you don’t have a shortcut for the wanted command.
Command Palette
  • Shift + Space scroll notebook up
  • Space scroll notebook down

While in edit mode (pressEnter to activate)

  • 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 cell end
  • Ctrl + Left go one word left
  • Ctrl + Right go one word right
  • Ctrl + Shift + P open the command palette
  • Down move cursor down
  • Up move cursor up

These are the shortcuts I use in my daily work. If you still need something that is not mentioned here you can find it in the keyboard shortcuts dialog (H). You can also edit existing or add more shortcuts from the Help > Edit Keyboard Shortcuts link in the menu bar. Clicking the link will open a dialog. At the bottom of it there are rules for adding or editing shortcuts. You need to use hyphens - to represent keys that should be pressed at the same time.



WhatsApp chat