How To Use IPython Jupyter Notebook To Develop Python Code

IPython is an enhanced Python interactive environment, you can run all Python source code in IPython. And it provide more features that make your development process easy. Jupyter notebook is a browser-server based website tool which can save your python souce code in a .ipynb notebook file, and then when you submit the python source code to your local jupyter webserver, it will start an ipython kernel to run the source code, and then display the result or error messages just below your python source code in the web page for interactive development. This article will introduce how to use ipython and jupyter notebook to develope interactive python souce code with examples.

1. Install IPython.

Before install IPython, you can run $ pip3 show ipython command to check whether it has been installed or not. This command will show it’s version and installation directory.

(base) :~$ pip3 show ipython
Name: ipython
Version: 7.3.0
Summary: IPython: Productive Interactive Computing
Author: The IPython Development Team
Author-email: [email protected]
License: BSD
Location: /home/zhaosong/.local/lib/python3.6/site-packages
Requires: pexpect, decorator, pickleshare, jedi, prompt-toolkit, backcall, pygments, traitlets, setuptools

If you do not install IPython, run $ pip3 install ipython to install it like below.

$ pip3 install ipython
Collecting ipython
  Downloading (768kB)
    100% |████████████████████████████████| 778kB 1.1MB/s 
Collecting jedi>=0.10 (from ipython)
  Downloading (178kB)
    100% |████████████████████████████████| 184kB 2.4MB/s 
Collecting setuptools>=18.5 (from ipython)
  Downloading (575kB)
    100% |████████████████████████████████| 583kB 1.8MB/s 
Collecting backcall (from ipython)
Collecting pexpect; sys_platform != "win32" (from ipython)
  Downloading (57kB)
    100% |████████████████████████████████| 61kB 3.0MB/s 
Collecting prompt-toolkit<2.1.0,>=2.0.0 (from ipython)
  Downloading (337kB)
    100% |████████████████████████████████| 337kB 2.1MB/s 
Collecting traitlets>=4.2 (from ipython)
  Downloading (74kB)
    100% |████████████████████████████████| 81kB 2.7MB/s 
Collecting decorator (from ipython)
Collecting pygments (from ipython)
  Downloading (849kB)
    100% |████████████████████████████████| 849kB 1.4MB/s 
Collecting pickleshare (from ipython)
Collecting parso>=0.3.0 (from jedi>=0.10->ipython)
  Downloading (93kB)
    100% |████████████████████████████████| 102kB 2.6MB/s 
Collecting ptyprocess>=0.5 (from pexpect; sys_platform != "win32"->ipython)
Collecting wcwidth (from prompt-toolkit<2.1.0,>=2.0.0->ipython)
Collecting six>=1.9.0 (from prompt-toolkit<2.1.0,>=2.0.0->ipython)
Collecting ipython-genutils (from traitlets>=4.2->ipython)
Building wheels for collected packages: backcall
  Running bdist_wheel for backcall ... done
  Stored in directory: /home/zhaosong/.cache/pip/wheels/98/b0/dd/29e28ff615af3dda4c67cab719dd51357597eabff926976b45
Successfully built backcall
Installing collected packages: parso, jedi, setuptools, backcall, ptyprocess, pexpect, wcwidth, six, prompt-toolkit, ipython-genutils, decorator, traitlets, pygments, pickleshare, ipython
Successfully installed backcall-0.1.0 decorator-4.3.2 ipython-7.3.0 ipython-genutils-0.2.0 jedi-0.13.3 parso-0.3.4 pexpect-4.6.0 pickleshare-0.7.5 prompt-toolkit-2.0.9 ptyprocess-0.6.0 pygments-2.3.1 setuptools-40.8.0 six-1.12.0 traitlets-4.3.2 wcwidth-0.1.7
(base) [email protected]:~$ pip3 show ipython
Name: ipython
Version: 7.3.0
Summary: IPython: Productive Interactive Computing
Author: The IPython Development Team
Author-email: [email protected]
License: BSD
Location: /home/zhaosong/.local/lib/python3.6/site-packages
Requires: decorator, pickleshare, setuptools, traitlets, jedi, pygments, pexpect, backcall, prompt-toolkit

2. Run Python Code In IPython .

