PyCharm 2016.1 Help

Tutorial: Using IPython/Jupyter Notebook with PyCharm

In this section:

Before you start

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

Note that PyCharm automatically installs the dependencies of these packages.

Creating a IPython Notebook file

In the Project tool window, click Alt+Insert. Then, on the pop-up menu that appears, choose the option Jupyter Notebook and type the file name (here it is MatplotlibExample.ipynb).

The newly created file now shows up in the Project tool window and automatically opens for editing.

By now, the new file is empty, but PyCharm recognizes it as a notebook file. As such, this file is marked with the icon ipnb_icon and features a toolbar, which is a complete replica of the real IPython Notebook toolbar:

py_ipynb_first_cell

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:

%matplotlib inline

Next, click the icon run to run the 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:

py_ipynb_start

The console shows the server URL:

py_ipynb_console

Follow this address:

py_ipynb_browser

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

py_ipynb_settings

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

Working with cells

First of all, add the following import statement:

from pylab import *

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:

py_ipynb_empty

Start typing in this cell, and notice code completion:

py_ipynb_code_completion

Click run 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:

figure() plot(x, y, 'r') xlabel('x') ylabel('y') title('title') show()

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

py_ipynb_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 add (or press Alt+Insert).

In the created cell, type the following code that will define x and y variables:

x = linspace(0, 5, 10) y = x ** 2

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

py_ipynb_result

Clipboard operations with the cells

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

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

Try these buttons yourself.

Running and stopping kernels

As you've already learnt, the button run is used to execute a cell.

If calculation of a certain cell takes too much time, you can always stop it. To do that, click stop on the document toolbar.

Finally, you can rerun the kernel by clicking refresh on the document toolbar.

The messages about all these actions show up in the console:

py_ipynb_console_kernels_interrupt

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 then click add. The new cell appears below. By default, its style selector shows Code. In this cell, type the following text:

plot example

Next, click the down arrow, and choose Markdown from the list. The cell changes its view:

py_ipynb_choose_style

Now click run on the toolbar, and how the cell looks now:

py_ipynb_choose_style1

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

py_ipynb_choose_style2

Writing formulae

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

$$c = \sqrt{a^2 + b^2}$$

Click run. The result is stunning:

py_ipynb_formula

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):

from __future__ import division from IPython.display import display from sympy.interactive import printing printing.init_printing(use_latex='mathjax') import sympy as sym from sympy import * x, y, z = symbols("x y z") k, m, n = symbols("k m n", integer=True) f, g, h = map(Function, 'fgh')

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

Rational(3,2)*pi + exp(I*x) / (x**2 + y)

Click run and enjoy:

py_ipynb_formula1

See Also

Last modified: 20 April 2016