~ellisonbg/ipython/bugfixes0411409

« back to all changes in this revision

Viewing changes to test/test_embed.py

  • Committer: ville
  • Date: 2008-02-16 09:50:47 UTC
  • mto: (0.12.1 ipython_main)
  • mto: This revision was merged to the branch mainline in revision 990.
  • Revision ID: ville@ville-pc-20080216095047-500x6dluki1iz40o
initialization (no svn history)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
""" An example of one way to embed IPython in your own application 
 
2
 
 
3
This basically means starting up IPython with some of your programs objects visible in the IPython 
 
4
user namespace.
 
5
 
 
6
"""
 
7
 
 
8
import sys
 
9
sys.path.append('..')
 
10
 
 
11
import IPython.ipapi
 
12
 
 
13
my_ns = dict(a=10)
 
14
 
 
15
ses = IPython.ipapi.make_session(my_ns)
 
16
 
 
17
# Now get the ipapi instance, to be stored somewhere in your program for manipulation of the running 
 
18
# IPython session. See http://ipython.scipy.org/moin/IpythonExtensionApi
 
19
 
 
20
ip = ses.IP.getapi()   
 
21
 
 
22
# let's play with the ipapi a bit, creating a magic function for a soon-to-be-started IPython
 
23
def mymagic_f(self,s):
 
24
    print "mymagic says",s
 
25
 
 
26
ip.expose_magic("mymagic",mymagic_f)
 
27
 
 
28
# And finally, start the IPython interaction! This will block until you say Exit.
 
29
 
 
30
ses.mainloop()
 
31
 
 
32
print "IPython session finished! namespace content:",my_ns