Pankaj Mathur
Published in

Pankaj Mathur

How to do interactive programming in Browser via Jupyter Notebook? A detail Guide..

In this article, we will be learning “How to do interactive programming in Browsers via Jupyter Notebook” and will be covering:

  1. What are Jupyter Notebooks?
  2. Why Jupyter Notebook?
  3. Installing & Launching Jupyter Notebook
  4. Quick Introduction to Notebook Interface
  5. Code & Markdown Cells
  6. CheatSheet of Notebook Keyboard Shortcuts
  7. Perform Special Operations using Magic Keywords
  8. Creating HTML Pages from Jupyter Notebook
  9. Creating Beautiful SlideShows from Jupyter Notebooks

What are Jupyter Notebooks?
Yes, gone are those days, at least for Python based Data Scientists, Machine Learners, Deep Learners or Software Engineers, when for analyzing quick data or creating research project, you will have to work in various terminal, either with the normal Python shell or with IPython, then you will create your visualizations for math equation and graphs in separate windows, and then write an in-depth documentation in a separate document software, along with managing various scripts for functions and classes and just when you thought you were almost done, you end up wasting huge amount of time on finding a not so friendly third-party tools to just to share all of this on web…

with the emergence of Literate Programming, all these steps have vanished in a breeze and Jupyter Notebooks are the reason behind it.

So, what is a Jupyter notebook? It is a web-based editor, that allows you to combine explanatory text, math equations, code, and visualizations all in one easily shareable document. For example, here’s is a list of world most popular jupyter notebooks which showcase research project in the field of Psychology & Neuroscience, Physics, Chemistry & Biology, Mathematics, Economics & Finance, Earth Science & Geo-Spatial Data, Social data, Signal, Sound and Image Processing and Natural Language Processing.

Jupyter Notebooks are also rendered automatically on GitHub. It’s a great feature that lets you easily share your work. Most commonly people use http://nbviewer.jupyter.org/ that renders notebooks directly from your GitHub repo or from notebooks stored elsewhere.

Literate Programming
Jupyter Notebooks are a form of literate programming, introduced by Donald Knuth, in which a program is given as an explanation of the program logic in a natural language, such as English, interspersed with snippets of macros and traditional source code, from which a compilable source code can be generated.

The literate programming paradigm, as conceived by Knuth, represents a move away from writing programs in the manner and order imposed by the computer, and instead enables programmers to develop programs in the order demanded by the logic and flow of their thoughts. Literate programs are written as an uninterrupted exposition of logic in an ordinary human language, much like the text of an essay, in which macros are included to hide abstractions and traditional source code.

Installing & Launching Jupyter Notebook

By far the easiest way to install Jupyter is with Anaconda. Jupyter notebooks automatically come with the distribution. (If you haven’t Please look at my other article to learn more about Anaconda & Conda environment)

You’ll be able to use jupyter notebooks from the default environment. If you haven’t installed standard anaconda package, which comes with the jupyter notebook, you can still install jupyter notebook by using below command, if you have already jupyter notebook and you will type this command then it will just try to update the current version with new one.

conda install jupyter notebook

If you are still not using Anaconda (& not convinced by my detail article What is Anaconda and Why should I bother about it?) then you can still install only Jupyter notebooks package via pip instead of whole anaconda environment, by using the command:

pip install jupyter notebook

Launching the notebook server
To start a notebook server, enter

jupyter notebook

in your terminal or console. This will start the jupyter notebook server in the directory where you ran the command in. That means any notebook files will be saved in that same directory. Ideally, you’d want to start the jupyter notebook server in the directory where you are saving your projects notebooks. However, you can navigate through your file system to any directory, where your notebooks are stored. For example, I started jupyter notebook in my root directory of mac but my notebook live in the deep-learning folder, so I will just navigate to the deep-learning folder and open the existing ipython notebook files in the jupyter notebook there.

Also, look at the web address of this browser window, When you run the command, the jupyter notebook server home by default runs at http://localhost:8888

Here, note that localhost can also be replaced with your machine IP and 8888 is the port of the jupyter notebook server is communicating on you can also type http://your_computer_ip:8888 to access it, for example, my machine IP 127.0.0.1, so I can access Jupyter Notebook directly via http://127.0.0.1:8888.

Now, as long as the jupyter notebook server is still running, you can always go back to its main page by going to http://localhost:8888 in your browser.

At this point, If you start another jupyter notebook server, without closing the previous server, the new notebook sever will just increment the port number and it will run on port 8889. Then, you’d connect to it at http://localhost:8889. Every additional instance of jupyter notebook server will increment the port number like this.

