Jupyter Notebook Magic Commands Introduction

Magic commands are special commands used to control the notebook. They run in code units, starting with % or %%, the former controls a single line, the latter controls a unit. For example, to get the running time of the code, you can run %timeit, for code debugging you can run %pdb. You can run %lsmagic to view all supported magic commands like below.

%lsmagic

Available line magics:
%alias  %alias_magic  %autoawait  %autocall  %automagic  %autosave  %bookmark  %cd  %clear  %cls  %colors  %conda  %config  %connect_info  %copy  %ddir  %debug  %dhist  %dirs  %doctest_mode  %echo  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %macro  %magic  %matplotlib  %mkdir  %more  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %pip  %popd  %pprint  %precision  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %ren  %rep  %rerun  %reset  %reset_selective  %rmdir  %run  %save  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode

Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%cmd  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.

1. Jupyter Notebook Magic Commands List.

  1. ! : Execute shell commands.
    !python --version
    
    Python 3.8.5
    ******************************
    !echo 'hello python' 
    
    'hello python'
    ******************************
    !dir
    
    Volume in drive C has no label.
     Volume Serial Number is 7689-56A2
    
     Directory of C:\WorkSpace\Work\dev2qa.com-example-code\JupyterNotebookProject
    
    04/09/2020  12:20    <DIR>          .
    04/09/2020  12:20    <DIR>          ..
    04/09/2020  12:06    <DIR>          .ipynb_checkpoints
    04/09/2020  12:20             1,042 JupyterNotebookMagicCommands.ipynb
    07/03/2019  10:03         3,411,509 play_video_test.mp4
    07/03/2019  10:03         4,113,874 test.mp3
    04/09/2020  11:01         5,488,522 TestSlideShow.ipynb
    03/09/2020  22:38         5,780,046 TestSlideShow.slides.html
                   5 File(s)     18,794,993 bytes
                   3 Dir(s)  194,749,968,384 bytes free
  2. ? : View help documentation.
    ?id
    
    Signature: id(obj, /)
    Docstring:
    Return the identity of an object.
    
    This is guaranteed to be unique among simultaneously existing objects.
    (CPython uses the object's memory address.)
    Type:      builtin_function_or_method
  3. ?? : View source code.
    timeit??
    
    Source:
        @skip_doctest
        @no_var_expand
        @line_cell_magic
        @needs_local_scope
        def timeit(self, line='', cell=None, local_ns=None):
            """Time execution of a Python statement or expression
    
            Usage, in line mode:
              %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
          ......
    
                if tc > tc_min:
                    print("Compiler time: %.2f s" % tc)
            if return_result:
                return timeit_result
    File:   c:\users\zhaosong\anaconda3\envs\python_example\lib\site-packages\ipython\core\magics\execution.py
  4. %connect_info : View specific information about the user interface and the core connection.
    %connect_info
    
    {
      "shell_port": 53916,
      "iopub_port": 53917,
      "stdin_port": 53918,
      "control_port": 53920,
      "hb_port": 53919,
      "ip": "127.0.0.1",
      "key": "0c627f3f-87df81e578f5dab54d888d09",
      "transport": "tcp",
      "signature_scheme": "hmac-sha256",
      "kernel_name": ""
    }
    
    Paste the above JSON into a file, and connect with:
        $> jupyter <app> --existing <file>
    or, if you are local, you can connect with just:
        $> jupyter <app> --existing kernel-eee4d83f-cac9-469d-93bc-ef2b85fccae4.json
    or even just:
        $> jupyter <app> --existing
    if this is the most recent Jupyter kernel you have started.
  5. %%javascript : Insert js code. When you run below code in jupyter notebook cell line, it will popup a prompt dialog.
    %%javascript
    
    prompt('hello python')
  6. %matplotlib inline : Matplotlib can be embedded in jupyter notebook. To use matplotlib in jupyter notebook, you need to install matplotlib library first use pip command like below.
    # run below command to install matplotlib
    > pip install matplotlib
    
    # run below command to verify the matplotlib installation.
    > pip show matplotlib
    
    Name: matplotlib
    Version: 3.3.1
    Summary: Python plotting package
    Home-page: https://matplotlib.org
    Author: John D. Hunter, Michael Droettboom
    Author-email: [email protected]
    License: PSF
    Location: c:\users\zhaosong\anaconda3\envs\python_example\lib\site-packages
    Requires: cycler, kiwisolver, pyparsing, numpy, pillow, certifi, python-dateutil
    Required-by:
    

    After install matplotlib, run below code in a jupyter notebook cell line, then you can get a plot picture below the source code.

    %matplotlib inline
    
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.arange(20)
    y = x**2
    
    plt.plot(x, y)

    matplotlib picture in jupyter notebook

  7. %qtconsole : Jupyter notebook support three ways to connect to the kernel. 1. Jupyter console. 2. Jupyter qtconsole. 3. Jupyter Notebook. Use the magic command %qtconsole you can open the jupyter qtconsole to connect to the kernel. But you should first make sure qtconsole library has been installed in your python environment.
    # install qtconsole library.
    > pip install qtconsole
    
    # display qtconsole libray installation info.
    > pip show qtconsole
    Name: qtconsole
    Version: 4.7.6
    Summary: Jupyter Qt console
    Home-page: http://jupyter.org
    Author: Jupyter Development Team
    Author-email: [email protected]
    License: BSD
    Location: c:\users\zhaosong\anaconda3\envs\python_example\lib\site-packages
    Requires: traitlets, jupyter-client, pygments, ipykernel, pyzmq, qtpy, jupyter-core, ipython-genutils
    Required-by:

    Then you can run %qtconsole magic command in a jupyter notebook cell line, then it will popup a Jupyter QtConsole window like below.
    jupyter qtconsole window

Leave a Reply

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.