How to work with Jupyter Notebooks in PyCharm

How to work with Jupyter Notebooks in PyCharm
If you are an aspiring data scientist today or someone in the field of Computer Science in general, it is impossible for you to not be 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 who use Python. 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.”

Python Developers Survey 2018 Results

PyCharm – a Cross-Platform IDE for Python Developers

In order to get the most out of Python, especially in terms of data analysis, it is important to find an integrated development environment that offers the most in terms of editing code and visualizing results. PyCharm is an IDE developed by JetBrains, the brains behind big development tools like PhpStorm.

The primary component of PyCharm, its code editor, offers 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 which makes 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) have the ability to 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.

With the extensive support for integrating Jupyter Notebook in PyCharm, developers have found it a whole lot of easier to create, execute and debug source codes while examining their outputs simultaneously.

What features are included for Jupyter Notebooks in PyCharm?

PyCharm allows you to make changes to your source document in a number of ways:

  • Editing and making previews
  • Use notebook as source code with definitions in form of texts
  • Live previews along with debugging
  • Options for Auto-saving your code
  • Highlighting of all types of Error and syntax mistakes
  • Ability to add line comments
  • Ability to execute and preview results simultaneously
  • Allows using the dedicated Jupyter Notebook Debugger

Let’s you recognize .ipynb files with the icon

Jupyter Notebook in PyCharm

Jupyter Notebook’s powerful code writing and editing capabilities and PyCharm’s elite dedicated debugging module for Jupyter, code navigation, framework support, plugin support, and error detection combined can form a development environment which lacks little.

Now the question is 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 of how to integrate Jupyter Notebook with PyCharm is 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 next to it you will have the option to “Install jupyter package”. 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.
  4. To start exploring Jupyter Notebook in PyCharm, create code cells and execute them.
  5. To launch the Jupyter server, execute the code cell.The Jupyter server is then launched using 8888 port by default on the localhost. You can view these configurations 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”.
  6. 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.
  7. You can even set breakpoints at lines of code and then click on the Run icon, , and select “Debug Cell” (or alternatively 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: The “TODO” tab is where you can view TODO comments and easily navigate to them by double-clicking on them in the TODO tab. The “Jupyter” tab is the Jupyter Server log. The “Terminal” is the python terminal where you can write python commands. The “Python Console” is the console where you can view code and its output line by line.

Getting along with the User Interface

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

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 both 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 a number of shortcuts that provide quick access to all basic operations you are going to work with.

The Server Log

The Server log appears when you launch any of the Jupyter Servers. It shows the current state of the server and link to the notebook that you are currently working on.

The Variables Tab

This 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!

Related Posts
Leave a Reply

Your email address will not be published.Required fields are marked *