Now, if you look at your current jupyter notebook page, on the right side, you can see the option to create a new notebook by clicking on “New”. This will create a new notebook, text file, folder, or terminal.

Also, here you will see different python kernel available for use in this notebook. Python [default] and If you run a Jupyter notebook server from a conda environment, you’ll also be able to choose a kernel from any of the other environments (see below). To create a new notebook, without any pre-configured conda environment and directly on conda root environment, which is popular way to use jupyter notebook is to use Python [conda root]

Quick Introduction to Notebook Interface
When you will create a new notebook by choosing Python [conda root], you should see something like this:

Go ahead and check out what each menu item has to offer to you.

When you will see below menu items and toolbar, You’ll see a little box with the name “In [1]: “ outlined in green. This is called a cell. Cells are where you write and run your code. You can also change it to render Markdown, a popular formatting syntax for writing web content. We will cover Markdown in more detail later. In the toolbar, under menu items, click “Code” to change it to Markdown and back. The little play button runs the cell, and the up and down arrows move cells up and down

When you run a code cell, the output is displayed below the cell. The cell also gets numbered, you see In [1]: on the left. This lets you know the code was run and the order if you run multiple cells. Running the cell in Markdown mode renders the Markdown as text.

The toolbar
let’s go through the toolbar one by one, starting from the left:

The first symbol which looks like a floppy disk is for “save,” It saves the notebook!
The + button creates a new cell in the notebook below.
Then, the third button with scissor shape, is to cut cell, fourth symbol with two similar transparent page is for copy cell, and the fifth symbol with one dark page and one transparent page is for paste cells from one place to another.
The up and down arrows are for moving from up and down from one cell to another
The next 3 buttons are standard Run, stop, restart buttons which run, stop and restart The next drop-down button is for choosing between cell type: code, Markdown, raw text, and header

The next button is Command palette, which will bring up a panel with a search bar where you can search for various commands. This is really helpful for speeding up your workflow as you don’t need to search around in the menus with your mouse.

Just open the command palette and type in what you want to do. For instance, if you want to see all the keyboard shortcuts of the jupyter notebook, you can choose keyboard shortcuts in command palette. (on the side note, you can also get keyboard shortcuts pop up by using fn + H hotkeys on Mac OSX)

The next button is Cell toolbar, gives various options for cells such as using them as slides

The next cloud looking button is to sign in to your anaconda cloud account

The next giftbox looking button is dedicated to slideshows controls and it will open a pallet from right side of notebook, with the first option will play slideshow and show slide show play options, second option will open each slide pallet in downside of your notebook, Third option will open change theme settings and fourth option is to get help on nbpresent aka slideshow functionality of jupyter notebooks.

Renaming Notebook
At the top, you see the title. Click on this to rename the notebook.

Quick Tips

  1. Over on the right is the kernel type and next to it, a little circle. When the kernel is running a cell, it will fill in. For most operations which run quickly, it won’t fill in. It’s a little indicator that’s let you know whether longer running code is actually running.
  2. Along with the save button in the toolbar, notebooks are automatically saved periodically. The most recent save is noted to the right of the title. You can save manually with the save button, or by pressing escape then s on your keyboard. The escape key changes to command mode and press “s” the shortcut for “save.” I’ll cover command mode and keyboard shortcuts later.
  3. In the “File” menu, you can download the notebook in multiple formats. You’ll often want to download it as an HTML file to share with others who aren’t using Jupyter. Also, you can download the notebook as a normal Python file where all the code will run as normal. The Markdown and reST formats are great for using notebooks in blogs or documentation.

Code and Markdown Cells
Code cells
Most of your work in notebooks will be done in code cells. This is where you write your code and it gets executed. In code cells you can write any code, assigning variables, defining functions and classes, importing packages, and more. Any code executed in one cell is available in all other cells.

To give you some practice, I created a notebook you can work through. Download the notebook Working With Code Cells below then run it from your own notebook server. (In your terminal, change to the directory with the notebook file, then enter jupyter notebook) Your browser might try to open the notebook file without downloading it. If that happens, right-click on the link then choose “Save Link As…”

Markdown cells
As mentioned before, cells can also be used for text written in Markdown. Markdown is a formatting syntax that allows you to include links, style text as bold or italicized, and format code. As with code cells, you press Shift + Enter or Control + Enter to run the Markdown cell, where it will render the Markdown to formatted text. Including text allows you to write a narrative alongside your code, as well as documenting your code and the thoughts that went into it.

You can find the documentation here, but I’ll provide a short primer.

Headers
You can write headers using the pound/hash/octothorpe symbol # placed before the text. One # renders as an h1 header, two ## renders an h2, and so on. Looks like this:

# Header 1
## Header 2
### Header 3

