Data Science

How to work with Jupyter Notebooks in PyCharm

If you are someone in the field of Computer Science, chances are you’re a little familiar with Python. As this high-level, general-purpose programming language is rising in popularity, its strengths and impact are becoming more and more prominent. New developers want to delve into data analytics possible with Python’s elite data visualization and analysis tools.

Python is Significant in the World of Programming

According to a survey done by JetBrains, “Python is the primary language used by 84% of programmers. Furthermore, almost 58% of developers use Python for data analysis, while 52% use it for web development. The use of Python for DevOps, machine learning, and web crawling or web scraping follow close behind along with a multitude of other uses.”

PyCharm – a Cross-Platform IDE for Python Developers

To get the most out of Python, especially in terms of data analysis, it is important to find an IDE that offers the most in terms of editing code and visualizing results. For this purpose, PyCharm is the way to go. PyCharm is an IDE developed by JetBrains, the brains behind big development tools like PhpStorm.

The primary component of PyCharm is the code editor, which offers features like an intelligent context-based auto-completion of code, code suggestions, and code snippets. It allows programmers to create logical code blocks to separate program modules.

The editor is efficient in identifying and highlighting errors as code is written. Code navigation has never been easier as PyCharm allows programmers to quickly jump to a particular snippet, object, or class in the source code.

PyCharm also has tons of refactoring features, making it easy for developers to make organized changes. Support for web technologies like HTML, CSS, JavaScript, and more combined with PyCharm’s live edit and view webpage environment makes it a powerful tool for web development in Python.

“Literate Programming” with Jupyter Notebook

Another IDE that comes into play when talking about Python is Jupyter Notebook. Formerly known as IPython Notebook, Jupyter Notebook is especially important in giving shape to what Donald Knuth, a computer scientist from Stanford, famously called “literate programming”.

Literate programming is a standard form of programming that focuses on the human readability of code. It allows programmers to give shape to the logical units of their code, the meaning of those code units, and their results. Compiled, a notebook presents code as a complete and understandable thought process and its technological manifestation.

To support literate programming, Jupyter Notebook has a multitude of tools available which provide complete liberty to edit code with its relevant supporting prose.

Starting at the basic level, notebooks (the files in which code is written) can separate code into “cells”. Cells make it easy to differentiate between specific functionality.

Apart from code cells, there are markup cells available where it is easy to type code descriptions, significance, or results. Editing options for markup cells are endless; you can play around with text formats, images, and even mathematical equations and diagrams.

The extensive support for integrating the Jupyter Notebook in PyCharm allows developers to create, execute, and debug source codes while simultaneously examining their outputs.

What features are included for Jupyter Notebooks in PyCharm?

PyCharm allows you to make changes to your source document in many ways. These include:

  • Editing and making previews.
  • Using the notebook as source code with definitions in the form of texts.
  • Providing live previews along with debugging.
  • Options for Auto-saving your code.
  • Highlighting all types of syntax mistakes and errors.
  • The ability to add line comments.
  • The ability to execute and preview results simultaneously.
  • Permissions to use the dedicated Jupyter Notebook Debugger.
  • Recognize .ipynb files with the icon.

Using the Jupyter Notebook in PyCharm

Jupyter Notebook’s powerful code writing and editing capabilities and PyCharm’s elite dedicated debugging module can form a development environment that lacks little.

All that’s left is to learn how to achieve an integrated development environment that combines the functionalities of PyCharm and Jupyter Notebook.

The short answer is that this is currently only possible with a licensed version of PyCharm Professional. PyCharm Professional is not free. However, you can get a free license if you are affiliated with an educational institute and have a .edu email address.

The long answer to the aforementioned question is to follow the steps given below:

1. First, you should create a new project.
2. In that project, create a new ipynb file by going to File > New… > Jupyter Notebook. This should open up a new notebook file.
3. If you don’t have the Jupyter Notebook package installed, an error will appear above the newly opened ipynb file. The error reads “Jupyter package is not installed” and you will have the option to “Install jupyter package” next to it.

4. Click on “Install jupyter package”. This will start the installation process, which you can view by clicking on the running processes in the bottom right corner of the PyCharm window.

5. To start exploring Jupyter Notebook in PyCharm, create code cells and execute them.

6. Execute the code cell to launch the Jupyter server. By default, the Jupyter server uses the 8888 port by default on the localhost. These configurations are available in the server’s tool window. Once launched, you can view the server above your source code window, and next to it, you can view the kernel created as “Python 2” or “Python 3”.
7. You can now access the variables tab in PyCharm to view how the values of your variables change as you execute code cells. This helps in debugging. You can also set breakpoints at lines of code and then click on the Run icon and select “Debug Cell” (or use the shortcut Alt + Shift + Enter) to begin debugging.
8. The following tabs at the bottom of the PyCharm window are essential to using Jupyter Notebook:

Getting along with the User Interface

Out of the many user interface components, let us begin exploring the ones you can work with without facing any difficulties.

Viewing Modes

PyCharm offers three viewing modes to edit your Jupyter notebook files:

1. Editor Only Mode
This allows adding and editing notebook cells.

2. Split View Mode
The split view mode lets you add cells and preview their output. This is also the default-viewing mode for all Jupyter notebooks in PyCharm.

3. Preview Only Mode
Here you can preview your code execution results, raw cells, and code markdown.

Toolbar

The toolbar provides several shortcuts that provide quick access to all basic operations.

The Server Log

The Server log is a window that appears when you launch any of the Jupyter Servers. It shows the current state of the server and the link to the notebook that is being worked on.

The Variables Tab

The variables tab provides a detailed report on the variable values present in the executed cell.

Now that you are familiar with the basics of editing and debugging Jupyter Notebooks in PyCharm, you can go ahead and install the Jupyter package in PyCharm for yourself. From here on, you can fully explore its features and use them to your satisfaction!

Conclusion

This concludes our guide on how to work with Jupyter Notebooks in PyCharm. We covered the reasons to learn Python, along with brief introductions about PyCharm and Jupyter. We then covered the steps to integrate Jupyter with PyCharm and different modes and toolbars that can aid the developer experience. With this, we hope this guide aids you in your journey to become a Python developer.

About the author

Zeeman Memon

Hi there! I'm a Software Engineer by degree, Blogger by skills who loves to write about tech, develop websites & do SEO. You can reach out to me on LinkedIn.