IPython offers special functions called Magic commands. These commands allow the user to quickly use special features within a Jupyter cell. To call these commands we use %
for a single line magic or %%
for an entire cell magic, for example using %lsmagic
will show all the available built in magic functions. try it now!
%lsmagic
We can even use magics to run bash commands right from the notebook, no need to open a terminal. For example using %ls
to list all segments in the present working directory, or %cd
to change working directory. Another option is to use the entire cell magic %%bash
to run multiple commands within one cell. See the example below where we use a %%bash
cell. Once we turn the cell into a bash cell we can use the factor
command or the apt-get
command for example.
%%bash
factor 1337
apt-get moo
Another very useful magic command is the %%html
cell magic. This magic command turns an entire code cell into a fully functioning html cell. Check it out!
%%html
<div>
<h1>Hello, world!</h1>
<p>Within this cell we can use most HTML tags including the script tag.</p>
<div>
<img src="http://res.publicdomainfiles.com/pdf_view/84/13939501228326.png" alt="Linux Magician" style="width: 100px; float: left" id="owl_img"/>
<img src="../assets/textBbl.png" style="float:left; display: none;" id="textBubble"/>
<img src="http://res.publicdomainfiles.com/pdf_view/84/13939501228326.png" alt="Linux Magician" style="width: 100px; display: none;" id="duplicate"/>
</div>
</div>
<script>
document.getElementById('owl_img').onmouseover = function() {
document.getElementById('textBubble').style.display = 'block';
document.getElementById('duplicate').style.display = 'block';
}
</script>
One very powerful aspect to magic commands is the ability to create our own custom magic commands. In this example we will see how easily we can import and use a custom magic command we have created. The first thing to keep in mind is that we need to keep the [filename.py] file within the same directory as the Jupyter notebook that will be importing it. Once this is done we can simply use the import [filename.py]
then use any of the custom magic commands included within that file. For this example we have a custom magic file in our current directory called "uiButtons.py", within this file we have a line magic command also called uiButtons. After we have imported the file we can simply call %uiButtons
to use our custom magic. Try it in the next cell!
import uiButtons
%uiButtons
Now you see that our %uiButtons
line magic adds two buttons, one to toggle showing/hiding the code cells and the other to initialize (run) all remaining code cells within one click.
#Python code
print('hello, world')
Now that we have a file which we can import, it is easy to add new line or cell magic commands. To do this we need to open the .py file, in our case the "uiButtons.py". Within this file there is a main class defined for all the magics. To make new ones simply define them within the class definition. See example below. (Good use case for %cat
)
%ls
%cat uiButtons.py
%helloWorld
Notice in the example above we have added our own custom line magic "helloWorld".
Running %lsmagic
shows many different options for line and cell magics, to find out more about what these built in functions do or how to use them click here