additional_ipython/script.rst
author Nishanth <nishanth@fossee.in>
Mon, 11 Oct 2010 13:42:52 +0530
changeset 297 bef342784a40
parent 271 bb4ed72f3ec6
child 327 28a54b57049d
permissions -rw-r--r--
removed redundant scripts
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
234
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     1
.. Objectives
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     2
.. ----------
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     3
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     4
.. A - Students and teachers from Science and engineering backgrounds
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     5
   B - 
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     6
   C - 
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     7
   D - 
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     8
271
bb4ed72f3ec6 Slides for additional ipython LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 234
diff changeset
     9
.. By the end of this tutorial you will be able to
bb4ed72f3ec6 Slides for additional ipython LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 234
diff changeset
    10
bb4ed72f3ec6 Slides for additional ipython LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 234
diff changeset
    11
.. #. Retrieve your ipython history 
bb4ed72f3ec6 Slides for additional ipython LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 234
diff changeset
    12
.. #. View a part of the history 
bb4ed72f3ec6 Slides for additional ipython LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 234
diff changeset
    13
.. #. Save a part of your history to a file. 
bb4ed72f3ec6 Slides for additional ipython LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 234
diff changeset
    14
.. #. Run a script from within ipython 
bb4ed72f3ec6 Slides for additional ipython LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 234
diff changeset
    15
bb4ed72f3ec6 Slides for additional ipython LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 234
diff changeset
    16
234
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    17
.. Prerequisites
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    18
.. -------------
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    19
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    20
..   1. Embellishing Plots
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    21
     
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    22
.. Author              : Nishanth Amuluru
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    23
   Internal Reviewer   : 
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    24
   External Reviewer   :
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    25
   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    26
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    27
Script
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    28
------
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    29
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    30
Hello friends and welcome to the tutorial on Additional Features of IPython
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    31
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    32
{{{ Show the slide containing title }}}
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    33
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    34
{{{ Show the slide containing the outline slide }}}
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    35
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    36
In this tutorial, we shall look at additional features of IPython that help us
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    37
to retreive the commands that we type on the interpreter and then save them
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    38
into a file and run it.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    39
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    40
Let us start ipython with pylab loaded, by typing
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    41
::
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    42
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    43
    $ ipython -pylab
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    44
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    45
on the terminal
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    46
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    47
{{{ shit to terminal and type ipython -pylab }}}
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    48
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    49
We shall first make a plot and then view the history and save it.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    50
::
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    51
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    52
    x = linspace(-2*pi, 2*pi, 100)
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    53
    plot(x, xsinx(x))
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    54
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    55
xsin(x) is actually x * sin(x)
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    56
::
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    57
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    58
    plot(x, x*sin(x))
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    59
    plot(x, sin(x))
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    60
    xlabel("x")
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    61
    ylabel("$f(x)$")   
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    62
    title("x and xsin")
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    63
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    64
We now have the plot. Let us look at the commands that we have typed in. The
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    65
history can be retreived by using =%hist= command. Type
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    66
::
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    67
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    68
    %hist
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    69
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    70
As you can see, it displays a list of recent commands that we typed. Every
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    71
command has a number in front, to specify in which order and when it was typed.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    72
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    73
Please note that there is a % sign before the hist command. This implies that 
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    74
%hist is a command that is specific to IPython and not available in vannila 
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    75
Python interpreter. These type of commands are called as magic commands.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    76
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    77
Also note that, the =%hist= itself is a command and is displayed as the most
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    78
recent command. This implies that anything we type in is stored as history, 
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    79
irrespective of whether it is command or an error or IPython magic command.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    80
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    81
If we want only the recent 5 to be displayed, we pass the number as an argument
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    82
to =%hist= command. Hence
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    83
::
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    84
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    85
    %hist 5 
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    86
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    87
displays the recent 5 commands, inclusive of the =%hist= command.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    88
The default number is 40.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    89
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    90
{{{ Pause here and try out the following exercises }}}
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    91
271
bb4ed72f3ec6 Slides for additional ipython LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 234
diff changeset
    92
%% 1 %% Read through the documentation of %hist and find out how to
bb4ed72f3ec6 Slides for additional ipython LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 234
diff changeset
    93
        list all the commands between 5 and 10
