##// END OF EJS Templates
Merge pull request #6235 from jdfreder/addclass...
Merge pull request #6235 from jdfreder/addclass Remove add/remove_class and set/get_css.

File last commit:

r16120:24b93a1d
r17949:a2e1c674 merge
Show More
Script Magics.ipynb
481 lines | 10.4 KiB | text/plain | TextLexer
MinRK
add Script Magics demo notebook
r7402 {
"metadata": {
"name": "Script Magics"
},
"nbformat": 3,
MinRK
rebuild example notebooks...
r7739 "nbformat_minor": 0,
MinRK
add Script Magics demo notebook
r7402 "worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "source": [
"Running Scripts from IPython"
]
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "source": [
MinRK
rebuild example notebooks...
r7739 "IPython has a `%%script` cell magic, which lets you run a cell in\n",
"a subprocess of any interpreter on your system, such as: bash, ruby, perl, zsh, R, etc.\n",
"\n",
MinRK
add Script Magics demo notebook
r7402 "It can even be a script of your own, which expects input on stdin."
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add Script Magics demo notebook
r7402 "input": [
"import sys"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "outputs": [],
"prompt_number": 1
},
{
Brian Granger
More changes to example notebooks.
r9193 "cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Basic usage"
]
},
{
MinRK
add Script Magics demo notebook
r7402 "cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "source": [
MinRK
rebuild example notebooks...
r7739 "To use it, simply pass a path or shell command to the program you want to run on the `%%script` line,\n",
MinRK
add Script Magics demo notebook
r7402 "and the rest of the cell will be run by that script, and stdout/err from the subprocess are captured and displayed."
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add Script Magics demo notebook
r7402 "input": [
MinRK
rebuild example notebooks...
r7739 "%%script python\n",
"import sys\n",
MinRK
add Script Magics demo notebook
r7402 "print 'hello from Python %s' % sys.version"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
MinRK
rebuild example notebooks...
r7739 "hello from Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53) \n",
"[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)]\n"
MinRK
add Script Magics demo notebook
r7402 ]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add Script Magics demo notebook
r7402 "input": [
MinRK
rebuild example notebooks...
r7739 "%%script python3\n",
"import sys\n",
MinRK
add Script Magics demo notebook
r7402 "print('hello from Python: %s' % sys.version)"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
MinRK
rebuild example notebooks...
r7739 "hello from Python: 3.2.3 (v3.2.3:3d0686d90f55, Apr 10 2012, 11:25:50) \n",
"[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]\n"
MinRK
add Script Magics demo notebook
r7402 ]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "source": [
MinRK
rebuild example notebooks...
r7739 "IPython also creates aliases for a few common interpreters, such as bash, ruby, perl, etc.\n",
"\n",
MinRK
add Script Magics demo notebook
r7402 "These are all equivalent to `%%script <name>`"
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add Script Magics demo notebook
r7402 "input": [
MinRK
rebuild example notebooks...
r7739 "%%ruby\n",
MinRK
add Script Magics demo notebook
r7402 "puts \"Hello from Ruby #{RUBY_VERSION}\""
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
MinRK
rebuild example notebooks...
r7739 "Hello from Ruby 1.8.7\n"
MinRK
add Script Magics demo notebook
r7402 ]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add Script Magics demo notebook
r7402 "input": [
MinRK
rebuild example notebooks...
r7739 "%%bash\n",
MinRK
add Script Magics demo notebook
r7402 "echo \"hello from $BASH\""
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
MinRK
rebuild example notebooks...
r7739 "hello from /usr/local/bin/bash\n"
MinRK
add Script Magics demo notebook
r7402 ]
}
],
"prompt_number": 5
},
{
"cell_type": "heading",
"level": 2,
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "source": [
"Capturing output"
]
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "source": [
"You can also capture stdout/err from these subprocesses into Python variables, instead of letting them go directly to stdout/err"
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add Script Magics demo notebook
r7402 "input": [
MinRK
rebuild example notebooks...
r7739 "%%bash\n",
"echo \"hi, stdout\"\n",
"echo \"hello, stderr\" >&2\n"
MinRK
add Script Magics demo notebook
r7402 ],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
MinRK
rebuild example notebooks...
r7739 "hi, stdout\n"
MinRK
add Script Magics demo notebook
r7402 ]
},
{
"output_type": "stream",
"stream": "stderr",
"text": [
MinRK
rebuild example notebooks...
r7739 "hello, stderr\n"
MinRK
add Script Magics demo notebook
r7402 ]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add Script Magics demo notebook
r7402 "input": [
MinRK
rebuild example notebooks...
r7739 "%%bash --out output --err error\n",
"echo \"hi, stdout\"\n",
MinRK
add Script Magics demo notebook
r7402 "echo \"hello, stderr\" >&2"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add Script Magics demo notebook
r7402 "input": [
Thomas Kluyver
More changes to example notebooks for Python 3 compatibility
r9198 "print(error)\n",
"print(output)"
MinRK
add Script Magics demo notebook
r7402 ],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
MinRK
rebuild example notebooks...
r7739 "hello, stderr\n",
"\n",
"hi, stdout\n",
"\n"
MinRK
add Script Magics demo notebook
r7402 ]
}
],
"prompt_number": 8
},
{
"cell_type": "heading",
"level": 2,
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "source": [
"Background Scripts"
]
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "source": [
MinRK
rebuild example notebooks...
r7739 "These scripts can be run in the background, by adding the `--bg` flag.\n",
"\n",
"When you do this, output is discarded unless you use the `--out/err`\n",
MinRK
add Script Magics demo notebook
r7402 "flags to store output as above."
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add Script Magics demo notebook
r7402 "input": [
MinRK
rebuild example notebooks...
r7739 "%%ruby --bg --out ruby_lines\n",
"for n in 1...10\n",
" sleep 1\n",
" puts \"line #{n}\"\n",
" STDOUT.flush\n",
MinRK
add Script Magics demo notebook
r7402 "end"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
MinRK
rebuild example notebooks...
r7739 "Starting job # 0 in a separate thread.\n"
MinRK
add Script Magics demo notebook
r7402 ]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "source": [
MinRK
rebuild example notebooks...
r7739 "When you do store output of a background thread, these are the stdout/err *pipes*,\n",
MinRK
add Script Magics demo notebook
r7402 "rather than the text of the output."
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add Script Magics demo notebook
r7402 "input": [
"ruby_lines"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "outputs": [
{
"output_type": "pyout",
"prompt_number": 10,
"text": [
MinRK
rebuild example notebooks...
r7739 "<open file '<fdopen>', mode 'rb' at 0x10a4be660>"
MinRK
add Script Magics demo notebook
r7402 ]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add Script Magics demo notebook
r7402 "input": [
Thomas Kluyver
More changes to example notebooks for Python 3 compatibility
r9198 "print(ruby_lines.read())"
MinRK
add Script Magics demo notebook
r7402 ],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
MinRK
rebuild example notebooks...
r7739 "line 1\n",
"line 2\n",
"line 3\n",
"line 4\n",
"line 5\n",
"line 6\n",
"line 7\n",
"line 8\n",
"line 9\n",
"\n"
MinRK
add Script Magics demo notebook
r7402 ]
}
],
"prompt_number": 11
},
{
"cell_type": "heading",
"level": 2,
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "source": [
"Arguments to subcommand"
]
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "source": [
MinRK
rebuild example notebooks...
r7739 "You can pass arguments the subcommand as well,\n",
MinRK
add Script Magics demo notebook
r7402 "such as this example instructing Python to use integer division from Python 3:"
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add Script Magics demo notebook
r7402 "input": [
MinRK
rebuild example notebooks...
r7739 "%%script python -Qnew\n",
MinRK
add Script Magics demo notebook
r7402 "print 1/3"
],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
MinRK
rebuild example notebooks...
r7739 "0.333333333333\n"
MinRK
add Script Magics demo notebook
r7402 ]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "source": [
MinRK
rebuild example notebooks...
r7739 "You can really specify *any* program for `%%script`,\n",
MinRK
add Script Magics demo notebook
r7402 "for instance here is a 'program' that echos the lines of stdin, with delays between each line."
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add Script Magics demo notebook
r7402 "input": [
MinRK
rebuild example notebooks...
r7739 "%%script --bg --out bashout bash -c \"while read line; do echo $line; sleep 1; done\"\n",
"line 1\n",
"line 2\n",
"line 3\n",
"line 4\n",
"line 5\n"
MinRK
add Script Magics demo notebook
r7402 ],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
MinRK
rebuild example notebooks...
r7739 "Starting job # 2 in a separate thread.\n"
MinRK
add Script Magics demo notebook
r7402 ]
}
],
"prompt_number": 13
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "source": [
MinRK
rebuild example notebooks...
r7739 "Remember, since the output of a background script is just the stdout pipe,\n",
MinRK
add Script Magics demo notebook
r7402 "you can read it as lines become available:"
]
},
{
"cell_type": "code",
MinRK
rebuild example notebooks...
r7739 "collapsed": false,
MinRK
add Script Magics demo notebook
r7402 "input": [
MinRK
rebuild example notebooks...
r7739 "import time\n",
"tic = time.time()\n",
"line = True\n",
"while True:\n",
" line = bashout.readline()\n",
" if not line:\n",
" break\n",
" sys.stdout.write(\"%.1fs: %s\" %(time.time()-tic, line))\n",
" sys.stdout.flush()\n"
MinRK
add Script Magics demo notebook
r7402 ],
"language": "python",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
MinRK
rebuild example notebooks...
r7739 "0.0s: line 1\n"
MinRK
add Script Magics demo notebook
r7402 ]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
MinRK
rebuild example notebooks...
r7739 "1.0s: line 2\n"
MinRK
add Script Magics demo notebook
r7402 ]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
MinRK
rebuild example notebooks...
r7739 "2.0s: line 3\n"
MinRK
add Script Magics demo notebook
r7402 ]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
MinRK
rebuild example notebooks...
r7739 "3.0s: line 4\n"
MinRK
add Script Magics demo notebook
r7402 ]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
MinRK
rebuild example notebooks...
r7739 "4.0s: line 5\n"
MinRK
add Script Magics demo notebook
r7402 ]
}
],
"prompt_number": 14
},
{
"cell_type": "heading",
"level": 2,
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "source": [
"Configuring the default ScriptMagics"
]
},
{
"cell_type": "markdown",
MinRK
rebuild example notebooks...
r7739 "metadata": {},
MinRK
add Script Magics demo notebook
r7402 "source": [
MinRK
rebuild example notebooks...
r7739 "The list of aliased script magics is configurable.\n",
"\n",
"The default is to pick from a few common interpreters, and use them if found, but you can specify your own in ipython_config.py:\n",
"\n",
" c.ScriptMagics.scripts = ['R', 'pypy', 'myprogram']\n",
"\n",
"And if any of these programs do not apear on your default PATH, then you would also need to specify their location with:\n",
"\n",
MinRK
add Script Magics demo notebook
r7402 " c.ScriptMagics.script_paths = {'myprogram': '/opt/path/to/myprogram'}"
]
}
MinRK
rebuild example notebooks...
r7739 ],
"metadata": {}
MinRK
add Script Magics demo notebook
r7402 }
]
}