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 provides more features that make your development process easy. Jupyter notebook is a browser-server based website tool that can save your python source code in a .ipynb notebook file, and then when you submit the python source code to your local Jupyter web server, 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 develop interactive python source code with examples.

1. Install IPython.

  1. Before installing IPython, you can run the command $ pip3 show ipython 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
  2. 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) zhaosong@zhaosong-VirtualBox:~$ 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.

  1. Open a terminal and input $ ipython command. Then you can write python source code in the iPython console.
    (base) zhaosong@zhaosong-VirtualBox:~$ 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 that 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 a 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 the Jupyter webserver to execute. If you submit Python source code, the Jupyter web server will create an iPython process to run it, and the execution result will be displayed below the python source code on the web page immediately.

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

  1. Install Anaconda: Anaconda integrates with Jupyter by default, so after installing anaconda, you can use Jupyter directly. You can refer to 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.

  1. After Jupyter has been installed successfully, you can run the command $ jupyter notebook 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:
  2. Copy and browse the above HTTP url http://localhost:8888/?token=a1ccb7865e275163dbd751856698a601f6f69feb2627b266 to go to the Jupyter website home page. Please do not lose the token generated in the url, if you do not provide the token, you will enter the below error page that requires you to input the token value.

    Password or token:          Log in
    Token authentication is enabled
    If no password has been configured, you need to open the notebook server with its login token in
    the URL. or paste it above. This requirement will be lifted if you enable a password.
    The command:
           jupyter notebook list
    will show you the URLs of running severs with their tokens. which you can copy and paste into
    your browser. For example:
            Currently running servers:
            http://localhost:BB88/?token-cBde56fa. .. :: /Users/you/notebooks
    or you can paste just the token value into the password field on this page.
    See the documentation on how to enable a password in place of token authentication. if you
    would like to avoid dealing with random tokens.
  3. If you browse url http://localhost:8888/?token=a1ccb7865e275163dbd751856698a601f6f69feb2627b266 with the correct token value, then you will be directed to the Jupyter home page.

5. Execute Python Code In Jupyter Interactively.

  1. On the Jupyter notebook web server home page, click the New drop-down menu list on the top right corner, and you can select Python 3 menu item to create an executable python 3 notebook file. Click the Folder menu item to create a working folder.
  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 Jupyter notebook file name.
  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 toolbar 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 the Run button will submit the line cell text value to the Jupyter server to run. Then Jupyter will start an iPython kernel to run the python source code on the server-side.
  5. You can input all kinds of python source code, such as to define python function, import libraries even run matpolib code to generate the chart, then the chart image will be displayed on the web page also.

Leave a Comment

Your email address will not be published. Required fields are marked *

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