Debian Codegraph @

docs/source/interactive/tips.rst

raw
1 .. _tips:
2
3 =====================
4 IPython Tips & Tricks
5 =====================
6
7 The `IPython cookbook
8 <https://github.com/ipython/ipython/wiki?path=Cookbook>`_ details more things
9 you can do with IPython.
10
11 .. This is not in the current version:
12
13
14 Embed IPython in your programs
15 ------------------------------
16
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
19 automatic processing has been completed. See :ref:`the embedding section <embedding>`.
20
21 Run doctests
22 ------------
23
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
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
28 contains leading '>>>' prompts, even if they have extra leading whitespace
29 (as is common in doctest files). This combined with the ``%hist -t`` call to
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
32 as needed.
33
34 Use IPython to present interactive demos
35 ----------------------------------------
36
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
39 the script, stopping as needed. See :ref:`here <interactive_demos>` for more.
40
41 Suppress output
42 ---------------
43
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
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.
48
49 Lightweight 'version control'
50 -----------------------------
51
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
54 session as a string variable. Thanks to IPython's output caching
55 mechanism, this is automatically stored::
56
57 In [1]: %edit
58
59 IPython will make a temporary file named: /tmp/ipython_edit_yR-HCN.py
60
61 Editing... done. Executing edited code...
62
63 hello - this is a temporary file
64
65 Out[1]: "print('hello - this is a temporary file')\n"
66
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
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
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
73 prompt.
74
75 Continuing with the example above, this should illustrate this idea::
76
77 In [2]: edit -p
78
79 IPython will make a temporary file named: /tmp/ipython_edit_nA09Qk.py
80
81 Editing... done. Executing edited code...
82
83 hello - now I made some changes
84
85 Out[2]: "print('hello - now I made some changes')\n"
86
87 In [3]: edit _1
88
89 IPython will make a temporary file named: /tmp/ipython_edit_gy6-zD.py
90
91 Editing... done. Executing edited code...
92
93 hello - this is a temporary file
94
95 IPython version control at work :)
96
97 Out[3]: "print('hello - this is a temporary file')\nprint('IPython version control at work :)')\n"
98
99
100 This section was written after a contribution by Alexander Belchenko on
101 the IPython user list.
102