- Google Colab - Discussion
- Google Colab - Useful Resources
- Google Colab - Quick Guide
- Google Colab - Conclusion
- Google Colab - Using Free GPU
- Google Colab - Installing ML Libraries
- Google Colab - Adding Forms
- Google Colab - Magics
- Google Colab - Code Editing Help
- Google Colab - Graphical Outputs
- Executing External Python Files
- Invoking System Commands
- Google Colab - Sharing Notebook
- Google Colab - Saving Your Work
- Documenting Your Code
- Your First Colab Notebook
- What is Google Colab?
- Google Colab - Introduction
- Google Colab - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Google Colab - Magics
Magics is a set of system commands that provide a mini extensive command language.
Magics are of two types −
Line magics
Cell magics
The pne magics as the name indicates that it consists of a single pne of command, while the cell magic covers the entire body of the code cell.
In case of pne 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.
Line Magics
Type the following code in your code cell −
%ldir
You will see the contents of your local directory, something pke 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.
Cell Magics
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 scrolpng welcome message on the screen as shown here −
The following code will add SVG to your document.
%%html <svg xmlns=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 −
Magics List
To get a complete pst of supported magics, execute the following command −
%lsmagic
You will see the following output −
Available pne magics: %apas %apas_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 %matplotpb %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 %unapas %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 pne magics.
Next, you will learn another powerful feature in Colab to set the program variables at runtime.
Advertisements