{ "cells": [ { "cell_type": "markdown", "metadata": { "nbsphinx": "hidden" }, "source": [ "This notebook is part of the `nbsphinx` documentation: http://nbsphinx.readthedocs.io/." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Code Cells\n", "\n", "## Code, Output, Streams\n", "\n", "An empty code cell:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Two empty lines:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Leading/trailing empty lines:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\n", "\n", "# 2 empty lines before, 1 after\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A simple output:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "6 * 7" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The standard output stream:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print('Hello, world!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Normal output + standard output" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print('Hello, world!')\n", "6 * 7" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The standard error stream is highlighted and displayed just below the code cell.\n", "The standard output stream comes afterwards (with no special highlighting).\n", "Finally, the \"normal\" output is displayed." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import sys\n", "\n", "print(\"I'll appear on the standard error stream\", file=sys.stderr, flush=True)\n", "print(\"I'll appear on the standard output stream\")\n", "\"I'm the 'normal' output\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Cell Magics\n", "\n", "Cells can contain code in other languages by means of [cell magics](http://ipython.readthedocs.io/en/stable/interactive/magics.html#cell-magics):" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%bash\n", "for i in 1 2 3\n", "do\n", " echo $i\n", "done" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Special Display Formats\n", "\n", "See [IPython example notebook](https://nbviewer.jupyter.org/github/ipython/ipython/blob/master/examples/IPython Kernel/Rich Output.ipynb).\n", "\n", "TODO: tables? e.g. Pandas DataFrame?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from IPython.display import display" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Local Image Files" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from IPython.display import Image\n", "i = Image(filename='images/notebook_icon.png')\n", "i" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "display(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For some reason this doesn't work with `Image(...)`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from IPython.display import SVG\n", "SVG(filename='images/python_logo.svg')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Image URLs" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Image(url='https://www.python.org/static/img/python-logo-large.png')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Image(url='https://www.python.org/static/img/python-logo-large.png', embed=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Image(url='http://jupyter.org/assets/nav_logo.svg')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Image(url='https://www.python.org/static/favicon.ico')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Image(url='http://python.org/images/python-logo.gif')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Math" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from IPython.display import Math\n", "eq = Math(r\"\\int_{-\\infty}^\\infty f(x) \\delta(x - x_0) dx = f(x_0)\")\n", "eq" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "display(eq)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%latex\n", "\\begin{equation}\n", "\\int_{-\\infty}^\\infty f(x) \\delta(x - x_0) dx = f(x_0)\n", "\\end{equation}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### YouTube Videos" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from IPython.display import YouTubeVideo\n", "YouTubeVideo('WAikxUGbomY')" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "### Arbitrary JavaScript Output (HTML only)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [], "source": [ "%%javascript\n", "\n", "var text = document.createTextNode(\"Hello, I was generated with JavaScript!\");\n", "// Content appended to \"element\" will be visible in the output area:\n", "element.appendChild(text);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "**Note:**\n", "\n", "jQuery should be available, but using the readthedocs.org default theme, it's not. See [the issue on Github](https://github.com/snide/sphinx_rtd_theme/issues/328).\n", "Other Sphinx themes are not affected by this.\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Unsupported Output Types\n", "\n", "If a code cell produces data with an unsupported MIME type, the Jupyter Notebook doesn't generate any output.\n", "`nbsphinx`, however, shows a warning message." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "display({\n", " 'text/x-python': 'print(\"Hello, world!\")',\n", " 'text/x-haskell': 'main = putStrLn \"Hello, world!\"',\n", "}, raw=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ANSI Colors\n", "\n", "The standard output and standard error streams may contain [ANSI escape sequences](https://en.wikipedia.org/wiki/ANSI_escape_code) to change the text and background colors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print('BEWARE: \\x1b[1;33;41mugly colors\\x1b[m!', file=sys.stderr, flush=True)\n", "print('ABC\\x1b[43mDEF\\x1b[35mGHI\\x1b[1mJKL\\x1b[49mMNO\\x1b[39mPQR\\x1b[22mSTU')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following code showing the 8 basic ANSI colors is based on http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html.\n", "Each of the 8 colors has an \"intense\" variation, which is used for bold text." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "text = ' XYZ '\n", "formatstring = '\\x1b[{}m' + text + '\\x1b[m'\n", "\n", "print(' ' * 6 + ' ' * len(text) +\n", " ''.join('{:^{}}'.format(bg, len(text)) for bg in range(40, 48)))\n", "for fg in range(30, 38):\n", " for bold in False, True:\n", " fg_code = ('1;' if bold else '') + str(fg)\n", " print(' {:>4} '.format(fg_code) + formatstring.format(fg_code) +\n", " ''.join(formatstring.format(fg_code + ';' + str(bg))\n", " for bg in range(40, 48)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ANSI also supports a set of 256 indexed colors.\n", "The following code showing all of them is based on http://bitmote.com/index.php?post/2012/11/19/Using-ANSI-Color-Codes-to-Colorize-Your-Bash-Prompt-on-Linux." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "formatstring = '\\x1b[38;5;{0};48;5;{0}mX\\x1b[1mX\\x1b[m'\n", "\n", "print(' + ' + ''.join('{:2}'.format(i) for i in range(36)))\n", "print(' 0 ' + ''.join(formatstring.format(i) for i in range(16)))\n", "for i in range(7):\n", " i = i * 36 + 16\n", " print('{:3} '.format(i) + ''.join(formatstring.format(i + j)\n", " for j in range(36) if i + j < 256))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can even use 24-bit RGB colors:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "start = 255, 0, 0\n", "end = 0, 0, 255\n", "length = 79\n", "out = []\n", "\n", "for i in range(length):\n", " rgb = [start[c] + int(i * (end[c] - start[c]) / length) for c in range(3)]\n", " out.append('\\x1b['\n", " '38;2;{rgb[2]};{rgb[1]};{rgb[0]};'\n", " '48;2;{rgb[0]};{rgb[1]};{rgb[2]}mX\\x1b[m'.format(rgb=rgb))\n", "print(''.join(out))" ] } ], "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.5.2+" } }, "nbformat": 4, "nbformat_minor": 1 }