• [PyCharm IDE]
 
Skip to end of metadata
Go to start of metadata

What this tutorial is about

This tutorial aims to walk you step by step through using PyCharm to create an IPython Notebook file and run its cells.

What this tutorial is not about

The details of IPython Notebook are out of scope of this tutorial. Refer to the product documentation.

Before you start...

Prior to executing the tasks of this tutorial, make sure that the following prerequisites are met:

  1. You have a Python project already created.
    In this tutorial the project C:/SampleProjects/py/IPythonNotebookExample is used.
  2. In the Project Interpreter page of the Settings/Preferences dialog, you have:
    1. created a virtual environment. For this tutorial a virtual environment based on Python.2.7.8 has been created.
    2. installed the following packages:
  • Jinja2
  • ipython
  • matplotlib
  • numpy
  • pyzmq
  • tornado
  • sympy
    Note that PyCharm automatically installs the dependencies of these packages.

    Creating a IPython Notebook file

In the Project tool window, right-click the project name and on the context menu choose New → File:

In the New File dialog box, enter the name of the new file and its extension MatplotlibExample.ipynb

The newly created file now shows up in the Project tool window:

Double-click this file to open it for editing. By now, the new file is empty, but PyCharm recognizes it as an IPython Notebook file. As such, this file is marked with the icon and features a toolbar, which is almost a complete replica of the real IPython Notebook toolbar:

Filling in and running the first cell

This is most easy. Just click the first cell and start typing. For example, in the very first cell type the following code to configure the matplotlib package:

Then click on the toolbar to run this cell (alternatively, you can press Shift+Enter). PyCharm shows a dialog box, where you have to specify the URL where the IPython Notebook server will run:

Click OK. PyCharm shows an information message:

You can follow this link and open URL in your browser:

Note that the default URL is specified in the IPython Notebook page of the Settings/Preferences dialog.

Actually, that's it... From now on you are ready to work with IPython Notebook integration.

Working with cells

First of all, add the following import statement:

This how it's done. As you might have noticed, while you've run the first cell. PyCharm has automatically created the next empty cell:

Start typing in this cell, and notice code completion:

Click on the toolbar again to run this cell. Note that the cell produces no output, but the next empty cell is created automatically.

In this new cell, enter the following code:

and run this cell. Oops! The attempt to run results in an error:

It seems that the variables should be defined first. To do that, add a new cell.

Adding

Since the new cell is added below the current one, click the cell with import statement - its frame becomes green. Then on the toolbar click (or press Alt+Enter):

Then type the following code, which will define x and y variables:

Run this cell, and then run the next cell. This time it shows the expected output:

Clipboard operations with the cells

Look at the toolbar. Besides and buttons, there are (Ctrl+X), (Ctrl+C) and (Ctrl+V) buttons. If you click , you thus delete the current cell, and take it into the clipboard.

Clicking results in inserting the contents of the clipboard below the current cell. Finally, just duplicates the current cell.

Try these buttons yourself.

Choosing style

Look at the drop-down list to the right of the document toolbar. It allows you to choose presentation style of a cell. For example, the existing cells are presented as code.

Click the cell with the import statement again, and click . The new cell appears below. By default, its style selector shows Code. In this cell, type the following text:

Click the down arrow and select Markdown from the list:

The cell changes its view:

Click on the toolbar, and see how the cell looks now:

Now you can just select the desired style from the drop-down list, and the view of the cell changes approriately:

Writing formulae

Add a new cell. In this cell, choose Markdown from the style selector, and type the following text:

Click . The result is stunning:

As you see, PyCharm's IPython Notebook integration makes it possible to use LaTex notation and render formulae, labels and text.

Next, explore the more complicated case. The expected result - the formula - should appear as the result of calculation.
Add a cell and type the following code (taken from SymPy: Open Source Symbolic Mathematics):

Run this cell. It gives no output. Next, add another cell and type the following:

Click , and enjoy:

Summary

This tutorial is over. Congrats! Now let's summarize what has been done:

  1. You've created a IPython Notebook file.
  2. Added cells
  3. Deleted, copied and pasted cells
  4. Run cells
  5. Selected presentation format of the cells
  6. Rendered formulae

Further reading

To learn more about IPython Notebook, refer to the product documentation, where you can find information about the user interface, workflow, keyboard shortcuts etc.
Refer to the PyCharm documentation (IPython Notebook Support) to learn about PyCharm's integration.

  • No labels