ipythonx.py
118 lines
| 3.3 KiB
| text/x-python
|
PythonLexer
Gael Varoquaux
|
r1391 | """ | ||
Entry point for a simple application giving a graphical frontend to | ||||
ipython. | ||||
""" | ||||
gvaroquaux
|
r1477 | try: | ||
import wx | ||||
except ImportError, e: | ||||
Brian Granger
|
r2293 | e.args[0] = """%s | ||
gvaroquaux
|
r1477 | ________________________________________________________________________________ | ||
You need wxPython to run this application. | ||||
Brian Granger
|
r2293 | """ % e.args[0] | ||
gvaroquaux
|
r1477 | raise e | ||
Gael Varoquaux
|
r1391 | from wx_frontend import WxController | ||
Gael Varoquaux
|
r1437 | import __builtin__ | ||
Gael Varoquaux
|
r1391 | |||
Gael Varoquaux
|
r1495 | |||
Gael Varoquaux
|
r1438 | class IPythonXController(WxController): | ||
Gael Varoquaux
|
r1391 | """ Sub class of WxController that adds some application-specific | ||
bindings. | ||||
""" | ||||
gvaroquaux
|
r1475 | debug = False | ||
Gael Varoquaux
|
r1450 | |||
Gael Varoquaux
|
r1391 | def __init__(self, *args, **kwargs): | ||
WxController.__init__(self, *args, **kwargs) | ||||
self.ipython0.ask_exit = self.do_exit | ||||
Gael Varoquaux
|
r1495 | # Scroll to top | ||
maxrange = self.GetScrollRange(wx.VERTICAL) | ||||
self.ScrollLines(-maxrange) | ||||
Gael Varoquaux
|
r1391 | |||
def _on_key_down(self, event, skip=True): | ||||
# Intercept Ctrl-D to quit | ||||
if event.KeyCode == ord('D') and event.ControlDown() and \ | ||||
gvaroquaux
|
r1462 | self.input_buffer == '' and \ | ||
gvaroquaux
|
r1449 | self._input_state == 'readline': | ||
Gael Varoquaux
|
r1391 | wx.CallAfter(self.ask_exit) | ||
else: | ||||
WxController._on_key_down(self, event, skip=skip) | ||||
def ask_exit(self): | ||||
""" Ask the user whether to exit. | ||||
""" | ||||
gvaroquaux
|
r1479 | self._input_state = 'subprocess' | ||
self.write('\n', refresh=False) | ||||
Gael Varoquaux
|
r1391 | self.capture_output() | ||
Brian Granger
|
r2499 | self.ipython0.exit() | ||
Gael Varoquaux
|
r1391 | self.release_output() | ||
if not self.ipython0.exit_now: | ||||
gvaroquaux
|
r1479 | wx.CallAfter(self.new_prompt, | ||
self.input_prompt_template.substitute( | ||||
Gael Varoquaux
|
r1458 | number=self.last_result['number'] + 1)) | ||
gvaroquaux
|
r1502 | else: | ||
wx.CallAfter(wx.GetApp().Exit) | ||||
self.write('Exiting ...', refresh=False) | ||||
Gael Varoquaux
|
r1391 | |||
def do_exit(self): | ||||
""" Exits the interpreter, kills the windows. | ||||
""" | ||||
WxController.do_exit(self) | ||||
self.release_output() | ||||
Gael Varoquaux
|
r1392 | wx.CallAfter(wx.Exit) | ||
Gael Varoquaux
|
r1391 | |||
Gael Varoquaux
|
r1438 | class IPythonX(wx.Frame): | ||
""" Main frame of the IPythonX app. | ||||
Gael Varoquaux
|
r1391 | """ | ||
gvaroquaux
|
r1484 | def __init__(self, parent, id, title, debug=False): | ||
Gael Varoquaux
|
r1391 | wx.Frame.__init__(self, parent, id, title, size=(300,250)) | ||
self._sizer = wx.BoxSizer(wx.VERTICAL) | ||||
gvaroquaux
|
r1484 | self.shell = IPythonXController(self, debug=debug) | ||
Gael Varoquaux
|
r1391 | self._sizer.Add(self.shell, 1, wx.EXPAND) | ||
self.SetSizer(self._sizer) | ||||
self.SetAutoLayout(1) | ||||
self.Show(True) | ||||
Gael Varoquaux
|
r1732 | wx.EVT_CLOSE(self, self.on_close) | ||
def on_close(self, event): | ||||
""" Called on closing the windows. | ||||
Stops the event loop, to close all the child windows. | ||||
""" | ||||
wx.CallAfter(wx.Exit) | ||||
Gael Varoquaux
|
r1391 | |||
def main(): | ||||
gvaroquaux
|
r1475 | from optparse import OptionParser | ||
usage = """usage: %prog [options] | ||||
Simple graphical frontend to IPython, using WxWidgets.""" | ||||
parser = OptionParser(usage=usage) | ||||
parser.add_option("-d", "--debug", | ||||
action="store_true", dest="debug", default=False, | ||||
help="Enable debug message for the wx frontend.") | ||||
options, args = parser.parse_args() | ||||
gvaroquaux
|
r1479 | # Clear the options, to avoid having the ipython0 instance complain | ||
import sys | ||||
sys.argv = sys.argv[:1] | ||||
Gael Varoquaux
|
r1391 | app = wx.PySimpleApp() | ||
gvaroquaux
|
r1484 | frame = IPythonX(None, wx.ID_ANY, 'IPythonX', debug=options.debug) | ||
Gael Varoquaux
|
r1391 | frame.shell.SetFocus() | ||
frame.shell.app = app | ||||
frame.SetSize((680, 460)) | ||||
app.MainLoop() | ||||
if __name__ == '__main__': | ||||
main() | ||||