Terminal Usage.ipynb
285 lines
| 5.8 KiB
| text/plain
|
TextLexer
Brian E. Granger
|
r17503 | { | ||
Min RK
|
r18669 | "cells": [ | ||
Brian E. Granger
|
r17503 | { | ||
Min RK
|
r18669 | "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": [ | ||||
Brian E. Granger
|
r17503 | { | ||
Min RK
|
r18669 | "name": "stdout", | ||
"output_type": "stream", | ||||
"text": [ | ||||
"Writing script.py\n" | ||||
Brian E. Granger
|
r17503 | ] | ||
Min RK
|
r18669 | } | ||
], | ||||
"source": [ | ||||
"%%writefile script.py\n", | ||||
"x = 10\n", | ||||
"y = 20\n", | ||||
"z = x+y\n", | ||||
Min RK
|
r20547 | "print('z is: %s' % z)" | ||
Min RK
|
r18669 | ] | ||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
"execution_count": 2, | ||||
"metadata": { | ||||
"collapsed": false | ||||
}, | ||||
"outputs": [ | ||||
Brian E. Granger
|
r17503 | { | ||
Min RK
|
r18669 | "name": "stdout", | ||
"output_type": "stream", | ||||
"text": [ | ||||
"z is: 30\n" | ||||
Brian E. Granger
|
r17503 | ] | ||
Min RK
|
r18669 | } | ||
], | ||||
"source": [ | ||||
"%run script" | ||||
] | ||||
}, | ||||
{ | ||||
"cell_type": "code", | ||||
"execution_count": 3, | ||||
"metadata": { | ||||
"collapsed": false | ||||
}, | ||||
"outputs": [ | ||||
Brian E. Granger
|
r17503 | { | ||
Min RK
|
r18669 | "data": { | ||
"text/plain": [ | ||||
"10" | ||||
] | ||||
}, | ||||
"execution_count": 3, | ||||
Brian E. Granger
|
r17503 | "metadata": {}, | ||
Min RK
|
r18669 | "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": [ | ||||
Brian E. Granger
|
r17503 | { | ||
Min RK
|
r18669 | "data": { | ||
"text/plain": [ | ||||
"12" | ||||
] | ||||
}, | ||||
"execution_count": 7, | ||||
Brian E. Granger
|
r17503 | "metadata": {}, | ||
Min RK
|
r18669 | "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", | ||||
Min RK
|
r20547 | "execution_count": 8, | ||
Min RK
|
r18669 | "metadata": { | ||
"collapsed": false | ||||
}, | ||||
"outputs": [ | ||||
Brian E. Granger
|
r17503 | { | ||
Min RK
|
r18669 | "name": "stdout", | ||
"output_type": "stream", | ||||
"text": [ | ||||
"Writing simple-embed.py\n" | ||||
Brian E. Granger
|
r17503 | ] | ||
} | ||||
], | ||||
Min RK
|
r18669 | "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)." | ||||
] | ||||
Brian E. Granger
|
r17503 | } | ||
Min RK
|
r18669 | ], | ||
Min RK
|
r20278 | "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" | ||||
} | ||||
}, | ||||
Min RK
|
r18669 | "nbformat": 4, | ||
"nbformat_minor": 0 | ||||
Min RK
|
r20278 | } | ||