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