If you had the chance to work with Jupyter notebooks in PyCharm before 2019, you may remember that the features from PyCharm that supported Jupyter were subpar and left much to be desired. However, with the big update at the beginning of 2019, PyCharm has proven itself to be one of the best Python IDEs once again by completely revamping its support for Jupyter. In this section, we will go over these support features to see how integrated Jupyter is in PyCharm:
- First, we can create a new notebook inside the PyCharm editor by right-clicking on a folder inside the directory tree and choosing the New option, as follows:
- This will open the notebook file inside the editor, which means that you can run your notebooks from within PyCharm, as opposed to having to use a web browser as in the previous subsection. Moreover, if, at this point, you don't have Jupyter installed in your environment, PyCharm will also let you know as soon as the notebook is opened:
- With the notebook opened in the editor, we can see that editing Jupyter notebooks is similar to editing Markdown text—we can edit the code using the left panel of the editor, and the right panel will display the rendered output. The rendering of the notebook output is done in real time, so it will adjust as you enter code into your notebook.
- The beginning of each code cell in the notebook is indicated by the #%% symbol (there is already one in the blank notebook we created). The code cell lasts until there is another #%% symbol. If you want to specify a Markdown cell, you can use #%% md at the beginning of a given cell.
- Now, open the basics.ipynb file inside the notebooks folder of the current chapter's code directory in the editor in PyCharm. This file contains the Jupyter code for the notebook we considered in the previous subsection. Go ahead and copy it over the new notebook we just created. Your workspace will look similar to the following:
The first thing we notice is the Run buttons for the individual code cells. Markdown cells can be processed in real time, but as we mentioned earlier, code cells need to be run to produce their effects.
- To run a code cell, simply click on its Run button and choose the Run Cell option.
- This action will initiate a Jupyter server that handles the backend execution of our notebook. Additionally, a new Jupyter panel will appear (most likely at the bottom of your window), displaying information about the execution of the Jupyter server:
There are multiple tabs in this panel that you can navigate between using the section that's highlighted in the preceding screenshot. The Server Log tab is basically the Terminal when we use Jupyter outside of PyCharm. In other words, you can use this tab to access the server in an actual web browser (by clicking on the link that was printed in the preceding screenshot) or close the server with the Control + C shortcut.
The other Variables tab in this panel display information about the variables that are declared in their respective notebooks. As you execute the second code cell of our current notebook, you will see that the tab is populated in the same way as a regular Variables panel.
- As we execute the code cells in our notebook, the right-hand panel of the Jupyter editor in PyCharm also updates its display accordingly in real time. Specifically, we can see the visualizations inside this panel as the cells producing them are run.
We have gone through the main features of PyCharm in the context of Jupyter notebooks. In general, one of the biggest drawbacks of using traditional Jupyter notebooks is the lack of syntax formatting and code completion while writing code in individual code cells. Specifically, when we write code in Jupyter notebooks in our browser, the process is very similar to writing code in a simple text editor with limited support.
However, as we work with Jupyter notebooks directly inside the PyCharm editor, we will see that all the code-writing support features that are available to regular Python scripts are also available here. In other words, when using PyCharm to write Jupyter notebooks, we get the best of both worlds—powerful, intelligent support from PyCharm and an iterative development style from Jupyter.