{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# How to Use Custom Line/Cell Magic\n", "### Quick Introduction to Magic Commands\n", "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!**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%lsmagic" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "factor 1337\n", "apt-get moo" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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!**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%html\n", "
\n", "

Hello, world!

\n", "

Within this cell we can use most HTML tags including the script tag.

\n", "
\n", " \"Linux\n", " \n", " \"Linux\n", "
\n", "
\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Cusom Magic Commands\n", "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_](./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!**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "import uiButtons\n", "%uiButtons" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Python code\n", "print('hello, world')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adding Custom Magic Commands\n", "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``` )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%ls" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%cat uiButtons.py" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%helloWorld" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice in the example above we have added our own custom line magic \"_helloWorld_\". \n", "\n", "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](http://ipython.readthedocs.io/en/stable/interactive/magics.html)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }