{ "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": null, "metadata": {}, "outputs": [], "source": [ "?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## \"Magic\" Functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%quickref" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`qtconsole` can also be started from a running notebook session:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%qtconsole" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "range?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%whos?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%whos??" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%pylab inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# TODO: TikZ extension?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%timeit?\n", "\n", "# or:\n", "#%run -t" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%debug?\n", "\n", "# or:\n", "#%run -d" ] }, { "cell_type": "code", "execution_count": null, "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": null, "metadata": {}, "outputs": [], "source": [ "extension = 'ipynb'\n", "a =! ls *.$extension\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d =! ddate\n", "print(d.n)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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": null, "metadata": {}, "outputs": [], "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 `