{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Command mode vs Edit mode"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "By default we are in COMMAND mode\n",
    "<li>Press **ENTER** the edit the current cell\n",
    "<li>Press **ESC** to switch back to command mode"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Main command mode shortcuts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notebook control:\n",
    "- **00**             : Restart the notebook\n",
    "\n",
    "Cells control:\n",
    "- **Up/Down arrows** : move up-down on cells\n",
    "- **a**              : add cell above\n",
    "- **b**              : add cell below\n",
    "- **x**              : delete current cell\n",
    "\n",
    "Editing cells:\n",
    "- **Return**         : enter edit mode for current cell\n",
    "- **Control+/**      : Toggle code comment\n",
    "- **Ctrl+Shift+-**   : Split cell at cursor position\n",
    "- **Esc**            : return to command mode\n",
    "\n",
    "Executing cells:\n",
    "- **Shift+Return**   : execute the content of the current cell\n",
    "\n",
    "More shortcuts listed under *\"Help\" => \"Keyboard shortcuts\"*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Cells editing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Cells have a type, which can be changed using shortcuts or the dedicated dropdown menu.<br>\n",
    "This is an example of text cell, where you can use **Markdown** tags to format your text."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also highlight chunks of code in almost any langauge"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example of Bash script:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```shell\n",
    "#!/bin/bash\n",
    "# A useless script\n",
    "for i in $(seq 10); do\n",
    "    echo  Hello World\n",
    "done\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example of C fragment:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```c\n",
    "  29 /*\n",
    "  28  * System energy normalization\n",
    "  27  * Returns the normalized value, in the range [0..SCHED_LOAD_SCALE],\n",
    "  26  * corresponding to the specified energy variation.\n",
    "  25  */\n",
    "  24 static inline int\n",
    "  23 normalize_energy(int energy_diff)\n",
    "  22 {\n",
    "  21         u32 normalized_nrg;\n",
    "  20         int max_delta;\n",
    "  19 \n",
    "  18 #ifdef CONFIG_SCHED_DEBUG\n",
    "  17         /* Check for boundaries */\n",
    "  16         max_delta  = schedtune_target_nrg.max_power;\n",
    "  15         max_delta -= schedtune_target_nrg.min_power;\n",
    "  14         WARN_ON(abs(energy_diff) >= max_delta);\n",
    "  13 #endif\n",
    "  12 \n",
    "  11         /* Do scaling using positive numbers to increase the range */\n",
    "  10         normalized_nrg = (energy_diff < 0) ? -energy_diff : energy_diff;\n",
    "   9 \n",
    "   8         /* Scale by energy magnitude */\n",
    "   7         normalized_nrg <<= SCHED_LOAD_SHIFT;\n",
    "   6 \n",
    "   5         /* Normalize on max energy for target platform */\n",
    "   4         normalized_nrg = reciprocal_divide(\n",
    "   3                         normalized_nrg, schedtune_target_nrg.rdiv);\n",
    "   2 \n",
    "   1         return (energy_diff < 0) ? -normalized_nrg : normalized_nrg;\n",
    "5292 }\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Code flow vs execution flow"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Normally cells contains code, which is executed when **Shift+Return** is pressed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "a = 1\n",
    "b = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def my_simple_sum(a, b):\n",
    "    \"\"\"Simple addition\n",
    "    :param a: fist number\n",
    "    :param b: second number\n",
    "    \"\"\"\n",
    "    print \"Sum is:\", a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sum is: 102\n"
     ]
    }
   ],
   "source": [
    "my_simple_sum(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Further down in the code we do some changes\n",
    "a = 100\n",
    "# than we can go back and re-execute just the previous cell"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Access to documentation and Code completion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sum is: 5\n"
     ]
    }
   ],
   "source": [
    "# Use TAB to complete the function name\n",
    "# Use SHIFT+Tab after the '(' to access \n",
    "my_simple_sum(2,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Local shell commands execution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can use a \"!\" at the beginning of a line to execute that command in a local shell"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/derkling/Code/lisa/ipynb/tutorial\r\n"
     ]
    }
   ],
   "source": [
    "!pwd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fri Feb 26 17:52:47 CET 2016\r\n"
     ]
    }
   ],
   "source": [
    "!date"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also use variables as parameters by passing them wrapped in \"{}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\r\n"
     ]
    }
   ],
   "source": [
    "folder = \"../\"\n",
    "!ls -la {folder} | wc -l"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Output of a local shell command can also be captured, for example to be post-processed in python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Available notebooks:\n",
      "   sched_tune/stune_juno_taskonly_rampL.ipynb\n",
      "   sched_tune/stune_oak_rampL.ipynb\n",
      "   sched_tune/stune_juno_rampL.ipynb\n",
      "   devlib/examples/cgroups.ipynb\n",
      "   profiling/kernel_functions_profiling.ipynb\n",
      "   trappy/example_custom_events.ipynb\n",
      "   sched_dvfs/smoke_test.ipynb\n",
      "   wlgen/simple_rtapp.ipynb\n",
      "   wlgen/rtapp_examples.ipynb\n",
      "   tutorial/01_IPythonNotebooksUsage.ipynb\n",
      "   tutorial/.ipynb_checkpoints/01_IPythonNotebooksUsage-checkpoint.ipynb\n",
      "   utils/testenv_example.ipynb\n",
      "   utils/executor_example.ipynb\n"
     ]
    }
   ],
   "source": [
    "output = !find ../../ipynb/ -name \"*.ipynb\"\n",
    "\n",
    "print \"Available notebooks:\"\n",
    "for line in output:\n",
    "    print line.replace('../../ipynb/', '   ')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# How can be used?\n",
    "\n",
    "## Exporting to scripts\n",
    "A notebook can be exported as a standalone script by via the \"File\" => \"Download as\" menu\n",
    "\n",
    "## Generate reports by exporting to HTML\n",
    "By mixing code and markdown formatted comments it's quite easy to generate an HTML report which can be exported via the \"File\" => \"Download As\" menu. PDF format is also supported but it requires some configuration of the backend.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Useful links:\n",
    "\n",
    "- Complete tutorial on Notebooks: http://nbviewer.jupyter.org/github/ipython/ipython/blob/3.x/examples/Notebook/Index.ipynb\n",
    "- Markdown syntax: http://nbviewer.jupyter.org/github/ipython/ipython/blob/3.x/examples/Notebook/Working%20With%20Markdown%20Cells.ipynb"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}