additional_ipython.rst
author nishanth
Wed, 15 Sep 2010 18:50:17 +0530
changeset 133 bc93dd9d22c5
parent 131 ca42b9821019
child 136 7f8b6a9fb61d
permissions -rw-r--r--
initial commit parsing_data
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
     1
Hello friends and welcome to the tutorial on Additional Features of IPython
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
     2
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
     3
{{{ Show the slide containing title }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
     4
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
     5
{{{ Show the slide containing the outline slide }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
     6
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
     7
In this tutorial, we shall look at additional features of IPython that help us
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
     8
to retreive the commands that we type on the interpreter and then save them
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
     9
into a file and run it.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    10
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    11
Let us start ipython with pylab loaded, by typing::
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    12
    $ ipython -pylab
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    13
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    14
on the terminal
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    15
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    16
{{{ shit to terminal and type ipython -pylab }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    17
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    18
We shall first make a plot and then view the history and save it.::
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    19
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    20
    x = linspace(-2*pi, 2*pi, 100)
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    21
    plot(x, xsinx(x))
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    22
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    23
xsin(x) is actually x * sin(x)::
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    24
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    25
    plot(x, x*sin(x))
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    26
    plot(x, sin(x))
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    27
    xlabel("x")
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    28
    ylabel("$f(x)$")   
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    29
    title("x and xsin")
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    30
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    31
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    32
We now have the plot. Let us look at the commands that we have typed in. The
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    33
history can be retreived by using =%hist= command. Type::
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    34
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    35
    %hist
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    36
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    37
As you can see, it displays a list of recent commands that we typed. Every
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    38
command has a number in front, to specify in which order and when it was typed.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    39
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    40
Please note that there is a % sign before the hist command. This implies that 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    41
%hist is a command that is specific to IPython and not available in vannila 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    42
Python interpreter. These type of commands are called as magic commands.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    43
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    44
Also note that, the =%hist= itself is a command and is displayed as the most
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    45
recent command. This implies that anything we type in is stored as history, 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    46
irrespective of whether it is command or an error or IPython magic command.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    47
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    48
If we want only the recent 5 to be displayed, we pass the number as an argument
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    49
to =%hist= command. Hence::
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    50
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    51
    %hist 5 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    52
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    53
displays the recent 5 commands, inclusive of the =%hist= command.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    54
The default number is 40.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    55
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    56
{{{ Pause here and try out the following exercises }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    57
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    58
%% 1 %% Read through the %hist documenatation and find out how can we list all
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    59
        the commands between 5 and 10
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    60
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    61
{{{ continue from paused state }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    62
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    63
As we can see from =%hist= documentation,::
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    64
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    65
    %hist 5 10
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    66
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    67
displays the commands from 5 to 10
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    68
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    69
Now that we have the history, we would like to save the required line of code
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    70
from history. This is possible by using the =%save= command.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    71
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    72
Before we do that, let us first look at history and identify what lines of code
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    73
we require.Type::
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    74
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    75
    %hist
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    76
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    77
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    78
{{{ point to the lines }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    79
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    80
The first command is linspace. But second command is a command that gave us an
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    81
error. Hence we do not need seconf. The commands from third to sixth are 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    82
required. The seventh command although is correct, we do not need it since we
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    83
are setting the title correctly in the eigthth command.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    84
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    85
So we need first third to sixth and the eigthth command for our program.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    86
Hence the syntax of =%save= is::
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    87
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    88
    %save /home/fossee/plot_script.py 1 3-6 8
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    89
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    90
{{{ point to the output of the command }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    91
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    92
The command saves first and then third to sixth and eighth lines of code into
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    93
the specified file.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    94
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    95
The first argument to %save is the path of file to save the commands and the
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    96
arguments there after are the commands to be saved in the given order.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    97
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    98
{{{ goto the file and open it and show it }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    99
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   100
{{{ Pause here and try out the following exercises }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   101
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   102
%% 2 %% change the label on y-axis to "y" and save the lines of code
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   103
        accordingly
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   104
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   105
{{{ continue from paused state }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   106
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   107
we use the command =ylabel= on interpreter as::
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   108
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   109
    ylabel("y")
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   110
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   111
and then do::
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   112
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   113
    %save /home/fossee/example_plot.py 1 3-6 10
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   114
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   115
Now that we have the required lines of code in a file, let us learn how to run
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   116
the file as a python script.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   117
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   118
We use the IPython magic command =%run= to do this. Type::
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   119
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   120
   %run -i /home/fossee/plot_script.py
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   121
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   122
The script runs but we do not see the plot. This happens because we are running
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   123
a script and we are not in interactive mode anymore.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   124
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   125
Hence on your terminal type::
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   126
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   127
    show()
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   128
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   129
to show the plot.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   130
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   131
{{{ Pause here and try out the following exercises }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   132
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   133
%% 3 %% Use %hist and %save and create a script that has show in it and run it
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   134
        to produce and show the plot.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   135
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   136
{{{ continue from paused state }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   137
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   138
We first look at the history using::
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   139
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   140
    %hist 20
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   141
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   142
Then save the script using::
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   143
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   144
    %save /home/fossee/show_included.py 1 3-6 8 10 13
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   145
    %run -i /home/fossee/show_included.py
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   146
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   147
We get the desired plot.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   148
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   149
The reason for including a -i after run is to tell the interpreter that if any
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   150
name is not found in script, search for it in the interpreter. Hence all these
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   151
sin, plot, pi and show which are not available in script, are taken from the
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   152
interpreter and used to run the script.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   153
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   154
{{{ Show summary slide }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   155
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   156
This brings us to the end of the tutorial.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   157
we have looked at 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   158
 * Retreiving history using =%hist= command
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   159
 * Vieweing only a part of history by passing an argument to %hist
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   160
 * saving the required lines of code in required order using %save
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   161
 * using %run -i command to run the saved script
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   162
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   163
{{{ Show the "sponsored by FOSSEE" slide }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   164
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   165
#[Nishanth]: Will add this line after all of us fix on one.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   166
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   167
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   168
Hope you have enjoyed and found it useful.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   169
Thankyou
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   170
 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   171
.. Author              : Nishanth
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   172
   Internal Reviewer 1 : 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   173
   Internal Reviewer 2 : 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   174
   External Reviewer   :