additional_ipython.rst
changeset 136 7f8b6a9fb61d
parent 131 ca42b9821019
child 212 c3172a51b555
equal deleted inserted replaced
135:7bc03b5096f9 136:7f8b6a9fb61d
     6 
     6 
     7 In this tutorial, we shall look at additional features of IPython that help us
     7 In this tutorial, we shall look at additional features of IPython that help us
     8 to retreive the commands that we type on the interpreter and then save them
     8 to retreive the commands that we type on the interpreter and then save them
     9 into a file and run it.
     9 into a file and run it.
    10 
    10 
    11 Let us start ipython with pylab loaded, by typing::
    11 Let us start ipython with pylab loaded, by typing
       
    12 ::
       
    13 
    12     $ ipython -pylab
    14     $ ipython -pylab
    13 
    15 
    14 on the terminal
    16 on the terminal
    15 
    17 
    16 {{{ shit to terminal and type ipython -pylab }}}
    18 {{{ shit to terminal and type ipython -pylab }}}
    17 
    19 
    18 We shall first make a plot and then view the history and save it.::
    20 We shall first make a plot and then view the history and save it.
       
    21 ::
    19 
    22 
    20     x = linspace(-2*pi, 2*pi, 100)
    23     x = linspace(-2*pi, 2*pi, 100)
    21     plot(x, xsinx(x))
    24     plot(x, xsinx(x))
    22 
    25 
    23 xsin(x) is actually x * sin(x)::
    26 xsin(x) is actually x * sin(x)
       
    27 ::
    24 
    28 
    25     plot(x, x*sin(x))
    29     plot(x, x*sin(x))
    26     plot(x, sin(x))
    30     plot(x, sin(x))
    27     xlabel("x")
    31     xlabel("x")
    28     ylabel("$f(x)$")   
    32     ylabel("$f(x)$")   
    29     title("x and xsin")
    33     title("x and xsin")
    30 
    34 
    31 
       
    32 We now have the plot. Let us look at the commands that we have typed in. The
    35 We now have the plot. Let us look at the commands that we have typed in. The
    33 history can be retreived by using =%hist= command. Type::
    36 history can be retreived by using =%hist= command. Type
       
    37 ::
    34 
    38 
    35     %hist
    39     %hist
    36 
    40 
    37 As you can see, it displays a list of recent commands that we typed. Every
    41 As you can see, it displays a list of recent commands that we typed. Every
    38 command has a number in front, to specify in which order and when it was typed.
    42 command has a number in front, to specify in which order and when it was typed.
    44 Also note that, the =%hist= itself is a command and is displayed as the most
    48 Also note that, the =%hist= itself is a command and is displayed as the most
    45 recent command. This implies that anything we type in is stored as history, 
    49 recent command. This implies that anything we type in is stored as history, 
    46 irrespective of whether it is command or an error or IPython magic command.
    50 irrespective of whether it is command or an error or IPython magic command.
    47 
    51 
    48 If we want only the recent 5 to be displayed, we pass the number as an argument
    52 If we want only the recent 5 to be displayed, we pass the number as an argument
    49 to =%hist= command. Hence::
    53 to =%hist= command. Hence
       
    54 ::
    50 
    55 
    51     %hist 5 
    56     %hist 5 
    52 
    57 
    53 displays the recent 5 commands, inclusive of the =%hist= command.
    58 displays the recent 5 commands, inclusive of the =%hist= command.
    54 The default number is 40.
    59 The default number is 40.
    58 %% 1 %% Read through the %hist documenatation and find out how can we list all
    63 %% 1 %% Read through the %hist documenatation and find out how can we list all
    59         the commands between 5 and 10
    64         the commands between 5 and 10
    60 
    65 
    61 {{{ continue from paused state }}}
    66 {{{ continue from paused state }}}
    62 
    67 
    63 As we can see from =%hist= documentation,::
    68 As we can see from =%hist= documentation,
       
    69 ::
    64 
    70 
    65     %hist 5 10
    71     %hist 5 10
    66 
    72 
    67 displays the commands from 5 to 10
    73 displays the commands from 5 to 10
    68 
    74 
    69 Now that we have the history, we would like to save the required line of code
    75 Now that we have the history, we would like to save the required line of code
    70 from history. This is possible by using the =%save= command.
    76 from history. This is possible by using the =%save= command.
    71 
    77 
    72 Before we do that, let us first look at history and identify what lines of code
    78 Before we do that, let us first look at history and identify what lines of code
    73 we require.Type::
    79 we require.Type
       
    80 ::
    74 
    81 
    75     %hist
    82     %hist
    76 
    83 
    77 
    84 
    78 {{{ point to the lines }}}
    85 {{{ point to the lines }}}
    81 error. Hence we do not need seconf. The commands from third to sixth are 
    88 error. Hence we do not need seconf. The commands from third to sixth are 
    82 required. The seventh command although is correct, we do not need it since we
    89 required. The seventh command although is correct, we do not need it since we
    83 are setting the title correctly in the eigthth command.
    90 are setting the title correctly in the eigthth command.
    84 
    91 
    85 So we need first third to sixth and the eigthth command for our program.
    92 So we need first third to sixth and the eigthth command for our program.
    86 Hence the syntax of =%save= is::
    93 Hence the syntax of =%save= is
       
    94 ::
    87 
    95 
    88     %save /home/fossee/plot_script.py 1 3-6 8
    96     %save /home/fossee/plot_script.py 1 3-6 8
    89 
    97 
    90 {{{ point to the output of the command }}}
    98 {{{ point to the output of the command }}}
    91 
    99 
   102 %% 2 %% change the label on y-axis to "y" and save the lines of code
   110 %% 2 %% change the label on y-axis to "y" and save the lines of code
   103         accordingly
   111         accordingly
   104 
   112 
   105 {{{ continue from paused state }}}
   113 {{{ continue from paused state }}}
   106 
   114 
   107 we use the command =ylabel= on interpreter as::
   115 we use the command =ylabel= on interpreter as
       
   116 ::
   108 
   117 
   109     ylabel("y")
   118     ylabel("y")
   110 
   119 
   111 and then do::
   120 and then do
       
   121 ::
   112 
   122 
   113     %save /home/fossee/example_plot.py 1 3-6 10
   123     %save /home/fossee/example_plot.py 1 3-6 10
   114 
   124 
   115 Now that we have the required lines of code in a file, let us learn how to run
   125 Now that we have the required lines of code in a file, let us learn how to run
   116 the file as a python script.
   126 the file as a python script.
   117 
   127 
   118 We use the IPython magic command =%run= to do this. Type::
   128 We use the IPython magic command =%run= to do this. Type
       
   129 ::
   119 
   130 
   120    %run -i /home/fossee/plot_script.py
   131    %run -i /home/fossee/plot_script.py
   121 
   132 
   122 The script runs but we do not see the plot. This happens because we are running
   133 The script runs but we do not see the plot. This happens because we are running
   123 a script and we are not in interactive mode anymore.
   134 a script and we are not in interactive mode anymore.
   124 
   135 
   125 Hence on your terminal type::
   136 Hence on your terminal type
       
   137 ::
   126 
   138 
   127     show()
   139     show()
   128 
   140 
   129 to show the plot.
   141 to show the plot.
   130 
   142 
   133 %% 3 %% Use %hist and %save and create a script that has show in it and run it
   145 %% 3 %% Use %hist and %save and create a script that has show in it and run it
   134         to produce and show the plot.
   146         to produce and show the plot.
   135 
   147 
   136 {{{ continue from paused state }}}
   148 {{{ continue from paused state }}}
   137 
   149 
   138 We first look at the history using::
   150 We first look at the history using
       
   151 ::
   139 
   152 
   140     %hist 20
   153     %hist 20
   141 
   154 
   142 Then save the script using::
   155 Then save the script using
       
   156 ::
   143 
   157 
   144     %save /home/fossee/show_included.py 1 3-6 8 10 13
   158     %save /home/fossee/show_included.py 1 3-6 8 10 13
   145     %run -i /home/fossee/show_included.py
   159     %run -i /home/fossee/show_included.py
   146 
   160 
   147 We get the desired plot.
   161 We get the desired plot.
   153 
   167 
   154 {{{ Show summary slide }}}
   168 {{{ Show summary slide }}}
   155 
   169 
   156 This brings us to the end of the tutorial.
   170 This brings us to the end of the tutorial.
   157 we have looked at 
   171 we have looked at 
       
   172 
   158  * Retreiving history using =%hist= command
   173  * Retreiving history using =%hist= command
   159  * Vieweing only a part of history by passing an argument to %hist
   174  * Vieweing only a part of history by passing an argument to %hist
   160  * saving the required lines of code in required order using %save
   175  * saving the required lines of code in required order using %save
   161  * using %run -i command to run the saved script
   176  * using %run -i command to run the saved script
   162 
   177