Open a terminal and input $ ipython command. Then you can write python source code in the ipython console.

(base) [email protected]:~$ ipython
Python 3.7.1 (default, Dec 14 2018, 19:28:38) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: print('hello world')                                                                                                                                                                                
hello world

In [2]: a = 5                                                                                                                                                                                               

In [3]: b = 6                                                                                                                                                                                               

In [4]: a + b                                                                                                                                                                                               
Out[4]: 11

In [5]: def sum(a, b): 
   ...:     return a + b 

In [6]: sum(1,2)                                                                                                                                                                                            
Out[6]: 3

In [7]:

3. Install Jupyter NoteBook.

Jupyter notebook is a browser-server based website tool which can help you to write and run Python source code interactively. When you run it, it will start a web server, and then you can create working folder and notebook file ( which  file extension is .ipynb ) in it. In each notebook file, you can write text in line cell input text box and mark the text as Code ( python source code ), Markdown ( comments), Raw NBConvert or Heading type. Then you can submit the text to Jupyter web server to execute. If you submit Python source code, jupyter webserver will create a ipython process to run it, and the execution result will be displayed below the python source code in the web page immediately.

But before use it, you should install it first, you have two way to install it.

  1. Install Anaconda : Anaconda integrate with Jupyter by default, so after install anaconda, you can use Jupyter directly. You can refer How To Install Anaconda On Linux, Windows, MacOS Correctly.
  2. Use pip to install Jupyter : Open a terminal and run $ pip3 show jupyter to check whether jupyter has been installed or not. Below output means jupyter has been installed.
    ~$ pip3 show jupyter
    Name: jupyter
    Version: 1.0.0
    Summary: Jupyter metapackage. Install all the Jupyter components in one go.
    Author: Jupyter Development Team
    Author-email: [email protected]
    License: BSD
    Location: /home/zhaosong/.local/lib/python3.6/site-packages
    Requires: ipykernel, qtconsole, ipywidgets, notebook, nbconvert, jupyter-console
  3. If jupyter is not installed, run $ pip3 install jupyter to install it. It will download and install a lot of dependency packages like below.
    $ pip3 install jupyter
    Collecting jupyter
    Collecting jupyter-console (from jupyter)
    Collecting qtconsole (from jupyter)
      Downloading (113kB)
        100% |████████████████████████████████| 122kB 215kB/s 
    Collecting ipywidgets (from jupyter)
      Downloading (111kB)
        100% |████████████████████████████████| 112kB 640kB/s 
    Collecting notebook (from jupyter)
      Downloading (9.0MB)
        100% |████████████████████████████████| 9.0MB 189kB/s 
    Collecting nbconvert (from jupyter)
      Downloading (407kB)
        100% |████████████████████████████████| 409kB 1.9MB/s 
    Collecting ipykernel (from jupyter)
      Downloading (113kB)
        100% |████████████████████████████████| 122kB 4.2MB/s 
    Collecting jupyter-client (from jupyter-console->jupyter)
      Downloading (89kB)
        100% |████████████████████████████████| 92kB 4.8MB/s 
    Collecting pygments (from jupyter-console->jupyter)
      Using cached
    Collecting prompt-toolkit<2.1.0,>=2.0.0 (from jupyter-console->jupyter)
      Using cached
    Collecting ipython (from jupyter-console->jupyter)
      Using cached
    Collecting jupyter-core (from qtconsole->jupyter)
      Downloading (126kB)
        100% |████████████████████████████████| 133kB 5.0MB/s 
    Collecting traitlets (from qtconsole->jupyter)
      Using cached
    Collecting ipython-genutils (from qtconsole->jupyter)
      Using cached
    Collecting nbformat>=4.2.0 (from ipywidgets->jupyter)
      Downloading (155kB)
        100% |████████████████████████████████| 163kB 1.7MB/s 
    Collecting widgetsnbextension~=3.4.0 (from ipywidgets->jupyter)
      Downloading (2.2MB)
        100% |████████████████████████████████| 2.2MB 563kB/s 
    Collecting terminado>=0.8.1 (from notebook->jupyter)
    Collecting tornado>=4 (from notebook->jupyter)
      Downloading (481kB)
        100% |████████████████████████████████| 491kB 1.4MB/s 
    Collecting jinja2 (from notebook->jupyter)
      Downloading (126kB)
        100% |████████████████████████████████| 133kB 2.0MB/s 
    Collecting Send2Trash (from notebook->jupyter)
    Collecting prometheus-client (from notebook->jupyter)
    Collecting pyzmq>=17 (from notebook->jupyter)
      Downloading (1.1MB)
        100% |████████████████████████████████| 1.1MB 1.1MB/s 
    Collecting pandocfilters>=1.4.1 (from nbconvert->jupyter)
    Collecting testpath (from nbconvert->jupyter)
      Downloading (163kB)
        100% |████████████████████████████████| 163kB 3.2MB/s 
    Collecting bleach (from nbconvert->jupyter)
      Downloading (157kB)
        100% |████████████████████████████████| 163kB 4.2MB/s 
    Collecting defusedxml (from nbconvert->jupyter)
    Collecting mistune>=0.8.1 (from nbconvert->jupyter)
    Collecting entrypoints>=0.2.2 (from nbconvert->jupyter)
    Collecting python-dateutil>=2.1 (from jupyter-client->jupyter-console->jupyter)
      Downloading (226kB)
        100% |████████████████████████████████| 235kB 2.6MB/s 
    Collecting wcwidth (from prompt-toolkit<2.1.0,>=2.0.0->jupyter-console->jupyter)
      Using cached
    Collecting six>=1.9.0 (from prompt-toolkit<2.1.0,>=2.0.0->jupyter-console->jupyter)
      Using cached
    Collecting pexpect; sys_platform != "win32" (from ipython->jupyter-console->jupyter)
      Using cached
    Collecting setuptools>=18.5 (from ipython->jupyter-console->jupyter)
      Using cached
    Collecting pickleshare (from ipython->jupyter-console->jupyter)
      Using cached
    Collecting jedi>=0.10 (from ipython->jupyter-console->jupyter)
      Using cached
    Collecting decorator (from ipython->jupyter-console->jupyter)
      Using cached
    Collecting backcall (from ipython->jupyter-console->jupyter)
    Collecting jsonschema!=2.5.0,>=2.4 (from nbformat>=4.2.0->ipywidgets->jupyter)
      Downloading (54kB)
        100% |████████████████████████████████| 61kB 10.0MB/s 
    Collecting ptyprocess; os_name != "nt" (from terminado>=0.8.1->notebook->jupyter)
      Using cached
    Collecting MarkupSafe>=0.23 (from jinja2->notebook->jupyter)
    Collecting webencodings (from bleach->nbconvert->jupyter)
    Collecting parso>=0.3.0 (from jedi>=0.10->ipython->jupyter-console->jupyter)
      Using cached
    Collecting pyrsistent>=0.14.0 (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets->jupyter)
      Downloading (104kB)
        100% |████████████████████████████████| 112kB 4.3MB/s 
    Collecting attrs>=17.4.0 (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets->jupyter)
    Building wheels for collected packages: tornado, prometheus-client, pandocfilters, pyrsistent
      Running bdist_wheel for tornado ... done
      Stored in directory: /home/zhaosong/.cache/pip/wheels/09/a9/78/41e23ffcb122e8f4b02537f4480a276ec1ad760dd492373961
      Running bdist_wheel for prometheus-client ... done
      Stored in directory: /home/zhaosong/.cache/pip/wheels/4b/04/b8/3709c73e7453f311ebd46ad581b89642543213f995e2659b9e
      Running bdist_wheel for pandocfilters ... done
      Stored in directory: /home/zhaosong/.cache/pip/wheels/39/01/56/f1b08a6275acc59e846fa4c1e1b65dbc1919f20157d9e66c20
      Running bdist_wheel for pyrsistent ... done
      Stored in directory: /home/zhaosong/.cache/pip/wheels/83/59/9a/a037b9b3c3e93d9275ea0aff9d6064400f372879dfdab01afe
    Successfully built tornado prometheus-client pandocfilters pyrsistent
    Installing collected packages: six, python-dateutil, ipython-genutils, decorator, traitlets, jupyter-core, pyzmq, tornado, jupyter-client, pygments, wcwidth, prompt-toolkit, ptyprocess, pexpect, setuptools, pickleshare, parso, jedi, backcall, ipython, ipykernel, jupyter-console, qtconsole, pyrsistent, attrs, jsonschema, nbformat, terminado, MarkupSafe, jinja2, Send2Trash, pandocfilters, testpath, webencodings, bleach, defusedxml, mistune, entrypoints, nbconvert, prometheus-client, notebook, widgetsnbextension, ipywidgets, jupyter
    Successfully installed MarkupSafe-1.1.1 Send2Trash-1.5.0 attrs-19.1.0 backcall-0.1.0 bleach-3.1.0 decorator-4.3.2 defusedxml-0.5.0 entrypoints-0.3 ipykernel-5.1.0 ipython-7.3.0 ipython-genutils-0.2.0 ipywidgets-7.4.2 jedi-0.13.3 jinja2-2.10 jsonschema-3.0.1 jupyter-1.0.0 jupyter-client-5.2.4 jupyter-console-6.0.0 jupyter-core-4.4.0 mistune-0.8.4 nbconvert-5.4.1 nbformat-4.4.0 notebook-5.7.4 pandocfilters-1.4.2 parso-0.3.4 pexpect-4.6.0 pickleshare-0.7.5 prometheus-client-0.6.0 prompt-toolkit-2.0.9 ptyprocess-0.6.0 pygments-2.3.1 pyrsistent-0.14.11 python-dateutil-2.8.0 pyzmq-18.0.1 qtconsole-4.4.3 setuptools-40.8.0 six-1.12.0 terminado-0.8.1 testpath-0.4.2 tornado-6.0.1 traitlets-4.3.2 wcwidth-0.1.7 webencodings-0.5.1 widgetsnbextension-3.4.2

