{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# A Short Introduction to Jupyter notebooks (using an IPython kernel)\n", "\n", "[back to main page](index.ipynb)\n", "\n", "This very page is a Jupyter notebook, so if you are reading this on a static web page, you should download the notebook file (which has the file extension `.ipynb`) and open it with Jupyter/IPython (see below for how that's done)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## IPython console, Qt console, Jupyter notebook\n", "\n", "The IPython (I guess that's short for *interactive* Python) kernel can be used in 3 quite different ways:\n", "\n", "* The **classic IPython console** runs in a terminal window (or on a real terminal, if those still exist) and is somewhat similar to the native interactive Python interpreter.\n", " However, it has many significant improvements like tab-completion, color output, \"magic\" functions, the ability to run shell commands, ... \n", " It can be started with\n", " \n", " ipython3\n", " \n", " Just give it a try!\n", " \n", " If you are really into text-mode applications, you might also want to check out\n", " [bpython](http://bpython-interpreter.org/) and [ptpython](https://github.com/jonathanslenders/ptpython).\n", "\n", "---\n", "\n", "* The **Qt console** uses the Qt GUI library but still keeps a terminal-like appearance.\n", " The main difference to the classic console is the support for inline graphics/plots and multi-line editing. \n", " It can be started with\n", "\n", " ipython3 qtconsole\n", " \n", " What are you waiting for? Go ahead and start it!\n", "\n", "---\n", "\n", "* The **Jupyter notebook** (formerly known as *IPython notebook*) brings your interactive Python session into the browser and opens up a cornucopia of multi-medial greatness (to say the least). \n", " Depending on your installation, the command for opening a notebook (like, for example, this very page) might be one of those:\n", "\n", " ipython3 notebook\n", " jupyter3\n", " jupyter3 notebook\n", " jupyter3-notebook\n", "\n", "The commands on your system may have the digit `3` in it (to distinguish between Python 3.x and 2.x) or not (if you have only Python 3 installed, anyway).\n", "\n", "If you don't know what's the deal with Python 2 vs. 3, just use Python 3!\n", "\n", "The rest of this page describes the Jupyter notebook, but some of the commands are also available for the two console applications." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Markdown cells, Code cells\n", "\n", "Text can be written in [Markdown](http://daringfireball.net/projects/markdown/).\n", "\n", "Support for $\\LaTeX$ equations: $a^2+b^2=c^2$ (using [MathJax](http://www.mathjax.org/))." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting Help" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## \"Magic\" Functions" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "%quickref" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`qtconsole` can also be started from a running notebook session:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "%qtconsole" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "range?" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "%whos?" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "%whos??" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Populating the interactive namespace from numpy and matplotlib\n" ] } ], "source": [ "%pylab inline" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# TODO: TikZ extension?" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "%timeit?\n", "\n", "# or:\n", "#%run -t" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "%debug?\n", "\n", "# or:\n", "#%run -d" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "%who?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also do crazy things like running shell commands. You can even pass Python names and use the shell output again in Python:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['index.ipynb',\n", " 'intro-jupyter.ipynb',\n", " 'intro-numpy.ipynb',\n", " 'intro-python.ipynb',\n", " 'noise.ipynb',\n", " 'simple-signals.ipynb',\n", " 'template.ipynb']" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "extension = 'ipynb'\n", "a =! ls *.$extension\n", "a" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Today is Setting Orange, the 67th day of Discord in the YOLD 3185\n" ] } ], "source": [ "d =! ddate\n", "print(d.n)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "some nonsense\n", "more nonsense\n" ] } ], "source": [ "%%bash\n", "tmp=$(tempfile).txt\n", "echo some text > $tmp\n", "echo more text >> $tmp\n", "sed -i s/text/nonsense/ $tmp\n", "cat $tmp\n", "rm $tmp" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... but it doesn't stop there, you can also run Ruby, Octave or R code!\n", "\n", "This is of course beyond the scope of this little tutorial, if you are interested, have a look at these commands:\n", "\n", " %%ruby\n", " %load_ext octavemagic\n", " %%octave\n", " %load_ext rmagic\n", " %%R\n", "\n", "See also [Running Scripts from IPython](http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/notebooks/Script%20Magics.ipynb)\n", "\n", "[Example notebook using Octave](http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/notebooks/Octave%20Magic.ipynb)\n", "\n", "[Example notebook using R](http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/notebooks/R%20Magics.ipynb)\n", "\n", "[Example notebook using Julia](http://nbviewer.ipython.org/github/JuliaLang/IJulia.jl/blob/master/python/doc/JuliaMagic.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Show all available \"magic\" functions:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "application/json": { "cell": { "!": "OSMagics", "HTML": "Other", "SVG": "Other", "bash": "Other", "capture": "ExecutionMagics", "debug": "ExecutionMagics", "file": "Other", "html": "DisplayMagics", "javascript": "DisplayMagics", "js": "DisplayMagics", "latex": "DisplayMagics", "markdown": "DisplayMagics", "perl": "Other", "prun": "ExecutionMagics", "pypy": "Other", "python": "Other", "python2": "Other", "python3": "Other", "ruby": "Other", "script": "ScriptMagics", "sh": "Other", "svg": "DisplayMagics", "sx": "OSMagics", "system": "OSMagics", "time": "ExecutionMagics", "timeit": "ExecutionMagics", "writefile": "OSMagics" }, "line": { "alias": "OSMagics", "alias_magic": "BasicMagics", "autoawait": "AsyncMagics", "autocall": "AutoMagics", "automagic": "AutoMagics", "autosave": "KernelMagics", "bookmark": "OSMagics", "cat": "Other", "cd": "OSMagics", "clear": "KernelMagics", "colors": "BasicMagics", "conda": "PackagingMagics", "config": "ConfigMagics", "connect_info": "KernelMagics", "cp": "Other", "debug": "ExecutionMagics", "dhist": "OSMagics", "dirs": "OSMagics", "doctest_mode": "BasicMagics", "ed": "Other", "edit": "KernelMagics", "env": "OSMagics", "gui": "BasicMagics", "hist": "Other", "history": "HistoryMagics", "killbgscripts": "ScriptMagics", "ldir": "Other", "less": "KernelMagics", "lf": "Other", "lk": "Other", "ll": "Other", "load": "CodeMagics", "load_ext": "ExtensionMagics", "loadpy": "CodeMagics", "logoff": "LoggingMagics", "logon": "LoggingMagics", "logstart": "LoggingMagics", "logstate": "LoggingMagics", "logstop": "LoggingMagics", "ls": "Other", "lsmagic": "BasicMagics", "lx": "Other", "macro": "ExecutionMagics", "magic": "BasicMagics", "man": "KernelMagics", "matplotlib": "PylabMagics", "mkdir": "Other", "more": "KernelMagics", "mv": "Other", "notebook": "BasicMagics", "page": "BasicMagics", "pastebin": "CodeMagics", "pdb": "ExecutionMagics", "pdef": "NamespaceMagics", "pdoc": "NamespaceMagics", "pfile": "NamespaceMagics", "pinfo": "NamespaceMagics", "pinfo2": "NamespaceMagics", "pip": "PackagingMagics", "popd": "OSMagics", "pprint": "BasicMagics", "precision": "BasicMagics", "prun": "ExecutionMagics", "psearch": "NamespaceMagics", "psource": "NamespaceMagics", "pushd": "OSMagics", "pwd": "OSMagics", "pycat": "OSMagics", "pylab": "PylabMagics", "qtconsole": "KernelMagics", "quickref": "BasicMagics", "recall": "HistoryMagics", "rehashx": "OSMagics", "reload_ext": "ExtensionMagics", "rep": "Other", "rerun": "HistoryMagics", "reset": "NamespaceMagics", "reset_selective": "NamespaceMagics", "rm": "Other", "rmdir": "Other", "run": "ExecutionMagics", "save": "CodeMagics", "sc": "OSMagics", "set_env": "OSMagics", "store": "StoreMagics", "sx": "OSMagics", "system": "OSMagics", "tb": "ExecutionMagics", "time": "ExecutionMagics", "timeit": "ExecutionMagics", "unalias": "OSMagics", "unload_ext": "ExtensionMagics", "who": "NamespaceMagics", "who_ls": "NamespaceMagics", "whos": "NamespaceMagics", "xdel": "NamespaceMagics", "xmode": "BasicMagics" } }, "text/plain": [ "Available line magics:\n", "%alias %alias_magic %autoawait %autocall %automagic %autosave %bookmark %cat %cd %clear %colors %conda %config %connect_info %cp %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %lf %lk %ll %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %lx %macro %magic %man %matplotlib %mkdir %more %mv %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %pip %popd %pprint %precision %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %rm %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode\n", "\n", "Available cell magics:\n", "%%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%js %%latex %%markdown %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile\n", "\n", "Automagic is ON, % prefix IS NOT needed for line magics." ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%lsmagic" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "IPython can display [all kinds of media](http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/notebooks/Part%205%20-%20Rich%20Display%20System.ipynb) and it can even handle the [HTML5 `