01_IPythonNotebooksUsage


Command mode vs Edit mode

By default we are in COMMAND mode

  • Press ENTER the edit the current cell
  • Press ESC to switch back to command mode
  • Main command mode shortcuts

    Notebook control:

    • 00 : Restart the notebook

    Cells control:

    • Up/Down arrows : move up-down on cells
    • a : add cell above
    • b : add cell below
    • x : delete current cell

    Editing cells:

    • Return : enter edit mode for current cell
    • Control+/ : Toggle code comment
    • Ctrl+Shift+- : Split cell at cursor position
    • Esc : return to command mode

    Executing cells:

    • Shift+Return : execute the content of the current cell

    More shortcuts listed under "Help" => "Keyboard shortcuts"

    Cells editing

    Cells have a type, which can be changed using shortcuts or the dedicated dropdown menu.
    This is an example of text cell, where you can use Markdown tags to format your text.

    You can also highlight chunks of code in almost any langauge

    Example of Bash script:

    #!/bin/bash
    # A useless script
    for i in $(seq 10); do
        echo  Hello World
    done
    

    Example of C fragment:

    29 /*
      28  * System energy normalization
      27  * Returns the normalized value, in the range [0..SCHED_LOAD_SCALE],
      26  * corresponding to the specified energy variation.
      25  */
      24 static inline int
      23 normalize_energy(int energy_diff)
      22 {
      21         u32 normalized_nrg;
      20         int max_delta;
      19 
      18 #ifdef CONFIG_SCHED_DEBUG
      17         /* Check for boundaries */
      16         max_delta  = schedtune_target_nrg.max_power;
      15         max_delta -= schedtune_target_nrg.min_power;
      14         WARN_ON(abs(energy_diff) >= max_delta);
      13 #endif
      12 
      11         /* Do scaling using positive numbers to increase the range */
      10         normalized_nrg = (energy_diff < 0) ? -energy_diff : energy_diff;
       9 
       8         /* Scale by energy magnitude */
       7         normalized_nrg <<= SCHED_LOAD_SHIFT;
       6 
       5         /* Normalize on max energy for target platform */
       4         normalized_nrg = reciprocal_divide(
       3                         normalized_nrg, schedtune_target_nrg.rdiv);
       2 
       1         return (energy_diff < 0) ? -normalized_nrg : normalized_nrg;
    5292 }
    

    Code flow vs execution flow

    Normally cells contains code, which is executed when Shift+Return is pressed

    
    
    In [28]:
    a = 1
    b = 2
    
    
    
    In [29]:
    def my_simple_sum(a, b):
        """Simple addition
        :param a: fist number
        :param b: second number
        """
        print "Sum is:", a+b
    
    
    
    In [33]:
    my_simple_sum(a,b)
    
    
    
    
    Sum is: 102
    
    
    
    In [32]:
    # Further down in the code we do some changes
    a = 100
    # than we can go back and re-execute just the previous cell
    

    Access to documentation and Code completion

    
    
    In [24]:
    # Use TAB to complete the function name
    # Use SHIFT+Tab after the '(' to access 
    my_simple_sum(2,3)
    
    
    
    
    Sum is: 5
    

    Local shell commands execution

    We can use a "!" at the beginning of a line to execute that command in a local shell

    
    
    In [34]:
    !pwd
    
    
    
    
    /home/derkling/Code/lisa/ipynb/tutorial
    
    
    
    In [36]:
    !date
    
    
    
    
    Fri Feb 26 17:52:47 CET 2016
    

    We can also use variables as parameters by passing them wrapped in "{}"

    
    
    In [57]:
    folder = "../"
    !ls -la {folder} | wc -l
    
    
    
    
    15
    

    Output of a local shell command can also be captured, for example to be post-processed in python

    
    
    In [68]:
    output = !find ../../ipynb/ -name "*.ipynb"
    
    print "Available notebooks:"
    for line in output:
        print line.replace('../../ipynb/', '   ')
    
    
    
    
    Available notebooks:
       sched_tune/stune_juno_taskonly_rampL.ipynb
       sched_tune/stune_oak_rampL.ipynb
       sched_tune/stune_juno_rampL.ipynb
       devlib/examples/cgroups.ipynb
       profiling/kernel_functions_profiling.ipynb
       trappy/example_custom_events.ipynb
       sched_dvfs/smoke_test.ipynb
       wlgen/simple_rtapp.ipynb
       wlgen/rtapp_examples.ipynb
       tutorial/01_IPythonNotebooksUsage.ipynb
       tutorial/.ipynb_checkpoints/01_IPythonNotebooksUsage-checkpoint.ipynb
       utils/testenv_example.ipynb
       utils/executor_example.ipynb
    

    How can be used?

    Exporting to scripts

    A notebook can be exported as a standalone script by via the "File" => "Download as" menu

    Generate reports by exporting to HTML

    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.