renders as

Header 1

Header 2

Header 3

Links
Linking in Markdown is done by enclosing text in square brackets and the URL in parentheses, like this

[pankajmathur.com home page] (https://pankajmathur.com)

for a link to PankajMathur.com home page.

Emphasis
You can add emphasis through bold or italics with asterisks or underscores (* or _). For italics, wrap the text in one asterisk or underscore,

_iPhone_

or

*iPhone*

renders iPhone.

Bold text uses two symbols,

**Android**

or

__Android__

looks like Android.

Either asterisks or underscores are fine as long as you use the same symbol on both sides of the text.

Code
There are two different ways to display code, in line with text and as a code block separated from the text. To format inline code, wrap the text in backticks. For example, `request.get()` renders as

request.get()

To create a code block, start a new line and wrap the text in three backticks

```
import requests
response = requests.get('https://www.pankajmathur.com')
```

or indent each line of the code block with four spaces.

import requests
response = requests.get('https://www.pankajmathur.com')

Math expressions
You can create math expressions in Markdown cells using LaTeX symbols. Notebooks use MathJax to render the LaTeX symbols as math symbols. To start math mode, wrap the LaTeX in dollar signs $y = mx + b$ for inline math. For a math block, use double dollar signs,

$$
y = \frac{a}{b+c}
$$

CheatSheet of Notebook Keyboard Shortcuts
Notebooks come with a bunch of keyboard shortcuts that let you use your keyboard to interact with the cells, instead of using the mouse and toolbars. They take a bit of time to get used to, but when you’re proficient with the shortcuts you’ll be much faster at working in notebooks.
Perform Special Operations using Magic Keywords
Magic keywords
Magic keywords are special commands you can run in cells that let you control the notebook itself or perform system calls such as changing directories. For example, you can set up matplotlib to work interactively in the notebook with %matplotlib.

Magic commands are preceded with one or two percent signs (% or %%) for line magic and cell magics, respectively. Line magics apply only to the line the magic command is written on, while cell magics apply to the whole cell.

NOTE: These magic keywords are specific to the normal Python kernel. If you are using other kernels, these most likely won’t work.

Timing code
At some point, you’ll probably spend some effort optimizing code to run faster. Timing how quickly your code runs is essential for this optimization. You can use the timeit magic command to time how long it takes for a function to run, like so:

If you want to time how long it takes for a whole cell to run, you’d use

%%timeit

Embedding visualizations in notebooks
As mentioned before, notebooks let you embed images along with text and code. This is most useful when you’re using matplotlib or other plotting packages to create visualizations. You can use

%matplotlib

to set up matplotlib for interactive use in the notebook. By default, figures will render in their own window. However, you can pass arguments to the command to select a specific “backend”, the software that renders the image. To render figures directly in the notebook, you should use the inline backend with the command

%matplotlib inline

Tip: On higher resolution screens such as Retina displays, the default images in notebooks can look blurry, so you should use:

%config InlineBackend.figure_format = 'retina'

so, the final command will be

%matplotlib inline
%config InlineBackend.figure_format = 'retina'

to render higher resolution images.

Debugging in the Notebook
With the Python kernel, you can turn on the interactive debugger using the magic command

%pdb

When you cause an error, you’ll be able to inspect the variables in the current namespace.
Above you can see I tried to sum up a string which gives an error. The debugger raises the error and provides a prompt for inspecting your code.

Read more about PDB in the documentation. To quit the debugger, simply enter q in the prompt.

More Magic
There are a whole bunch of other magic commands, I just touched on a few of the ones you’ll use the most often. To learn more about them, here’s the list of all available magic commands.

Creating HTML Pages from Notebooks
Notebooks are just big JSON files with the extension .ipynb.
Since notebooks are JSON, it is simple to convert them to other formats. Jupyter comes with a utility called nbconvert for converting to HTML, Markdown, slideshows, etc.

For example, to convert a notebook to an HTML file, in your terminal use

jupyter nbconvert --to html notebook.ipynb

Converting to HTML is useful for sharing your notebooks with others who aren’t using notebooks. Markdown is great for including a notebook in blogs and other text editors that accept Markdown formatting.
Creating Beautiful SlideShows
Creating slideshows from notebooks is one of the awesome hidden features of the jupyter notebook. To create a slideshow, in your terminal use

ipython nbconvert  --to slides your_slides.ipynb

You can see an example of a slideshow here

I hope, this detailed article will help you to jumpstart your interactive programming experience in Browsers via Jupyter Notebook.

Please do let me know your thoughts, questions under the comments section. I would really appreciate getting some feedback on this article & ideas to improve it.
In the meanwhile, Happy Thinking…

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store