Show More
This diff has been collapsed as it changes many lines, (546 lines changed) Show them Hide them | |||
@@ -53,6 +53,7 b' which tries to:' | |||
|
53 | 53 | |
|
54 | 54 | |
|
55 | 55 | Main features |
|
56 | ------------- | |
|
56 | 57 | |
|
57 | 58 | * Dynamic object introspection. One can access docstrings, function |
|
58 | 59 | definition prototypes, source code, source files and other details |
@@ -602,10 +603,14 b' Input/Output prompts and exception tracebacks' | |||
|
602 | 603 | You can test whether the colored prompts and tracebacks work on your |
|
603 | 604 | system interactively by typing '%colors Linux' at the prompt (use |
|
604 | 605 | '%colors LightBG' if your terminal has a light background). If the input |
|
605 | prompt shows garbage like: | |
|
606 | prompt shows garbage like:: | |
|
607 | ||
|
606 | 608 | [0;32mIn [[1;32m1[0;32m]: [0;00m |
|
607 | instead of (in color) something like: | |
|
609 | ||
|
610 | instead of (in color) something like:: | |
|
611 | ||
|
608 | 612 | In [1]: |
|
613 | ||
|
609 | 614 | this means that your terminal doesn't properly handle color escape |
|
610 | 615 | sequences. You can go to a 'no color' mode by typing '%colors NoColor'. |
|
611 | 616 | |
@@ -3092,6 +3097,23 b" won't work::" | |||
|
3092 | 3097 | Customization |
|
3093 | 3098 | ============= |
|
3094 | 3099 | |
|
3100 | There are 2 ways to configure IPython - the old way of using ipythonrc | |
|
3101 | files (an INI-file like format), and the new way that involves editing | |
|
3102 | your ipy_user_conf.py. Both configuration systems work at the same | |
|
3103 | time, so you can set your options in both, but if you are hesitating | |
|
3104 | about which alternative to choose, we recommend the ipy_user_conf.py | |
|
3105 | approach, as it will give you more power and control in the long | |
|
3106 | run. However, there are few options such as pylab_import_all that can | |
|
3107 | only be specified in ipythonrc file or command line - the reason for | |
|
3108 | this is that they are needed before IPython has been started up, and | |
|
3109 | the IPApi object used in ipy_user_conf.py is not yet available at that | |
|
3110 | time. A hybrid approach of specifying a few options in ipythonrc and | |
|
3111 | doing the more advanced configuration in ipy_user_conf.py is also | |
|
3112 | possible. | |
|
3113 | ||
|
3114 | The ipythonrc approach | |
|
3115 | ---------------------- | |
|
3116 | ||
|
3095 | 3117 | As we've already mentioned, IPython reads a configuration file which can |
|
3096 | 3118 | be specified at the command line (-rcfile) or which by default is |
|
3097 | 3119 | assumed to be called ipythonrc. Such a file is looked for in the current |
@@ -3150,7 +3172,6 b' Each of these options may appear as many times as you need it in the file.' | |||
|
3150 | 3172 | normal system shell. |
|
3151 | 3173 | |
|
3152 | 3174 | |
|
3153 | ||
|
3154 | 3175 | Sample ipythonrc file |
|
3155 | 3176 | --------------------- |
|
3156 | 3177 | |
@@ -3793,47 +3814,135 b' reproduce it here for reference::' | |||
|
3793 | 3814 | #************************* end of file <ipythonrc> ************************ |
|
3794 | 3815 | |
|
3795 | 3816 | |
|
3817 | ipy_user_conf.py | |
|
3818 | ---------------- | |
|
3819 | ||
|
3820 | There should be a simple template ipy_user_conf.py file in your | |
|
3821 | ~/.ipython directory. It is a plain python module that is imported | |
|
3822 | during IPython startup, so you can do pretty much what you want there | |
|
3823 | - import modules, configure extensions, change options, define magic | |
|
3824 | commands, put variables and functions in the IPython namespace, | |
|
3825 | etc. You use the IPython extension api object, acquired by | |
|
3826 | IPython.ipapi.get() and documented in the "IPython extension API" | |
|
3827 | chapter, to interact with IPython. A sample ipy_user_conf.py is listed | |
|
3828 | below for reference:: | |
|
3829 | ||
|
3830 | # Most of your config files and extensions will probably start | |
|
3831 | # with this import | |
|
3832 | ||
|
3833 | import IPython.ipapi | |
|
3834 | ip = IPython.ipapi.get() | |
|
3835 | ||
|
3836 | # You probably want to uncomment this if you did %upgrade -nolegacy | |
|
3837 | # import ipy_defaults | |
|
3838 | ||
|
3839 | import os | |
|
3840 | ||
|
3841 | def main(): | |
|
3842 | ||
|
3843 | #ip.dbg.debugmode = True | |
|
3844 | ip.dbg.debug_stack() | |
|
3845 | ||
|
3846 | # uncomment if you want to get ipython -p sh behaviour | |
|
3847 | # without having to use command line switches | |
|
3848 | import ipy_profile_sh | |
|
3849 | import jobctrl | |
|
3850 | ||
|
3851 | # Configure your favourite editor? | |
|
3852 | # Good idea e.g. for %edit os.path.isfile | |
|
3853 | ||
|
3854 | #import ipy_editors | |
|
3855 | ||
|
3856 | # Choose one of these: | |
|
3857 | ||
|
3858 | #ipy_editors.scite() | |
|
3859 | #ipy_editors.scite('c:/opt/scite/scite.exe') | |
|
3860 | #ipy_editors.komodo() | |
|
3861 | #ipy_editors.idle() | |
|
3862 | # ... or many others, try 'ipy_editors??' after import to see them | |
|
3863 | ||
|
3864 | # Or roll your own: | |
|
3865 | #ipy_editors.install_editor("c:/opt/jed +$line $file") | |
|
3866 | ||
|
3867 | ||
|
3868 | o = ip.options | |
|
3869 | # An example on how to set options | |
|
3870 | #o.autocall = 1 | |
|
3871 | o.system_verbose = 0 | |
|
3872 | ||
|
3873 | #import_all("os sys") | |
|
3874 | #execf('~/_ipython/ns.py') | |
|
3875 | ||
|
3876 | ||
|
3877 | # -- prompt | |
|
3878 | # A different, more compact set of prompts from the default ones, that | |
|
3879 | # always show your current location in the filesystem: | |
|
3880 | ||
|
3881 | #o.prompt_in1 = r'\C_LightBlue[\C_LightCyan\Y2\C_LightBlue]\C_Normal\n\C_Green|\#>' | |
|
3882 | #o.prompt_in2 = r'.\D: ' | |
|
3883 | #o.prompt_out = r'[\#] ' | |
|
3884 | ||
|
3885 | # Try one of these color settings if you can't read the text easily | |
|
3886 | # autoexec is a list of IPython commands to execute on startup | |
|
3887 | #o.autoexec.append('%colors LightBG') | |
|
3888 | #o.autoexec.append('%colors NoColor') | |
|
3889 | o.autoexec.append('%colors Linux') | |
|
3890 | ||
|
3891 | ||
|
3892 | # some config helper functions you can use | |
|
3893 | def import_all(modules): | |
|
3894 | """ Usage: import_all("os sys") """ | |
|
3895 | for m in modules.split(): | |
|
3896 | ip.ex("from %s import *" % m) | |
|
3897 | ||
|
3898 | def execf(fname): | |
|
3899 | """ Execute a file in user namespace """ | |
|
3900 | ip.ex('execfile("%s")' % os.path.expanduser(fname)) | |
|
3901 | ||
|
3902 | main() | |
|
3903 | ||
|
3904 | ||
|
3796 | 3905 | |
|
3797 | 3906 | Fine-tuning your prompt |
|
3798 | 3907 | ----------------------- |
|
3799 | 3908 | |
|
3800 | 3909 | IPython's prompts can be customized using a syntax similar to that of |
|
3801 | 3910 | the bash shell. Many of bash's escapes are supported, as well as a few |
|
3802 | additional ones. We list them below: | |
|
3911 | additional ones. We list them below:: | |
|
3803 | 3912 | |
|
3804 | *\#* | |
|
3913 | \# | |
|
3805 | 3914 | the prompt/history count number. This escape is automatically |
|
3806 | 3915 | wrapped in the coloring codes for the currently active color scheme. |
|
3807 | *\N* | |
|
3916 | \N | |
|
3808 | 3917 | the 'naked' prompt/history count number: this is just the number |
|
3809 | 3918 | itself, without any coloring applied to it. This lets you produce |
|
3810 | 3919 | numbered prompts with your own colors. |
|
3811 | *\D* | |
|
3920 | \D | |
|
3812 | 3921 | the prompt/history count, with the actual digits replaced by dots. |
|
3813 | 3922 | Used mainly in continuation prompts (prompt_in2) |
|
3814 | *\w* | |
|
3923 | \w | |
|
3815 | 3924 | the current working directory |
|
3816 | *\W* | |
|
3925 | \W | |
|
3817 | 3926 | the basename of current working directory |
|
3818 | *\Xn* | |
|
3927 | \Xn | |
|
3819 | 3928 | where $n=0\ldots5.$ The current working directory, with $HOME |
|
3820 | 3929 | replaced by ~, and filtered out to contain only $n$ path elements |
|
3821 | *\Yn* | |
|
3930 | \Yn | |
|
3822 | 3931 | Similar to \Xn, but with the $n+1$ element included if it is ~ (this |
|
3823 | 3932 | is similar to the behavior of the %cn escapes in tcsh) |
|
3824 | *\u* | |
|
3933 | \u | |
|
3825 | 3934 | the username of the current user |
|
3826 | *\$* | |
|
3935 | \$ | |
|
3827 | 3936 | if the effective UID is 0, a #, otherwise a $ |
|
3828 | *\h* | |
|
3937 | \h | |
|
3829 | 3938 | the hostname up to the first '.' |
|
3830 | *\H* | |
|
3939 | \H | |
|
3831 | 3940 | the hostname |
|
3832 | *\n* | |
|
3941 | \n | |
|
3833 | 3942 | a newline |
|
3834 | *\r* | |
|
3943 | \r | |
|
3835 | 3944 | a carriage return |
|
3836 | *\v* | |
|
3945 | \v | |
|
3837 | 3946 | IPython version string |
|
3838 | 3947 | |
|
3839 | 3948 | In addition to these, ANSI color escapes can be insterted into the |
@@ -4225,7 +4334,7 b' uncaught exception is triggered by your code.' | |||
|
4225 | 4334 | |
|
4226 | 4335 | For stand-alone use of the feature in your programs which do not use |
|
4227 | 4336 | IPython at all, put the following lines toward the top of your 'main' |
|
4228 | routine: | |
|
4337 | routine:: | |
|
4229 | 4338 | |
|
4230 | 4339 | import sys,IPython.ultraTB |
|
4231 | 4340 | sys.excepthook = IPython.ultraTB.FormattedTB(mode='Verbose', |
@@ -4255,7 +4364,7 b" supplied, which we will briefly describe now. These can be used 'as is'" | |||
|
4255 | 4364 | starting point for writing your own extensions. |
|
4256 | 4365 | |
|
4257 | 4366 | |
|
4258 |
Pasting of code starting with ' |
|
|
4367 | Pasting of code starting with '>>> ' or '... ' | |
|
4259 | 4368 | ---------------------------------------------- |
|
4260 | 4369 | |
|
4261 | 4370 | In the python tutorial it is common to find code examples which have |
@@ -4345,7 +4454,7 b' the "pysh" shortcut in start menu.' | |||
|
4345 | 4454 | |
|
4346 | 4455 | If you want to use the features of sh profile as your defaults (which |
|
4347 | 4456 | might be a good idea if you use other profiles a lot of the time but |
|
4348 |
still want the convenience of sh profile), add |
|
|
4457 | still want the convenience of sh profile), add ``import ipy_profile_sh`` | |
|
4349 | 4458 | to your ~/.ipython/ipy_user_conf.py. |
|
4350 | 4459 | |
|
4351 | 4460 | The 'sh' profile is different from the default profile in that: |
@@ -4473,73 +4582,145 b" Provide the magic function %mglob, which makes it easier (than the 'find' comman" | |||
|
4473 | 4582 | Note that the first 2 calls will put the file list in result history (_, _9, _10), and the last one will assign it to 'workfiles'. |
|
4474 | 4583 | |
|
4475 | 4584 | |
|
4585 | Prompt customization | |
|
4586 | -------------------- | |
|
4476 | 4587 | |
|
4588 | The sh profile uses the following prompt configurations:: | |
|
4477 | 4589 | |
|
4590 | o.prompt_in1= r'\C_LightBlue[\C_LightCyan\Y2\C_LightBlue]\C_Green|\#>' | |
|
4591 | o.prompt_in2= r'\C_Green|\C_LightGreen\D\C_Green>' | |
|
4478 | 4592 | |
|
4479 | Prompt customization | |
|
4480 | -------------------- | |
|
4593 | You can change the prompt configuration to your liking by editing | |
|
4594 | ipy_user_conf.py. | |
|
4481 | 4595 | |
|
4482 | The supplied ipy_profile_sh.py profile comes with an example of a very | |
|
4483 | colored and detailed prompt, mainly to serve as an illustration. The | |
|
4484 | valid escape sequences, besides color names, are:: | |
|
4596 | String lists | |
|
4597 | ============ | |
|
4598 | ||
|
4599 | String lists (IPython.genutils.SList) are handy way to process output | |
|
4600 | from system commands. They are produced by ``var = !cmd`` syntax. | |
|
4601 | ||
|
4602 | First, we acquire the output of 'ls -l':: | |
|
4603 | ||
|
4604 | [Q:doc/examples]|2> lines = !ls -l | |
|
4605 | == | |
|
4606 | ['total 23', | |
|
4607 | '-rw-rw-rw- 1 ville None 1163 Sep 30 2006 example-demo.py', | |
|
4608 | '-rw-rw-rw- 1 ville None 1927 Sep 30 2006 example-embed-short.py', | |
|
4609 | '-rwxrwxrwx 1 ville None 4606 Sep 1 17:15 example-embed.py', | |
|
4610 | '-rwxrwxrwx 1 ville None 1017 Sep 30 2006 example-gnuplot.py', | |
|
4611 | '-rwxrwxrwx 1 ville None 339 Jun 11 18:01 extension.py', | |
|
4612 | '-rwxrwxrwx 1 ville None 113 Dec 20 2006 seteditor.py', | |
|
4613 | '-rwxrwxrwx 1 ville None 245 Dec 12 2006 seteditor.pyc'] | |
|
4614 | ||
|
4615 | Now, let's take a look at the contents of 'lines' (the first number is | |
|
4616 | the list element number):: | |
|
4617 | ||
|
4618 | [Q:doc/examples]|3> lines | |
|
4619 | <3> SList (.p, .n, .l, .s, .grep(), .fields() available). Value: | |
|
4620 | ||
|
4621 | 0: total 23 | |
|
4622 | 1: -rw-rw-rw- 1 ville None 1163 Sep 30 2006 example-demo.py | |
|
4623 | 2: -rw-rw-rw- 1 ville None 1927 Sep 30 2006 example-embed-short.py | |
|
4624 | 3: -rwxrwxrwx 1 ville None 4606 Sep 1 17:15 example-embed.py | |
|
4625 | 4: -rwxrwxrwx 1 ville None 1017 Sep 30 2006 example-gnuplot.py | |
|
4626 | 5: -rwxrwxrwx 1 ville None 339 Jun 11 18:01 extension.py | |
|
4627 | 6: -rwxrwxrwx 1 ville None 113 Dec 20 2006 seteditor.py | |
|
4628 | 7: -rwxrwxrwx 1 ville None 245 Dec 12 2006 seteditor.pyc | |
|
4629 | ||
|
4630 | Now, let's filter out the 'embed' lines:: | |
|
4631 | ||
|
4632 | [Q:doc/examples]|4> l2 = lines.grep('embed',prune=1) | |
|
4633 | [Q:doc/examples]|5> l2 | |
|
4634 | <5> SList (.p, .n, .l, .s, .grep(), .fields() available). Value: | |
|
4635 | ||
|
4636 | 0: total 23 | |
|
4637 | 1: -rw-rw-rw- 1 ville None 1163 Sep 30 2006 example-demo.py | |
|
4638 | 2: -rwxrwxrwx 1 ville None 1017 Sep 30 2006 example-gnuplot.py | |
|
4639 | 3: -rwxrwxrwx 1 ville None 339 Jun 11 18:01 extension.py | |
|
4640 | 4: -rwxrwxrwx 1 ville None 113 Dec 20 2006 seteditor.py | |
|
4641 | 5: -rwxrwxrwx 1 ville None 245 Dec 12 2006 seteditor.pyc | |
|
4642 | ||
|
4643 | Now, we want strings having just file names and permissions:: | |
|
4644 | ||
|
4645 | [Q:doc/examples]|6> l2.fields(8,0) | |
|
4646 | <6> SList (.p, .n, .l, .s, .grep(), .fields() available). Value: | |
|
4647 | ||
|
4648 | 0: total | |
|
4649 | 1: example-demo.py -rw-rw-rw- | |
|
4650 | 2: example-gnuplot.py -rwxrwxrwx | |
|
4651 | 3: extension.py -rwxrwxrwx | |
|
4652 | 4: seteditor.py -rwxrwxrwx | |
|
4653 | 5: seteditor.pyc -rwxrwxrwx | |
|
4654 | ||
|
4655 | Note how the line with 'total' does not raise IndexError. | |
|
4656 | ||
|
4657 | If you want to split these (yielding lists), call fields() without | |
|
4658 | arguments:: | |
|
4659 | ||
|
4660 | [Q:doc/examples]|7> _.fields() | |
|
4661 | <7> | |
|
4662 | [['total'], | |
|
4663 | ['example-demo.py', '-rw-rw-rw-'], | |
|
4664 | ['example-gnuplot.py', '-rwxrwxrwx'], | |
|
4665 | ['extension.py', '-rwxrwxrwx'], | |
|
4666 | ['seteditor.py', '-rwxrwxrwx'], | |
|
4667 | ['seteditor.pyc', '-rwxrwxrwx']] | |
|
4668 | ||
|
4669 | If you want to pass these separated with spaces to a command (typical | |
|
4670 | for lists if files), use the .s property:: | |
|
4671 | ||
|
4672 | ||
|
4673 | [Q:doc/examples]|13> files = l2.fields(8).s | |
|
4674 | [Q:doc/examples]|14> files | |
|
4675 | <14> 'example-demo.py example-gnuplot.py extension.py seteditor.py seteditor.pyc' | |
|
4676 | [Q:doc/examples]|15> ls $files | |
|
4677 | example-demo.py example-gnuplot.py extension.py seteditor.py seteditor.pyc | |
|
4678 | ||
|
4679 | SLists are inherited from normal python lists, so every list method is | |
|
4680 | available:: | |
|
4681 | ||
|
4682 | [Q:doc/examples]|21> lines.append('hey') | |
|
4683 | ||
|
4684 | ||
|
4685 | Real world example: remove all files outside version control | |
|
4686 | ------------------------------------------------------------ | |
|
4687 | ||
|
4688 | First, capture output of "hg status":: | |
|
4689 | ||
|
4690 | [Q:/ipython]|28> out = !hg status | |
|
4691 | == | |
|
4692 | ['M IPython\\Extensions\\ipy_kitcfg.py', | |
|
4693 | 'M IPython\\Extensions\\ipy_rehashdir.py', | |
|
4694 | ... | |
|
4695 | '? build\\lib\\IPython\\Debugger.py', | |
|
4696 | '? build\\lib\\IPython\\Extensions\\InterpreterExec.py', | |
|
4697 | '? build\\lib\\IPython\\Extensions\\InterpreterPasteInput.py', | |
|
4698 | ... | |
|
4699 | ||
|
4700 | (lines starting with ? are not under version control). | |
|
4701 | ||
|
4702 | :: | |
|
4703 | ||
|
4704 | [Q:/ipython]|35> junk = out.grep(r'^\?').fields(1) | |
|
4705 | [Q:/ipython]|36> junk | |
|
4706 | <36> SList (.p, .n, .l, .s, .grep(), .fields() availab | |
|
4707 | ... | |
|
4708 | 10: build\bdist.win32\winexe\temp\_ctypes.py | |
|
4709 | 11: build\bdist.win32\winexe\temp\_hashlib.py | |
|
4710 | 12: build\bdist.win32\winexe\temp\_socket.py | |
|
4711 | ||
|
4712 | Now we can just remove these files by doing 'rm $junk.s'. | |
|
4713 | ||
|
4714 | The .s, .n, .p properties | |
|
4715 | ------------------------- | |
|
4716 | ||
|
4717 | The '.s' property returns one string where lines are separated by | |
|
4718 | single space (for convenient passing to system commands). The '.n' | |
|
4719 | property return one string where the lines are separated by '\n' | |
|
4720 | (i.e. the original output of the function). If the items in string | |
|
4721 | list are file names, '.p' can be used to get a list of "path" objects | |
|
4722 | for convenient file manipulation. | |
|
4485 | 4723 | |
|
4486 | \# | |
|
4487 | - Prompt number, wrapped in the color escapes for the input prompt | |
|
4488 | (determined by the current color scheme). | |
|
4489 | \N | |
|
4490 | - Just the prompt counter number, without any coloring wrappers. You | |
|
4491 | can thus customize the actual prompt colors manually. | |
|
4492 | \D | |
|
4493 | - Dots, as many as there are digits in \# (so they align). | |
|
4494 | \w | |
|
4495 | - Current working directory (cwd). | |
|
4496 | \W | |
|
4497 | - Basename of current working directory. | |
|
4498 | \XN | |
|
4499 | - Where N=0..5. N terms of the cwd, with $HOME written as ~. | |
|
4500 | \YN | |
|
4501 | - Where N=0..5. Like XN, but if ~ is term N+1 it's also shown. | |
|
4502 | \u | |
|
4503 | - Username. | |
|
4504 | \H | |
|
4505 | - Full hostname. | |
|
4506 | \h | |
|
4507 | - Hostname up to first '.' | |
|
4508 | \$ | |
|
4509 | - Root symbol ($ or #). | |
|
4510 | \t | |
|
4511 | - Current time, in H:M:S format. | |
|
4512 | \v | |
|
4513 | - IPython release version. | |
|
4514 | \n | |
|
4515 | - Newline. | |
|
4516 | \r | |
|
4517 | - Carriage return. | |
|
4518 | \\ | |
|
4519 | - An explicitly escaped '\'. | |
|
4520 | ||
|
4521 | You can configure your prompt colors using any ANSI color escape. Each | |
|
4522 | color escape sets the color for any subsequent text, until another | |
|
4523 | escape comes in and changes things. The valid color escapes are:: | |
|
4524 | ||
|
4525 | \C_Black | |
|
4526 | \C_Blue | |
|
4527 | \C_Brown | |
|
4528 | \C_Cyan | |
|
4529 | \C_DarkGray | |
|
4530 | \C_Green | |
|
4531 | \C_LightBlue | |
|
4532 | \C_LightCyan | |
|
4533 | \C_LightGray | |
|
4534 | \C_LightGreen | |
|
4535 | \C_LightPurple | |
|
4536 | \C_LightRed | |
|
4537 | \C_Purple | |
|
4538 | \C_Red | |
|
4539 | \C_White | |
|
4540 | \C_Yellow | |
|
4541 | \C_Normal | |
|
4542 | Stop coloring, defaults to your terminal settings. | |
|
4543 | 4724 | |
|
4544 | 4725 | Threading support |
|
4545 | 4726 | ================= |
@@ -4734,6 +4915,204 b" mechanism (Sec. 7.3 <node7.html#sec:profiles>): ''ipython -pylab -p" | |||
|
4734 | 4915 | myprofile'' will load the profile defined in ipythonrc-myprofile after |
|
4735 | 4916 | configuring matplotlib. |
|
4736 | 4917 | |
|
4918 | IPython Extension Api | |
|
4919 | ===================== | |
|
4920 | ||
|
4921 | IPython api (defined in IPython/ipapi.py) is the public api that | |
|
4922 | should be used for | |
|
4923 | ||
|
4924 | * Configuration of user preferences (.ipython/ipy_user_conf.py) | |
|
4925 | * Creating new profiles (.ipython/ipy_profile_PROFILENAME.py) | |
|
4926 | * Writing extensions | |
|
4927 | ||
|
4928 | Note that by using the extension api for configuration (editing | |
|
4929 | ipy_user_conf.py instead of ipythonrc), you get better validity checks | |
|
4930 | and get richer functionality - for example, you can import an | |
|
4931 | extension and call functions in it to configure it for your purposes. | |
|
4932 | ||
|
4933 | For an example extension (the 'sh' profile), see | |
|
4934 | IPython/Extensions/ipy_profile_sh.py. | |
|
4935 | ||
|
4936 | For the last word on what's available, see the source code of | |
|
4937 | IPython/ipapi.py. | |
|
4938 | ||
|
4939 | ||
|
4940 | Getting started | |
|
4941 | --------------- | |
|
4942 | ||
|
4943 | If you want to define an extension, create a normal python module that | |
|
4944 | can be imported. The module will access IPython functionality through | |
|
4945 | the 'ip' object defined below. | |
|
4946 | ||
|
4947 | If you are creating a new profile (e.g. foobar), name the module as | |
|
4948 | 'ipy_profile_foobar.py' and put it in your ~/.ipython directory. Then, | |
|
4949 | when you start ipython with the '-p foobar' argument, the module is | |
|
4950 | automatically imported on ipython startup. | |
|
4951 | ||
|
4952 | If you are just doing some per-user configuration, you can either | |
|
4953 | ||
|
4954 | * Put the commands directly into ipy_user_conf.py. | |
|
4955 | ||
|
4956 | * Create a new module with your customization code and import *that* | |
|
4957 | module in ipy_user_conf.py. This is preferable to the first approach, | |
|
4958 | because now you can reuse and distribute your customization code. | |
|
4959 | ||
|
4960 | Getting a handle to the api | |
|
4961 | --------------------------- | |
|
4962 | ||
|
4963 | Put this in the start of your module:: | |
|
4964 | ||
|
4965 | #!python | |
|
4966 | import IPython.ipapi | |
|
4967 | ip = IPython.ipapi.get() | |
|
4968 | ||
|
4969 | The 'ip' object will then be used for accessing IPython | |
|
4970 | functionality. 'ip' will mean this api object in all the following | |
|
4971 | code snippets. The same 'ip' that we just acquired is always | |
|
4972 | accessible in interactive IPython sessions by the name _ip - play with | |
|
4973 | it like this:: | |
|
4974 | ||
|
4975 | [~\_ipython]|81> a = 10 | |
|
4976 | [~\_ipython]|82> _ip.e | |
|
4977 | _ip.ev _ip.ex _ip.expose_magic | |
|
4978 | [~\_ipython]|82> _ip.ev('a+13') | |
|
4979 | <82> 23 | |
|
4980 | ||
|
4981 | The _ip object is also used in some examples in this document - it can | |
|
4982 | be substituted by 'ip' in non-interactive use. | |
|
4983 | ||
|
4984 | Changing options | |
|
4985 | ---------------- | |
|
4986 | ||
|
4987 | The ip object has 'options' attribute that can be used te get/set | |
|
4988 | configuration options (just as in the ipythonrc file):: | |
|
4989 | ||
|
4990 | o = ip.options | |
|
4991 | o.autocall = 2 | |
|
4992 | o.automagic = 1 | |
|
4993 | ||
|
4994 | Executing statements in IPython namespace with 'ex' and 'ev' | |
|
4995 | ------------------------------------------------------------ | |
|
4996 | ||
|
4997 | Often, you want to e.g. import some module or define something that | |
|
4998 | should be visible in IPython namespace. Use ``ip.ev`` to | |
|
4999 | *evaluate* (calculate the value of) expression and ``ip.ex`` to | |
|
5000 | '''execute''' a statement:: | |
|
5001 | ||
|
5002 | # path module will be visible to the interactive session | |
|
5003 | ip.ex("from path import path" ) | |
|
5004 | ||
|
5005 | # define a handy function 'up' that changes the working directory | |
|
5006 | ||
|
5007 | ip.ex('import os') | |
|
5008 | ip.ex("def up(): os.chdir('..')") | |
|
5009 | ||
|
5010 | ||
|
5011 | # _i2 has the input history entry #2, print its value in uppercase. | |
|
5012 | print ip.ev('_i2.upper()') | |
|
5013 | ||
|
5014 | Accessing the IPython namespace | |
|
5015 | ------------------------------- | |
|
5016 | ||
|
5017 | ip.user_ns attribute has a dictionary containing the IPython global | |
|
5018 | namespace (the namespace visible in the interactive session). | |
|
5019 | ||
|
5020 | :: | |
|
5021 | ||
|
5022 | [~\_ipython]|84> tauno = 555 | |
|
5023 | [~\_ipython]|85> _ip.user_ns['tauno'] | |
|
5024 | <85> 555 | |
|
5025 | ||
|
5026 | Defining new magic commands | |
|
5027 | --------------------------- | |
|
5028 | ||
|
5029 | The following example defines a new magic command, %impall. What the | |
|
5030 | command does should be obvious:: | |
|
5031 | ||
|
5032 | def doimp(self, arg): | |
|
5033 | ip = self.api | |
|
5034 | ip.ex("import %s; reload(%s); from %s import *" % ( | |
|
5035 | arg,arg,arg) | |
|
5036 | ) | |
|
5037 | ||
|
5038 | ip.expose_magic('impall', doimp) | |
|
5039 | ||
|
5040 | Things to observe in this example: | |
|
5041 | ||
|
5042 | * Define a function that implements the magic command using the | |
|
5043 | ipapi methods defined in this document | |
|
5044 | * The first argument of the function is 'self', i.e. the | |
|
5045 | interpreter object. It shouldn't be used directly. however. | |
|
5046 | The interpreter object is probably *not* going to remain stable | |
|
5047 | through IPython versions. | |
|
5048 | * Access the ipapi through 'self.api' instead of the global 'ip' object. | |
|
5049 | * All the text following the magic command on the command line is | |
|
5050 | contained in the second argument | |
|
5051 | * Expose the magic by ip.expose_magic() | |
|
5052 | ||
|
5053 | ||
|
5054 | Calling magic functions and system commands | |
|
5055 | ------------------------------------------- | |
|
5056 | ||
|
5057 | Use ip.magic() to execute a magic function, and ip.system() to execute | |
|
5058 | a system command:: | |
|
5059 | ||
|
5060 | # go to a bookmark | |
|
5061 | ip.magic('%cd -b relfiles') | |
|
5062 | ||
|
5063 | # execute 'ls -F' system command. Interchangeable with os.system('ls'), really. | |
|
5064 | ip.system('ls -F') | |
|
5065 | ||
|
5066 | Launching IPython instance from normal python code | |
|
5067 | -------------------------------------------------- | |
|
5068 | ||
|
5069 | Use ipapi.launch_new_instance() with an argument that specifies the | |
|
5070 | namespace to use. This can be useful for trivially embedding IPython | |
|
5071 | into your program. Here's an example of normal python program test.py | |
|
5072 | ('''without''' an existing IPython session) that launches an IPython | |
|
5073 | interpreter and regains control when the interpreter is exited:: | |
|
5074 | ||
|
5075 | [ipython]|1> cat test.py | |
|
5076 | my_ns = dict( | |
|
5077 | kissa = 15, | |
|
5078 | koira = 16) | |
|
5079 | import IPython.ipapi | |
|
5080 | print "launching IPython instance" | |
|
5081 | IPython.ipapi.launch_new_instance(my_ns) | |
|
5082 | print "Exited IPython instance!" | |
|
5083 | print "New vals:",my_ns['kissa'], my_ns['koira'] | |
|
5084 | ||
|
5085 | And here's what it looks like when run (note how we don't start it | |
|
5086 | from an ipython session):: | |
|
5087 | ||
|
5088 | Q:\ipython>python test.py | |
|
5089 | launching IPython instance | |
|
5090 | Py 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] IPy 0.7.3b3.r1975 | |
|
5091 | [ipython]|1> kissa = 444 | |
|
5092 | [ipython]|2> koira = 555 | |
|
5093 | [ipython]|3> Exit | |
|
5094 | Exited IPython instance! | |
|
5095 | New vals: 444 555 | |
|
5096 | ||
|
5097 | Accessing unexposed functionality | |
|
5098 | --------------------------------- | |
|
5099 | ||
|
5100 | There are still many features that are not exposed via the ipapi. If | |
|
5101 | you can't avoid using them, you can use the functionality in | |
|
5102 | InteractiveShell object (central IPython session class, defined in | |
|
5103 | iplib.py) through ip.IP. | |
|
5104 | ||
|
5105 | For example:: | |
|
5106 | ||
|
5107 | [~]|7> _ip.IP.expand_aliases('np','myfile.py') | |
|
5108 | <7> 'c:/opt/Notepad++/notepad++.exe myfile.py' | |
|
5109 | [~]|8> | |
|
5110 | ||
|
5111 | Still, it's preferable that if you encounter such a feature, contact | |
|
5112 | the IPython team and request that the functionality be exposed in a | |
|
5113 | future version of IPython. Things not in ipapi are more likely to | |
|
5114 | change over time. | |
|
5115 | ||
|
4737 | 5116 | Reporting bugs |
|
4738 | 5117 | ============== |
|
4739 | 5118 | |
@@ -4769,6 +5148,7 b' Brief history' | |||
|
4769 | 5148 | |
|
4770 | 5149 | |
|
4771 | 5150 | Origins |
|
5151 | ------- | |
|
4772 | 5152 | |
|
4773 | 5153 | The current IPython system grew out of the following three projects: |
|
4774 | 5154 |
General Comments 0
You need to be logged in to leave comments.
Login now