PyCharm 2019.2 Help

Edit Jupyter notebook files

Set up your project

  1. In PyCharm, create a new Python project or open an existing one.

  2. Install the jupyter package for the selected interpreter.

  3. When all the indexing processes are finished, you are ready to start working with the notebook files.

To open an existing .ipynb file, follow the same steps as for the files of the other types. If needed, you can create a notebook file.

Create a notebook file

  1. Do one of the following:

    • Right-click the target directory in the Project tool window, and select New from the context menu.

    • Press Alt+Insert

  2. Select Jupyter Notebook.

  3. In the dialog that opens, type a filename.

A notebook document has the *.ipynb extension and is marked with the corresponding icon: Jupyter Notebook file icon.

Switch between the available editing modes by using the corresponding icons.

Edit source code in the notebook

You can add Markdown, raw, and code cells to the notebook file.

  1. Use one of the following ways to add a new cell:

    • Click the the Add button icon in the lowest line in the gutter. Select a cell type to add.

      Adding a new cell

    • Type the corresponding comment:

      Comment

      Cell type

      #%% md

      Markdown markup language.

      #%%

      Python code fragment

      #%% raw

      “Raw NBConvert” cell type.

    • For code cells, press Shift+Alt+A (Windows) or ⌥ ⇧ A (macOS) to insert a new cell above the currently selected cell, or Shift+Alt+B (Windows) or ⌥ ⇧ B (macOS) to create a new cell below the selected cell.

  2. Add appropriate content to the newly created cell. Once a Markdown or raw cell is added, its output appears in the Preview pane. To preview the results produced by a code cell you need to execute it first.

    Example of a Markdown fragment

You can edit code with the help of Python code insights, such as syntax highlighting, code completion, and so on.

Code completion for Jupyter notebooks

Use the Show sources in preview icon to control visibility of the source code preview.

Configure source editor

  1. Set up cell separators.

    By default, all cells are separated by lines. To alter this formatting, open project Settings/Preferences Ctrl+Alt+S, select Build, Execution, Deployment | Jupyter dialog, and deselect the Show Cell Line Separators in the Editor checkbox.

  2. Set up the number of virtual lines in code and Markdown cells.

    When you create new code or Markdown cells, PyCharm adds opening and closing virtual lines that are not part of the cell content, are not displayed in the Preview area, and are not saved in the notebook file. By default, PyCharm adds one line before the actual content and one line after it. To change this behavior, open Settings/Preferences Ctrl+Alt+S, select Build, Execution, Deployment | Jupyter dialog, and modify the value of the Number of Virtual Lines in Code Call Start/End input stepper.

Last modified: 6 November 2019