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