Sunday, July 17, 2016

Coding Python on the Web Browser - Introduction to IPython (Jupyter) notebook

Hello there,

I was once asked by a PHP programmer who was trying to lay his hands on Python Programming language that: whether there is a way to program in Python on the Web (browser)? That is using the Web Browser as the editor? 

Since he was used to PHP development on the browser with the likes of PHP content management systems (Joomla, Drupal, WordPress etc), he will also like to learn or code with Python on the browser.

Here I present to you one of the ways to Program with Python language on the Web browser.

What is IPython or Jupyter? 

IPython is an interactive shell for the Python programming language that offers enhanced introspection, additional shell syntax, tab completion and rich history.

IPython notebook is a web-based interactive computational environment for Python Programming language where you can combine code execution, text, mathematics, plots and rich media into a single document.

Jupyter on the other hand is the evolution of IPython notebooks. The Jupyter Notebook used to be called the IPython Notebook.

IPython focused solely on interactive Python Programming, while the new project name Jupyter now supports approximately forty (40) Programming languages including R, C#, F#, C++, Ruby, PERL and Julia as seen below.

These notebooks are normal files that can be shared with colleagues, converted to other formats such as HTML, ipynb, dashboards, slides, PDF etc. This makes it easy to give your colleagues a document they can read immediately without having to install anything.

Installing Jupyter Notebook

There several ways to install Jupyter Notebook on local computers, however one of the simplest is to install using python distribution called Anaconda and Enthought. These two python distributions allow you to download a desktop version of Jupyter Notebook.

If you're using Windows Operating System, I recommend you download and install Anaconda, which is a free bundled installer of Python together with many other useful tools (including Jupyter Notebook.)

Once you've installed the Anaconda, you start Jupyter notebook by navigating to windows start button and select Anaconda folder, you see "Jupyter notebook" click it. This will open Jupyter Command Prompt and a browser on a localhost to the URL of your Notebooks, by default: http://localhost:8888/tree or

Learn more about the installation here.

I you wish to try its online, then you can go to this url: (Hosted by Rackspace).

Starting a New Notebook

To start a new notebook, click on "New" and select Python[root] or you select the programming language you are using. (Note: You can upload an existing notebook by using the upload button.)

After selecting your language from above, a new tab will open with you notebook dashboard as seen below:-

Here, you got all sort of tools in the menus and on the tools bar to manipulate your new jupyter notebook. Lets demonstrate some of the things you can do

Rename Title

The current title name reads "Untitled". To rename the title of our new jupyter notebook, go to the "File" menu and select "Rename..." or simply double click on the tile name. A dialog box will appear where you can enter the new name you want an click on 'Ok" button to apply it.

Now, the renamed notebook will look like this;

How to run code

Code cells are based on an input and output format.
The empty box at the top is the first "cell" for entering Python code.

Note: that "code" most be selected from the little drop down box above for your code to run.

Try typing something like print("Hello World") into the cell. To run the code in the cell and see the output, click the Run button (play icon) on the toolbar, or type Shift-Enter.

How to write regular and headers text

You can also turn a cell into a regular and headers text by switching the drop down box above from "code" to "markdown". And any text (This is a markdown cell (regular text, not code)) you entered into the cell will appear just like a comment. It is useful for adding personal notes to your code.

You can change the markdown text into a header text by adding "#" sign at the beginning of the text as seen below.

You can run any python expression on the Jupyter Notebook just as you do on the interactive prompt. For example;-

Help commands

You can get help of functions in Jupyter Notebook by;-
~ help() for interactive help
~ help(object) for help about object
~ type the name of a Python function or module and end it with a "?"

To learn more about Jupyter Notebook, use the "Help" menu to navigate the docmentation and other useful tools. Or access the official website of Jupyter Notebook for more.

Happy reading.

No comments:

Post a Comment