4. Run Jupyter Notebook Web Server.

After jupyter has been installed sucessfully, you can run $ jupyter notebook command in terminal to start the jupyter web server.

~$ jupyter notebook
[I 22:21:57.057 NotebookApp] Writing notebook server cookie secret to /run/user/1000/jupyter/notebook_cookie_secret
[I 22:21:57.234 NotebookApp] JupyterLab extension loaded from /home/zhaosong/anaconda3/lib/python3.7/site-packages/jupyterlab
[I 22:21:57.234 NotebookApp] JupyterLab application directory is /home/zhaosong/anaconda3/share/jupyter/lab
[I 22:21:57.235 NotebookApp] Serving notebooks from local directory: /home/zhaosong
[I 22:21:57.235 NotebookApp] The Jupyter Notebook is running at:
[I 22:21:57.236 NotebookApp] http://localhost:8888/?token=a1ccb7865e275163dbd751856698a601f6f69feb2627b266
[I 22:21:57.236 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 22:21:57.254 NotebookApp] 
    To access the notebook, open this file in a browser:
    Or copy and paste one of these URLs:

Copy and browse above http url http://localhost:8888/?token=a1ccb7865e275163dbd751856698a601f6f69feb2627b266 to go to jupyter website home page. Please do not lost the token generated in the url, if you do not provide the token, you will enter below error page that require you to input the token value.

access jupyter notebook website without token error

If you browse url http://localhost:8888/?token=a1ccb7865e275163dbd751856698a601f6f69feb2627b266 with correct token value, then you will get below jupyter home page.

jupyter home page

5. Execute Python Code In Jupyter Interactively.

  1. Click the New drop down menu list, and you can select Python 3 menu item to create a executable python 3 notebook file. Click Folder to create a working folder.
    create new jupyter notebook execute python file
  2. Click the file name Untitled.ipynb, you will go to the notebook editor page like below. Click Untitled in the top left corner, you can change the file name.
    jupyter ipynb file editor page
  3. Then you can input text in the line cell input text box. You can also specify the line cell text value type from the drop down list at the end of the tool bar icons. There are four text value types : Code ( python source code ), Markdown ( comments ), Raw NBConvert, Heading.
  4. If you select Code type for the line cell text, then click Run button will submit the line cell text value to jupyter server to run. Then jupyter will start a ipython kernel to run the python source code at server side.
  5. You can input all kind of python source code, such as define python function, import libraries even run matpolib code to generate chart, then the chart image will be displayed in the web page also.
0 0 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x