##// END OF EJS Templates
Applying feedbacks - changing enable_history_search from an envvar to a configurable
Applying feedbacks - changing enable_history_search from an envvar to a configurable

File last commit:

r20547:8f4e2b41
r24147:333a6a80
Show More
Terminal Usage.ipynb
285 lines | 5.8 KiB | text/plain | TextLexer

A few things that work best/only at the IPython terminal or Qt console clients

Running code with %run

In [1]:
%%writefile script.py
x = 10
y = 20
z = x+y
print('z is: %s' % z)
Writing script.py
In [2]:
%run script
z is: 30
In [3]:
x
Out[3]:
10

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:

In [4]:
%gui qt

We can define a simple Qt application class (simplified version from this Qt tutorial):

In [5]:
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:

In [6]:
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:

In [7]:
10+2
Out[7]:
12

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

In [8]:
%%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')
Writing simple-embed.py

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.

Logging terminal sessions and transitioning to a notebook

The %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).