PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/IPython/deathrow/gui/wx/wxIPython.py

https://github.com/cboos/ipython
Python | 266 lines | 217 code | 26 blank | 23 comment | 12 complexity | 539ae1618943122c7fe190e290b6053a MD5 | raw file
  1. #!/usr/bin/python
  2. # -*- coding: iso-8859-15 -*-
  3. import wx.aui
  4. import sys
  5. #used for about dialog
  6. from wx.lib.wordwrap import wordwrap
  7. #used for ipython GUI objects
  8. from IPython.gui.wx.ipython_view import IPShellWidget
  9. from IPython.gui.wx.ipython_history import IPythonHistoryPanel
  10. #used to invoke ipython1 wx implementation
  11. ### FIXME ### temporary disabled due to interference with 'show_in_pager' hook
  12. is_sync_frontend_ok = False
  13. try:
  14. from IPython.frontend.wx.ipythonx import IPythonXController
  15. except ImportError:
  16. is_sync_frontend_ok = False
  17. #used to create options.conf file in user directory
  18. from IPython.core.ipapi import get
  19. __version__ = 0.91
  20. __author__ = "Laurent Dufrechou"
  21. __email__ = "laurent.dufrechou _at_ gmail.com"
  22. __license__ = "BSD"
  23. #-----------------------------------------
  24. # Creating one main frame for our
  25. # application with movables windows
  26. #-----------------------------------------
  27. class MyFrame(wx.Frame):
  28. """Creating one main frame for our
  29. application with movables windows"""
  30. def __init__(self, parent=None, id=-1, title="WxIPython",
  31. pos=wx.DefaultPosition,
  32. size=(800, 600), style=wx.DEFAULT_FRAME_STYLE, sync_ok=False):
  33. wx.Frame.__init__(self, parent, id, title, pos, size, style)
  34. self._mgr = wx.aui.AuiManager()
  35. # notify PyAUI which frame to use
  36. self._mgr.SetManagedWindow(self)
  37. #create differents panels and make them persistant
  38. self.history_panel = IPythonHistoryPanel(self)
  39. self.history_panel.setOptionTrackerHook(self.optionSave)
  40. self.ipython_panel = IPShellWidget(self,background_color = "BLACK")
  41. #self.ipython_panel = IPShellWidget(self,background_color = "WHITE")
  42. if(sync_ok):
  43. self.ipython_panel2 = IPythonXController(self)
  44. else:
  45. self.ipython_panel2 = None
  46. self.ipython_panel.setHistoryTrackerHook(self.history_panel.write)
  47. self.ipython_panel.setStatusTrackerHook(self.updateStatus)
  48. self.ipython_panel.setAskExitHandler(self.OnExitDlg)
  49. self.ipython_panel.setOptionTrackerHook(self.optionSave)
  50. #Create a notebook to display different IPython shell implementations
  51. self.nb = wx.aui.AuiNotebook(self)
  52. self.optionLoad()
  53. self.statusbar = self.createStatus()
  54. self.createMenu()
  55. ########################################################################
  56. ### add the panes to the manager
  57. # main panels
  58. self._mgr.AddPane(self.nb , wx.CENTER, "IPython Shells")
  59. self.nb.AddPage(self.ipython_panel , "IPython0 Shell")
  60. if(sync_ok):
  61. self.nb.AddPage(self.ipython_panel2, "IPython1 Synchroneous Shell")
  62. self._mgr.AddPane(self.history_panel , wx.RIGHT, "IPython history")
  63. # now we specify some panel characteristics
  64. self._mgr.GetPane(self.ipython_panel).CaptionVisible(True);
  65. self._mgr.GetPane(self.history_panel).CaptionVisible(True);
  66. self._mgr.GetPane(self.history_panel).MinSize((200,400));
  67. # tell the manager to "commit" all the changes just made
  68. self._mgr.Update()
  69. #global event handling
  70. self.Bind(wx.EVT_CLOSE, self.OnClose)
  71. self.Bind(wx.EVT_MENU, self.OnClose,id=wx.ID_EXIT)
  72. self.Bind(wx.EVT_MENU, self.OnShowIPythonPanel,id=wx.ID_HIGHEST+1)
  73. self.Bind(wx.EVT_MENU, self.OnShowHistoryPanel,id=wx.ID_HIGHEST+2)
  74. self.Bind(wx.EVT_MENU, self.OnShowAbout, id=wx.ID_HIGHEST+3)
  75. self.Bind(wx.EVT_MENU, self.OnShowAllPanel,id=wx.ID_HIGHEST+6)
  76. warn_text = 'Hello from IPython and wxPython.\n'
  77. warn_text +='Please Note that this work is still EXPERIMENTAL\n'
  78. warn_text +='It does NOT emulate currently all the IPython functions.\n'
  79. warn_text +="\nIf you use MATPLOTLIB with show() you'll need to deactivate the THREADING option.\n"
  80. if(not sync_ok):
  81. warn_text +="\n->No twisted package detected, IPython1 example deactivated."
  82. dlg = wx.MessageDialog(self,
  83. warn_text,
  84. 'Warning Box',
  85. wx.OK | wx.ICON_INFORMATION
  86. )
  87. dlg.ShowModal()
  88. dlg.Destroy()
  89. def optionSave(self, name, value):
  90. ip = get()
  91. path = ip.ipython_dir
  92. opt = open(path + '/options.conf','w')
  93. try:
  94. options_ipython_panel = self.ipython_panel.getOptions()
  95. options_history_panel = self.history_panel.getOptions()
  96. for key in options_ipython_panel.keys():
  97. opt.write(key + '=' + options_ipython_panel[key]['value']+'\n')
  98. for key in options_history_panel.keys():
  99. opt.write(key + '=' + options_history_panel[key]['value']+'\n')
  100. finally:
  101. opt.close()
  102. def optionLoad(self):
  103. try:
  104. ip = get()
  105. path = ip.ipython_dir
  106. opt = open(path + '/options.conf','r')
  107. lines = opt.readlines()
  108. opt.close()
  109. options_ipython_panel = self.ipython_panel.getOptions()
  110. options_history_panel = self.history_panel.getOptions()
  111. for line in lines:
  112. key = line.split('=')[0]
  113. value = line.split('=')[1].replace('\n','').replace('\r','')
  114. if key in options_ipython_panel.keys():
  115. options_ipython_panel[key]['value'] = value
  116. elif key in options_history_panel.keys():
  117. options_history_panel[key]['value'] = value
  118. else:
  119. print >>sys.__stdout__,"Warning: key ",key,"not found in widget options. Check Options.conf"
  120. self.ipython_panel.reloadOptions(options_ipython_panel)
  121. self.history_panel.reloadOptions(options_history_panel)
  122. except IOError:
  123. print >>sys.__stdout__,"Could not open Options.conf, defaulting to default values."
  124. def createMenu(self):
  125. """local method used to create one menu bar"""
  126. mb = wx.MenuBar()
  127. file_menu = wx.Menu()
  128. file_menu.Append(wx.ID_EXIT, "Exit")
  129. view_menu = wx.Menu()
  130. view_menu.Append(wx.ID_HIGHEST+1, "Show IPython Panel")
  131. view_menu.Append(wx.ID_HIGHEST+2, "Show History Panel")
  132. view_menu.AppendSeparator()
  133. view_menu.Append(wx.ID_HIGHEST+6, "Show All")
  134. about_menu = wx.Menu()
  135. about_menu.Append(wx.ID_HIGHEST+3, "About")
  136. mb.Append(file_menu, "File")
  137. mb.Append(view_menu, "View")
  138. mb.Append(about_menu, "About")
  139. #mb.Append(options_menu, "Options")
  140. self.SetMenuBar(mb)
  141. def createStatus(self):
  142. statusbar = self.CreateStatusBar(2, wx.ST_SIZEGRIP)
  143. statusbar.SetStatusWidths([-2, -3])
  144. statusbar.SetStatusText("Ready", 0)
  145. statusbar.SetStatusText("WxIPython "+str(__version__), 1)
  146. return statusbar
  147. def updateStatus(self,text):
  148. states = {'IDLE':'Idle',
  149. 'DO_EXECUTE_LINE':'Send command',
  150. 'WAIT_END_OF_EXECUTION':'Running command',
  151. 'WAITING_USER_INPUT':'Waiting user input',
  152. 'SHOW_DOC':'Showing doc',
  153. 'SHOW_PROMPT':'Showing prompt'}
  154. self.statusbar.SetStatusText(states[text], 0)
  155. def OnClose(self, event):
  156. """#event used to close program """
  157. # deinitialize the frame manager
  158. self._mgr.UnInit()
  159. self.Destroy()
  160. event.Skip()
  161. def OnExitDlg(self, event):
  162. dlg = wx.MessageDialog(self, 'Are you sure you want to quit WxIPython',
  163. 'WxIPython exit',
  164. wx.ICON_QUESTION |
  165. wx.YES_NO | wx.NO_DEFAULT
  166. )
  167. if dlg.ShowModal() == wx.ID_YES:
  168. dlg.Destroy()
  169. self._mgr.UnInit()
  170. self.Destroy()
  171. dlg.Destroy()
  172. #event to display IPython pannel
  173. def OnShowIPythonPanel(self,event):
  174. """ #event to display Boxpannel """
  175. self._mgr.GetPane(self.ipython_panel).Show(True)
  176. self._mgr.Update()
  177. #event to display History pannel
  178. def OnShowHistoryPanel(self,event):
  179. self._mgr.GetPane(self.history_panel).Show(True)
  180. self._mgr.Update()
  181. def OnShowAllPanel(self,event):
  182. """#event to display all Pannels"""
  183. self._mgr.GetPane(self.ipython_panel).Show(True)
  184. self._mgr.GetPane(self.history_panel).Show(True)
  185. self._mgr.Update()
  186. def OnShowAbout(self, event):
  187. # First we create and fill the info object
  188. info = wx.AboutDialogInfo()
  189. info.Name = "WxIPython"
  190. info.Version = str(__version__)
  191. info.Copyright = "(C) 2007 Laurent Dufrechou"
  192. info.Description = wordwrap(
  193. "A Gui that embbed a multithreaded IPython Shell",
  194. 350, wx.ClientDC(self))
  195. info.WebSite = ("http://ipython.scipy.org/", "IPython home page")
  196. info.Developers = [ "Laurent Dufrechou" ]
  197. licenseText="BSD License.\nAll rights reserved. This program and the accompanying materials are made available under the terms of the BSD which accompanies this distribution, and is available at http://www.opensource.org/licenses/bsd-license.php"
  198. info.License = wordwrap(licenseText, 500, wx.ClientDC(self))
  199. # Then we call wx.AboutBox giving it that info object
  200. wx.AboutBox(info)
  201. #-----------------------------------------
  202. #Creating our application
  203. #-----------------------------------------
  204. class MyApp(wx.PySimpleApp):
  205. """Creating our application"""
  206. def __init__(self, sync_ok=False):
  207. wx.PySimpleApp.__init__(self)
  208. self.frame = MyFrame(sync_ok=sync_ok)
  209. self.frame.Show()
  210. #-----------------------------------------
  211. #Main loop
  212. #-----------------------------------------
  213. def main():
  214. app = MyApp(is_sync_frontend_ok)
  215. app.SetTopWindow(app.frame)
  216. app.MainLoop()
  217. #if launched as main program run this
  218. if __name__ == '__main__':
  219. main()