Interacting with the operating system

Any code with some degree of complexity will interact with the computer's operating system when files must be opened and closed, scripts must be run, or online data must be accessed. Plain Python already has a lot of tools to access these facilities, and IPython and the notebook add another level of functionality and convenience.

Saving the notebook

The notebook is autosaved in periodic intervals. The default interval is 2 minutes, but this can be changed in the configuration files or using the %autosave magic. For example, to change the autosave interval to 5 minutes, run the following command:

%autosave 300

Notice that the time is entered in seconds. To disable the autosave feature, run the following command:

%autosave 0

We can also save the notebook using the File menu or by clicking on the disk icon on the toolbar. This creates a checkpoint. Checkpoints are stored in a hidden folder and can be restored from the File menu. Notice that only the latest checkpoint is made available.

Notebooks are saved as plain text files with the .ipynb extension, using JSON. JSON is a format widely used for information exchange in web applications, and is documented in http://www.json.org/. This makes it easy to exchange notebooks with other people: simply give them the .ipynb file, and it can then be copied to the appropriate working directory. The next time the notebook server is opened in that directory, the new notebook will be available (or the directory list can be refreshed from the dashboard). Also, since JSON is in a plain text format, it can be easily versioned.

Converting the notebook to other formats

Notebooks can be converted to other formats using the nbconvert utility. This is a command-line utility. So, to use it, open a terminal window in the directory that contains your notebook files.

Tip

Windows users can press Shift and right-click on the name of the directory that contains the notebook files and then select Open command window here.

Open a shell window and enter the following line:

ipython nbconvert "Chapter 2.ipynb"

You must, of course, replace Chapter 2.ipynb with the name of the file that contains your notebook. The file name must be enclosed by quotes.

The preceding command will convert the notebook to a static HTML page that can be directly placed in a web server.

Note

An alternative way to publish notebooks on the Web is to use the site http://nbviewer.ipython.org/.

It is also possible to create a slideshow with nbconvert using the following command:

ipython nbconvert "Chapter 2.ipynb" --to slides

However, to get a decent presentation, we must first specify its structure in the notebook. To do so, go to the notebook and select Slide show on the Cell toolbar drop-down list. Then, determine for each cell if it should be a slide, a sub-slide, or a fragment.

To view the slide show, you need to install the reveal.js file in the same directory as the web page containing the presentation. You can download this file from https://github.com/hakimel/reveal.js. If necessary, rename the directory that contains all the files to reveal.js. We are then ready to open the HTML file that contains the presentation.

It is also possible to convert notebooks to LaTeX and PDF. However, this requires the installation of packages not included in Anaconda.

Running shell commands

We can run any shell command from the notebook by starting a cell with an exclamation (!) mark. For example, to obtain a directory listing in Windows, run the following command in a cell:

!dir

The equivalent command in Linux or OS X is the following:

!ls

You can enter command lines of any complexity in the cell. For example, the following line would compile the famous "Hello, world!" program every student of C has to try:

!cc hello.c –o hello

Of course, this will not run correctly in your computer unless you have the C compiler, cc, installed and the hello.c file with the proper code.

Instead of using shell commands directly, a lot of the same functionality is provided by magic commands. For example, a directory listing (in any operating system) is obtained by running the following command:

%ls

The following table shows a list of some of the most commonly used magics to interact with the system:

Magic

Purpose

%cd

Changes the directory

%pwd

Prints the current directory

%ls

Lists the current directory contents

%mkdir

Creates a new directory

%rmdir

Removes a directory

%echo

Prints a string

%alias

Creates an alias

The %echo magic is frequently used to print values of environment variables. For example, to print the contents of the PATH environment variable in Windows, run the following command:

%echo %PATH%

In Linux or OS X, use the following command:

%echo $PATH

The %alias magic creates an alias for frequently used commands. For example, to define a macro that displays the PATH value in Windows, execute the following command:

%alias show_path echo %PATH%

In Linux or OS X, use the following command:

%alias show_path echo $PATH

After the preceding command is defined, we can run the following command to display the path:

show_path

To make entering commands even easier, a feature called automagic allows line-oriented magics to be entered without the % symbol (as shown in the preceding command). For example, to create a directory, we can simply enter the following command:

mkdir my-directory

If we change our mind, we can remove the directory using the following command:

rmdir my-directory

The automagic feature is controlled by the %automagic magic. For example, use the following command to turn automagic off:

%automagic off
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset