%%writefile script.py
x = 10
y = 20
z = x+y
print('z is: %s' % z)

%run script

x

"""
Explanation: A few things that work best/only at the IPython terminal or Qt console clients
Running code with %run
End of explanation
"""


%gui qt

"""
Explanation: Event loop and GUI integration
The %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:
End of explanation
"""


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()'))

"""
Explanation: We can define a simple Qt application class (simplified version from this Qt tutorial):
End of explanation
"""


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)

"""
Explanation: And now we can instantiate it:
End of explanation
"""


10+2

"""
Explanation: But IPython still remains responsive:
End of explanation
"""


%%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')

"""
Explanation: The %gui magic can be similarly used to control Wx, Tk, glut and pyglet applications, as can be seen in our examples.
Embedding IPython in a terminal application
End of explanation
"""


