##// END OF EJS Templates
Mention that suppressing output also bypasses output cache.
Thomas Kluyver -
Show More
@@ -1,100 +1,101 b''
1 .. _tips:
1 .. _tips:
2
2
3 =====================
3 =====================
4 IPython Tips & Tricks
4 IPython Tips & Tricks
5 =====================
5 =====================
6
6
7 The `IPython cookbook <http://wiki.ipython.org/Cookbook>`_ details more things
7 The `IPython cookbook <http://wiki.ipython.org/Cookbook>`_ details more things
8 you can do with IPython.
8 you can do with IPython.
9
9
10 .. This is not in the current version:
10 .. This is not in the current version:
11
11
12
12
13 Embed IPython in your programs
13 Embed IPython in your programs
14 ------------------------------
14 ------------------------------
15
15
16 A few lines of code are enough to load a complete IPython inside your own
16 A few lines of code are enough to load a complete IPython inside your own
17 programs, giving you the ability to work with your data interactively after
17 programs, giving you the ability to work with your data interactively after
18 automatic processing has been completed. See :ref:`the embedding section <embedding>`.
18 automatic processing has been completed. See :ref:`the embedding section <embedding>`.
19
19
20 Run doctests
20 Run doctests
21 ------------
21 ------------
22
22
23 Run your doctests from within IPython for development and debugging. The
23 Run your doctests from within IPython for development and debugging. The
24 special %doctest_mode command toggles a mode where the prompt, output and
24 special %doctest_mode command toggles a mode where the prompt, output and
25 exceptions display matches as closely as possible that of the default Python
25 exceptions display matches as closely as possible that of the default Python
26 interpreter. In addition, this mode allows you to directly paste in code that
26 interpreter. In addition, this mode allows you to directly paste in code that
27 contains leading '>>>' prompts, even if they have extra leading whitespace
27 contains leading '>>>' prompts, even if they have extra leading whitespace
28 (as is common in doctest files). This combined with the ``%history -t`` call
28 (as is common in doctest files). This combined with the ``%history -t`` call
29 to see your translated history allows for an easy doctest workflow, where you
29 to see your translated history allows for an easy doctest workflow, where you
30 can go from doctest to interactive execution to pasting into valid Python code
30 can go from doctest to interactive execution to pasting into valid Python code
31 as needed.
31 as needed.
32
32
33 Use IPython to present interactive demos
33 Use IPython to present interactive demos
34 ----------------------------------------
34 ----------------------------------------
35
35
36 Use the :class:`IPython.lib.demo.Demo` class to load any Python script as an interactive
36 Use the :class:`IPython.lib.demo.Demo` class to load any Python script as an interactive
37 demo. With a minimal amount of simple markup, you can control the execution of
37 demo. With a minimal amount of simple markup, you can control the execution of
38 the script, stopping as needed. See :ref:`here <interactive_demos>` for more.
38 the script, stopping as needed. See :ref:`here <interactive_demos>` for more.
39
39
40 Suppress output
40 Suppress output
41 ---------------
41 ---------------
42
42
43 Put a ';' at the end of a line to suppress the printing of output. This is
43 Put a ';' at the end of a line to suppress the printing of output. This is
44 useful when doing calculations which generate long output you are not
44 useful when doing calculations which generate long output you are not
45 interested in seeing.
45 interested in seeing. It also keeps the object out of the output cache, so if
46 you're working with large temporary objects, they'll be released from memory sooner.
46
47
47 Lightweight 'version control'
48 Lightweight 'version control'
48 -----------------------------
49 -----------------------------
49
50
50 When you call ``%edit`` with no arguments, IPython opens an empty editor
51 When you call ``%edit`` with no arguments, IPython opens an empty editor
51 with a temporary file, and it returns the contents of your editing
52 with a temporary file, and it returns the contents of your editing
52 session as a string variable. Thanks to IPython's output caching
53 session as a string variable. Thanks to IPython's output caching
53 mechanism, this is automatically stored::
54 mechanism, this is automatically stored::
54
55
55 In [1]: %edit
56 In [1]: %edit
56
57
57 IPython will make a temporary file named: /tmp/ipython_edit_yR-HCN.py
58 IPython will make a temporary file named: /tmp/ipython_edit_yR-HCN.py
58
59
59 Editing... done. Executing edited code...
60 Editing... done. Executing edited code...
60
61
61 hello - this is a temporary file
62 hello - this is a temporary file
62
63
63 Out[1]: "print 'hello - this is a temporary file'\n"
64 Out[1]: "print 'hello - this is a temporary file'\n"
64
65
65 Now, if you call ``%edit -p``, IPython tries to open an editor with the
66 Now, if you call ``%edit -p``, IPython tries to open an editor with the
66 same data as the last time you used %edit. So if you haven't used %edit
67 same data as the last time you used %edit. So if you haven't used %edit
67 in the meantime, this same contents will reopen; however, it will be
68 in the meantime, this same contents will reopen; however, it will be
68 done in a new file. This means that if you make changes and you later
69 done in a new file. This means that if you make changes and you later
69 want to find an old version, you can always retrieve it by using its
70 want to find an old version, you can always retrieve it by using its
70 output number, via '%edit _NN', where NN is the number of the output
71 output number, via '%edit _NN', where NN is the number of the output
71 prompt.
72 prompt.
72
73
73 Continuing with the example above, this should illustrate this idea::
74 Continuing with the example above, this should illustrate this idea::
74
75
75 In [2]: edit -p
76 In [2]: edit -p
76
77
77 IPython will make a temporary file named: /tmp/ipython_edit_nA09Qk.py
78 IPython will make a temporary file named: /tmp/ipython_edit_nA09Qk.py
78
79
79 Editing... done. Executing edited code...
80 Editing... done. Executing edited code...
80
81
81 hello - now I made some changes
82 hello - now I made some changes
82
83
83 Out[2]: "print 'hello - now I made some changes'\n"
84 Out[2]: "print 'hello - now I made some changes'\n"
84
85
85 In [3]: edit _1
86 In [3]: edit _1
86
87
87 IPython will make a temporary file named: /tmp/ipython_edit_gy6-zD.py
88 IPython will make a temporary file named: /tmp/ipython_edit_gy6-zD.py
88
89
89 Editing... done. Executing edited code...
90 Editing... done. Executing edited code...
90
91
91 hello - this is a temporary file
92 hello - this is a temporary file
92
93
93 IPython version control at work :)
94 IPython version control at work :)
94
95
95 Out[3]: "print 'hello - this is a temporary file'\nprint 'IPython version control at work :)'\n"
96 Out[3]: "print 'hello - this is a temporary file'\nprint 'IPython version control at work :)'\n"
96
97
97
98
98 This section was written after a contribution by Alexander Belchenko on
99 This section was written after a contribution by Alexander Belchenko on
99 the IPython user list.
100 the IPython user list.
100
101
General Comments 0
You need to be logged in to leave comments. Login now