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