Google Colab - Magics

Magics is a set of system commands that provide a mini extensive command language.
Magics are of two types −
Line magics
Line magics
Cell magics
Cell magics
The line magics as the name indicates that it consists of a single line of command, while the cell magic covers the entire body of the code cell.
In case of line magics, the command is prepended with a single % character and in the case of cell magics, it is prepended with two % characters (%%).
Let us look into some examples of both to illustrate these.
Type the following code in your code cell −
%ldir

You will see the contents of your local directory, something like this -
drwxr-xr-x 3 root 4096 Jun 20 10:05 drive/
drwxr-xr-x 1 root 4096 May 31 16:17 sample_data/

Try the following command −
%history

This presents the complete history of commands that you have previously executed.
Type in the following code in your code cell −
%%html
<marquee style='width: 50%; color: Green;'>Welcome to Tutorialspoint!</marquee>

Now, if you run the code and you will see the scrolling welcome message on the screen as shown here −
The following code will add SVG to your document.
%%html
<svg xmlns="https://www.w3.org/2000/svg" viewBox="0 0 600 400" width="400" height="400">
   <rect x="10" y="00" width="300" height="100" rx="0" style="fill:orange; stroke:black; fill-opacity:1.0" />
   <rect x="10" y="100" width="300" height="100" rx="0" style="fill:white; stroke:black; fill-opacity:1.0;" />
   <rect x="10" y="200" width="300" height="100" rx="0" style="fill:green; stroke:black; fill-opacity:1.0;" />
</svg>

If you run the code, you would see the following output −
To get a complete list of supported magics, execute the following command −
%lsmagic

You will see the following output −
Available line magics:
%alias %alias_magic %autocall %automagic %autosave %bookmark %cat %cd %clear
%colors %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 %profile %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 %shell %store %sx %system %tb %tensorflow_version %time %timeit
%unalias %unload_ext %who %who_ls %whos %xdel %xmode

Available cell magics:
%%! %%HTML %%SVG %%bash %%bigquery %%capture %%debug %%file %%html %%javascript
%%js %%latex %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script
%%sh %%shell %%svg %%sx %%system %%time %%timeit %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.

Next, you will learn another powerful feature in Colab to set the program variables at runtime.

    20 Lectures 
     2.5 hours 

 Asif Hussain

    7 Lectures 
     1 hours 

 Aditya Kulkarni

    33 Lectures 
     2.5 hours 

 Sasha Miller

    22 Lectures 
     1.5 hours 

 Zach Miller

    16 Lectures 
     1.5 hours 

 Sasha Miller

    23 Lectures 
     2.5 hours 

 Sasha Miller
 Print
 Add Notes
 Bookmark this page