{ "metadata": { "name": "", "signature": "sha256:993106eecfd7abe1920e1dbe670c4518189c26e7b29dcc541835f7dcf6fffbb2" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "A few things that work best/only at the IPython terminal or Qt console clients" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Running code with `%run`" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile script.py\n", "x = 10\n", "y = 20\n", "z = x+y\n", "print 'z is:', z" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Writing script.py\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "%run script" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "z is: 30\n" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "x" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "10" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Event loop and GUI integration" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `%gui` magic enables the integration of GUI event loops with the interactive execution loop, allowing you to run GUI code without blocking IPython.\n", "\n", "Consider for example the execution of Qt-based code. Once we enable the Qt gui support:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%gui qt" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can define a simple Qt application class (simplified version from [this Qt tutorial](http://zetcode.com/tutorials/pyqt4/firstprograms)):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "from PyQt4 import QtGui, QtCore\n", "\n", "class SimpleWindow(QtGui.QWidget):\n", " def __init__(self, parent=None):\n", " QtGui.QWidget.__init__(self, parent)\n", "\n", " self.setGeometry(300, 300, 200, 80)\n", " self.setWindowTitle('Hello World')\n", "\n", " quit = QtGui.QPushButton('Close', self)\n", " quit.setGeometry(10, 10, 60, 35)\n", "\n", " self.connect(quit, QtCore.SIGNAL('clicked()'),\n", " self, QtCore.SLOT('close()'))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "And now we can instantiate it:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "app = QtCore.QCoreApplication.instance()\n", "if app is None:\n", " app = QtGui.QApplication([])\n", "\n", "sw = SimpleWindow()\n", "sw.show()\n", "\n", "from IPython.lib.guisupport import start_event_loop_qt4\n", "start_event_loop_qt4(app)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "But IPython still remains responsive:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "10+2" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "12" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `%gui` magic can be similarly used to control Wx, Tk, glut and pyglet applications, [as can be seen in our examples](https://github.com/ipython/ipython/tree/master/examples/lib)." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Embedding IPython in a terminal application" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile simple-embed.py\n", "# This shows how to use the new top-level embed function. It is a simpler\n", "# API that manages the creation of the embedded shell.\n", "\n", "from IPython import embed\n", "\n", "a = 10\n", "b = 20\n", "\n", "embed(header='First time', banner1='')\n", "\n", "c = 30\n", "d = 40\n", "\n", "embed(header='The second time')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Writing simple-embed.py\n" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Logging terminal sessions and transitioning to a notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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)." ] } ], "metadata": {} } ] }