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