{
 "cells": [
  {
   "cell_type": "markdown",
   "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",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing script.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile script.py\n",
    "x = 10\n",
    "y = 20\n",
    "z = x+y\n",
    "print('z is: %s' % z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "z is: 30\n"
     ]
    }
   ],
   "source": [
    "%run script"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "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",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%gui qt"
   ]
  },
  {
   "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",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "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()'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And now we can instantiate it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "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)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But IPython still remains responsive:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10+2"
   ]
  },
  {
   "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": "markdown",
   "metadata": {},
   "source": [
    "## Embedding IPython in a terminal application"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing simple-embed.py\n"
     ]
    }
   ],
   "source": [
    "%%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')"
   ]
  },
  {
   "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": "markdown",
   "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": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
