%%writefile script.py x = 10 y = 20 z = x+y print('z is: %s' % z)
z is: 30
%gui magic enables the integration of GUI event loops with the interactive execution loop, allowing you to run GUI code without blocking IPython.
Consider for example the execution of Qt-based code. Once we enable the Qt gui support:
import sys from PyQt4 import QtGui, QtCore class SimpleWindow(QtGui.QWidget): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.setGeometry(300, 300, 200, 80) self.setWindowTitle('Hello World') quit = QtGui.QPushButton('Close', self) quit.setGeometry(10, 10, 60, 35) self.connect(quit, QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('close()'))
And now we can instantiate it:
app = QtCore.QCoreApplication.instance() if app is None: app = QtGui.QApplication() sw = SimpleWindow() sw.show() from IPython.lib.guisupport import start_event_loop_qt4 start_event_loop_qt4(app)
But IPython still remains responsive:
%gui magic can be similarly used to control Wx, Tk, glut and pyglet applications, as can be seen in our examples.
%%writefile simple-embed.py # This shows how to use the new top-level embed function. It is a simpler # API that manages the creation of the embedded shell. from IPython import embed a = 10 b = 20 embed(header='First time', banner1='') c = 30 d = 40 embed(header='The second time')
The example in kernel-embedding shows how to embed a full kernel into an application and how to connect to this kernel from an external process.
%logstart magic lets you log a terminal session with various degrees of control, and the
%notebook one will convert an interactive console session into a notebook with all input cells already created for you (but no output).