234
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    94
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    95
{{{ continue from paused state }}}
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    96
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    97
As we can see from =%hist= documentation,
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    98
::
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    99
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   100
    %hist 5 10
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   101
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   102
displays the commands from 5 to 10
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   103
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   104
Now that we have the history, we would like to save the required line of code
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   105
from history. This is possible by using the =%save= command.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   106
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   107
Before we do that, let us first look at history and identify what lines of code
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   108
we require.Type
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   109
::
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   110
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   111
    %hist
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   112
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   113
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   114
{{{ point to the lines }}}
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   115
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   116
The first command is linspace. But second command is a command that gave us an
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   117
error. Hence we do not need seconf. The commands from third to sixth are 
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   118
required. The seventh command although is correct, we do not need it since we
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   119
are setting the title correctly in the eigthth command.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   120
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   121
So we need first third to sixth and the eigthth command for our program.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   122
Hence the syntax of =%save= is
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   123
::
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   124
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   125
    %save /home/fossee/plot_script.py 1 3-6 8
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   126
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   127
{{{ point to the output of the command }}}
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   128
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   129
The command saves first and then third to sixth and eighth lines of code into
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   130
the specified file.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   131
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   132
The first argument to %save is the path of file to save the commands and the
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   133
arguments there after are the commands to be saved in the given order.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   134
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   135
{{{ goto the file and open it and show it }}}
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   136
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   137
{{{ Pause here and try out the following exercises }}}
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   138
271
bb4ed72f3ec6 Slides for additional ipython LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 234
diff changeset
   139
%% 2 %% Change the label on y-axis to "y" and save the lines of code
234
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   140
        accordingly
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   141
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   142
{{{ continue from paused state }}}
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   143
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   144
we use the command =ylabel= on interpreter as
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   145
::
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   146
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   147
    ylabel("y")
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   148
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   149
and then do
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   150
::
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   151
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   152
    %save /home/fossee/example_plot.py 1 3-6 10
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   153
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   154
Now that we have the required lines of code in a file, let us learn how to run
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   155
the file as a python script.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   156
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   157
We use the IPython magic command =%run= to do this. Type
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   158
::
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   159
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   160
   %run -i /home/fossee/plot_script.py
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   161
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   162
The script runs but we do not see the plot. This happens because we are running
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   163
a script and we are not in interactive mode anymore.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   164
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   165
Hence on your terminal type
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   166
::
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   167
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   168
    show()
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   169
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   170
to show the plot.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   171
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   172
{{{ Pause here and try out the following exercises }}}
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   173
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   174
%% 3 %% Use %hist and %save and create a script that has show in it and run it
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   175
        to produce and show the plot.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   176
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   177
{{{ continue from paused state }}}
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   178
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   179
We first look at the history using
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   180
::
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   181
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   182
    %hist 20
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   183
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   184
Then save the script using
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   185
::
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   186
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   187
    %save /home/fossee/show_included.py 1 3-6 8 10 13
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   188
    %run -i /home/fossee/show_included.py
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   189
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   190
We get the desired plot.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   191
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   192
The reason for including a -i after run is to tell the interpreter that if any
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   193
name is not found in script, search for it in the interpreter. Hence all these
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   194
sin, plot, pi and show which are not available in script, are taken from the
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   195
interpreter and used to run the script.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   196
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   197
{{{ Pause here and try out the following exercises }}}
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   198
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   199
%% 4 %% Run the script without using the -i option. Do you find any difference?
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   200
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   201
{{{ continue from paused state }}}
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   202
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   203
We see that it raises nameerror saying the name linspace is not found.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   204
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   205
{{{ Show summary slide }}}
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   206
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   207
This brings us to the end of the tutorial.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   208
we have looked at 
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   209
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   210
 * Retreiving history using =%hist= command
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   211
 * Vieweing only a part of history by passing an argument to %hist
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   212
 * saving the required lines of code in required order using %save
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   213
 * using %run -i command to run the saved script
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   214
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   215
{{{ Show the "sponsored by FOSSEE" slide }}}
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   216
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   217
#[Nishanth]: Will add this line after all of us fix on one.
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   218
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   219
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   220
Hope you have enjoyed and found it useful.
271
bb4ed72f3ec6 Slides for additional ipython LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 234
diff changeset
   221
Thank you!
234
2b88724a7ee0 Converted the additional_ipython script into new template form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   222