##// END OF EJS Templates
Implemented %loadpy magic for loading .py scripts into Qt console.
Brian Granger -
Show More
@@ -14,6 +14,7 b''
14 from collections import namedtuple
14 from collections import namedtuple
15 import re
15 import re
16 from subprocess import Popen
16 from subprocess import Popen
17 from textwrap import dedent
17
18
18 # System library imports
19 # System library imports
19 from PyQt4 import QtCore, QtGui
20 from PyQt4 import QtCore, QtGui
@@ -109,6 +110,7 b' class IPythonWidget(FrontendWidget):'
109 _payload_source_edit = 'IPython.zmq.zmqshell.ZMQInteractiveShell.edit_magic'
110 _payload_source_edit = 'IPython.zmq.zmqshell.ZMQInteractiveShell.edit_magic'
110 _payload_source_exit = 'IPython.zmq.zmqshell.ZMQInteractiveShell.ask_exit'
111 _payload_source_exit = 'IPython.zmq.zmqshell.ZMQInteractiveShell.ask_exit'
111 _payload_source_page = 'IPython.zmq.page.page'
112 _payload_source_page = 'IPython.zmq.page.page'
113 _payload_source_loadpy = 'IPython.zmq.zmqshell.ZMQInteractiveShell.magic_loadpy'
112
114
113 #---------------------------------------------------------------------------
115 #---------------------------------------------------------------------------
114 # 'object' interface
116 # 'object' interface
@@ -121,8 +123,10 b' class IPythonWidget(FrontendWidget):'
121 self._payload_handlers = {
123 self._payload_handlers = {
122 self._payload_source_edit : self._handle_payload_edit,
124 self._payload_source_edit : self._handle_payload_edit,
123 self._payload_source_exit : self._handle_payload_exit,
125 self._payload_source_exit : self._handle_payload_exit,
124 self._payload_source_page : self._handle_payload_page }
126 self._payload_source_page : self._handle_payload_page,
127 self._payload_source_loadpy : self._handle_payload_loadpy }
125 self._previous_prompt_obj = None
128 self._previous_prompt_obj = None
129 self._code_to_load = None
126
130
127 # Initialize widget styling.
131 # Initialize widget styling.
128 if self.style_sheet:
132 if self.style_sheet:
@@ -302,6 +306,11 b' class IPythonWidget(FrontendWidget):'
302 self._set_continuation_prompt(
306 self._set_continuation_prompt(
303 self._make_continuation_prompt(self._prompt), html=True)
307 self._make_continuation_prompt(self._prompt), html=True)
304
308
309 if self._code_to_load is not None:
310 text = unicode(self._code_to_load).rstrip()
311 self._insert_plain_text_into_buffer(dedent(text))
312 self._code_to_load = None
313
305 def _show_interpreter_prompt_for_reply(self, msg):
314 def _show_interpreter_prompt_for_reply(self, msg):
306 """ Reimplemented for IPython-style prompts.
315 """ Reimplemented for IPython-style prompts.
307 """
316 """
@@ -436,6 +445,11 b' class IPythonWidget(FrontendWidget):'
436 else:
445 else:
437 self._page(item['text'], html=False)
446 self._page(item['text'], html=False)
438
447
448 def _handle_payload_loadpy(self, item):
449 # Simple save the text of the .py file for later. The text is written
450 # to the buffer when _prompt_started_hook is called.
451 self._code_to_load = item['text']
452
439 #------ Trait change handlers ---------------------------------------------
453 #------ Trait change handlers ---------------------------------------------
440
454
441 def _style_sheet_changed(self):
455 def _style_sheet_changed(self):
@@ -542,5 +542,26 b' class ZMQInteractiveShell(InteractiveShell):'
542 from IPython.core.usage import gui_reference
542 from IPython.core.usage import gui_reference
543 page.page(gui_reference, auto_html=True)
543 page.page(gui_reference, auto_html=True)
544
544
545 def magic_loadpy(self, arg_s):
546 """Load a .py python script into the GUI console.
547
548 This magic command can either take a local filename or a url::
549
550 %loadpy myscript.py
551 %loadpy http://www.example.com/myscript.py
552 """
553 if not arg_s.endswith('.py'):
554 raise ValueError('%%load only works with .py files: %s' % arg_s)
555 if arg_s.startswith('http'):
556 import urllib2
557 response = urllib2.urlopen(arg_s)
558 content = response.read()
559 else:
560 content = open(arg_s).read()
561 payload = dict(
562 source='IPython.zmq.zmqshell.ZMQInteractiveShell.magic_loadpy',
563 text=content
564 )
565 self.payload_manager.write_payload(payload)
545
566
546 InteractiveShellABC.register(ZMQInteractiveShell)
567 InteractiveShellABC.register(ZMQInteractiveShell)
General Comments 0
You need to be logged in to leave comments. Login now