##// END OF EJS Templates
Add demo app. Add callback for exit to the ipython0 code.
Gael Varoquaux -
Show More
@@ -0,0 +1,80 b''
1 """
2 Entry point for a simple application giving a graphical frontend to
3 ipython.
4 """
5
6 import wx
7 from wx_frontend import WxController
8
9 class WIPythonController(WxController):
10 """ Sub class of WxController that adds some application-specific
11 bindings.
12 """
13
14 def __init__(self, *args, **kwargs):
15 WxController.__init__(self, *args, **kwargs)
16 self.ipython0.ask_exit = self.do_exit
17
18
19 def _on_key_down(self, event, skip=True):
20 # Intercept Ctrl-D to quit
21 if event.KeyCode == ord('D') and event.ControlDown() and \
22 self.get_current_edit_buffer()=='':
23 wx.CallAfter(self.ask_exit)
24 else:
25 WxController._on_key_down(self, event, skip=skip)
26
27
28 def ask_exit(self):
29 """ Ask the user whether to exit.
30 """
31 self.write('\n')
32 self.capture_output()
33 self.ipython0.shell.exit()
34 self.release_output()
35 wx.Yield()
36 if not self.ipython0.exit_now:
37 self.new_prompt(self.prompt % (self.last_result['number'] + 1))
38
39
40 def do_exit(self):
41 """ Exits the interpreter, kills the windows.
42 """
43 WxController.do_exit(self)
44 # Remove the callbacks, to avoid PyDeadObjectErrors
45 do_nothing = lambda *args, **kwargs: True
46 self.release_output()
47 self._on_key_down = do_nothing
48 self._on_key_up = do_nothing
49 self._on_enter = do_nothing
50 self.after_execute = do_nothing
51 wx.Yield()
52 wx.CallAfter(self.Parent.Destroy)
53
54
55
56 class WIPython(wx.Frame):
57 """ Main frame of the WIPython app.
58 """
59
60 def __init__(self, parent, id, title):
61 wx.Frame.__init__(self, parent, id, title, size=(300,250))
62 self._sizer = wx.BoxSizer(wx.VERTICAL)
63 self.shell = WIPythonController(self)
64 self._sizer.Add(self.shell, 1, wx.EXPAND)
65 self.SetSizer(self._sizer)
66 self.SetAutoLayout(1)
67 self.Show(True)
68
69
70 def main():
71 app = wx.PySimpleApp()
72 frame = WIPython(None, wx.ID_ANY, 'WIpython')
73 frame.shell.SetFocus()
74 frame.shell.app = app
75 frame.SetSize((680, 460))
76
77 app.MainLoop()
78
79 if __name__ == '__main__':
80 main()
@@ -49,7 +49,7 b' ip.expose_magic("rehash", magic_rehash)'
49 49 def magic_Quit(self, parameter_s=''):
50 50 """Exit IPython without confirmation (like %Exit)."""
51 51
52 self.shell.exit_now = True
52 self.shell.ask_exit()
53 53
54 54 ip.expose_magic("Quit", magic_Quit)
55 55
@@ -2459,7 +2459,7 b' Defaulting color scheme to \'NoColor\'"""'
2459 2459 def magic_Exit(self, parameter_s=''):
2460 2460 """Exit IPython without confirmation."""
2461 2461
2462 self.shell.exit_now = True
2462 self.shell.ask_exit()
2463 2463
2464 2464 #......................................................................
2465 2465 # Functions to implement unix shell-type things
@@ -24,6 +24,7 b' from IPython.ipapi import IPApi'
24 24 from IPython.kernel.core.sync_output_trap import SyncOutputTrap
25 25
26 26 from IPython.genutils import Term
27 import pydoc
27 28
28 29 #-------------------------------------------------------------------------------
29 30 # Utility functions (temporary, should be moved out of here)
@@ -71,9 +72,10 b' class PrefilterFrontEnd(LineFrontEndBase):'
71 72 # terminal
72 73 self.shell.output_trap = SyncOutputTrap(write_out=self.write,
73 74 write_err=self.write)
74
75 import pydoc
76 pydoc.help.output = self.shell.output_trap.out
75 # Capture and release the outputs, to make sure all the
76 # shadow variables are set
77 self.capture_output()
78 self.release_output()
77 79
78 80
79 81 def prefilter_input(self, input_string):
@@ -105,8 +107,15 b' class PrefilterFrontEnd(LineFrontEndBase):'
105 107 self.release_output()
106 108
107 109
110 def execute(self, python_string, raw_string=None):
111 self.capture_output()
112 LineFrontEndBase.execute(self, python_string,
113 raw_string=raw_string)
114 self.release_output()
115
116
108 117 def capture_output(self):
109 """ Capture all the output mechanism we can think of.
118 """ Capture all the output mechanisms we can think of.
110 119 """
111 120 self.__old_cout_write = Term.cout.write
112 121 self.__old_err_write = Term.cerr.write
@@ -116,16 +125,18 b' class PrefilterFrontEnd(LineFrontEndBase):'
116 125 self.__old_stderr= sys.stderr
117 126 sys.stdout = Term.cout
118 127 sys.stderr = Term.cerr
128 self.__old_help_output = pydoc.help.output
129 pydoc.help.output = self.shell.output_trap.out
119 130
120 131
121 132 def release_output(self):
122 """ Release all the different captures we have made,
123 and flush the buffers.
133 """ Release all the different captures we have made.
124 134 """
125 135 Term.cout.write = self.__old_cout_write
126 136 Term.cerr.write = self.__old_err_write
127 137 sys.stdout = self.__old_stdout
128 138 sys.stderr = self.__old_stderr
139 pydoc.help.output = self.__old_help_output
129 140
130 141
131 142 def complete(self, line):
@@ -139,3 +150,8 b' class PrefilterFrontEnd(LineFrontEndBase):'
139 150 return line, completions
140 151
141 152
153 def do_exit(self):
154 """ Exit the shell, cleanup and save the history.
155 """
156 self.ipython0.atexit_operations()
157
@@ -151,10 +151,18 b' class WxController(PrefilterFrontEnd, ConsoleWidget):'
151 151 #self.SetSelection(self.GetLength()-1, self.GetLength())
152 152 #self.ReplaceSelection('')
153 153 self.GotoPos(self.GetLength())
154 PrefilterFrontEnd.execute(self, python_string, raw_string=raw_string)
155
156
157 def capture_output(self):
154 158 self.__old_raw_input = __builtin__.raw_input
155 159 __builtin__.raw_input = self.raw_input
156 PrefilterFrontEnd.execute(self, python_string, raw_string=raw_string)
160 PrefilterFrontEnd.capture_output(self)
161
162
163 def release_output(self):
157 164 __builtin__.raw_input = self.__old_raw_input
165 PrefilterFrontEnd.capture_output(self)
158 166
159 167
160 168 def after_execute(self):
@@ -734,7 +734,7 b' class InteractiveShell(object,Magic):'
734 734 batchrun = True
735 735 # without -i option, exit after running the batch file
736 736 if batchrun and not self.rc.interact:
737 self.exit_now = True
737 self.ask_exit()
738 738
739 739 def add_builtins(self):
740 740 """Store ipython references into the builtin namespace.
@@ -1590,7 +1590,7 b' want to merge them back into the new files.""" % locals()'
1590 1590 #sys.argv = ['-c']
1591 1591 self.push(self.prefilter(self.rc.c, False))
1592 1592 if not self.rc.interact:
1593 self.exit_now = True
1593 self.ask_exit()
1594 1594
1595 1595 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1596 1596 """Embeds IPython into a running python program.
@@ -1752,7 +1752,8 b' want to merge them back into the new files.""" % locals()'
1752 1752
1753 1753 if self.has_readline:
1754 1754 self.readline_startup_hook(self.pre_readline)
1755 # exit_now is set by a call to %Exit or %Quit
1755 # exit_now is set by a call to %Exit or %Quit, through the
1756 # ask_exit callback.
1756 1757
1757 1758 while not self.exit_now:
1758 1759 self.hooks.pre_prompt_hook()
@@ -2157,7 +2158,7 b' want to merge them back into the new files.""" % locals()'
2157 2158 except ValueError:
2158 2159 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2159 2160 " or sys.stdout.close()!\nExiting IPython!")
2160 self.exit_now = True
2161 self.ask_exit()
2161 2162 return ""
2162 2163
2163 2164 # Try to be reasonably smart about not re-indenting pasted input more
@@ -2507,16 +2508,20 b' want to merge them back into the new files.""" % locals()'
2507 2508 """Write a string to the default error output"""
2508 2509 Term.cerr.write(data)
2509 2510
2511 def ask_exit(self):
2512 """ Call for exiting. Can be overiden and used as a callback. """
2513 self.exit_now = True
2514
2510 2515 def exit(self):
2511 2516 """Handle interactive exit.
2512 2517
2513 This method sets the exit_now attribute."""
2518 This method calls the ask_exit callback."""
2514 2519
2515 2520 if self.rc.confirm_exit:
2516 2521 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2517 self.exit_now = True
2522 self.ask_exit()
2518 2523 else:
2519 self.exit_now = True
2524 self.ask_exit()
2520 2525
2521 2526 def safe_execfile(self,fname,*where,**kw):
2522 2527 """A safe version of the builtin execfile().
General Comments 0
You need to be logged in to leave comments. Login now