23rd Apr 2019 8 minutes read Jupyter Notebook – the Free Editor for Python Dorota Wdzięczna python learn Python python basics Jupyter If you have basic knowledge about Python and are looking for an IDE to work with on your own computer, consider the Jupyter Notebook. It is a good tool, especially for Data Science projects. What is Jupyter Notebook? Jupyter Notebook is an open-source server-client application used to create and run mainly Data Science projects. Where does its name come from? JUPYTER is an acronym of Julia, Python, and R, because they were the first programming languages supported by this editor. But now Jupyter supports over 40 programming languages. :-) In this editor you can create documents called notebooks. Jupyter Project provides one edition to run on a PC (you can run the application without access to the Internet but installation is required) and another edition to use without installation via your browser with Internet access. It is fantastic, because you can choose whichever edition is more convenient for you. Jupyter is a popular editor for Data Science, because it also provides tools for visualization, numerical simulation, and data cleaning. Besides coding, the document in Jupyter can contain rich text or media elements like pictures, therefore it is considered a good tool for projects that require analysis of data in real time and building interactive data science applications. It is very easy for beginners to use the editor, especially if you are learning Python. First you should know that this editor is composed of two components: kernels and the dashboard. What is a kernel in Jupyter? It is the program which executes documents, performs introspection-based completion of code, performs computations, and produces results. The default kernel in Jupyter is for the Python language. There are, however, kernels for many languages. The second component of Jupyter is the dashboard. It is used as a manager for kernels, the program for displaying notebooks and documents, operating on them, and navigating between files. A dashboard is shown just after launching Jupyter Notebook. But what is a notebook in Jupyter? Notebooks are documents produced by Jupyter containing code and rich text elements (pictures, links, and tables). How to Get Started with Jupyter Notebook? If you would like to try Jupyter, you can use the Try option by going to the website at https://try.jupyter.org and starting a project. You don't need to install any tools, because the software runs in your browser. This way it is very easy to get started, and it is a good solution for people who want to test if Jupyter is an appropriate editor for them. On this website you will see several sections, one of them is Try Jupyter with Python. Click it to use Python in Jupyter. The Jupyter contents will be displayed in your browser. How to Install Jupyter Notebook ? If you decide to install this environment on the computer, you can do it in two ways: you can install Anaconda and use it to call Jupyter, or you can install Python 3 with the pip tool. The second way is for more experienced users. Regardless of whether you will be installing the software on a Windows or a Linux operating system, the process after you run Anaconda will be the same. How to install Anaconda on Windows and Linux is described in the article about the Spyder IDE. After installing Anaconda and launching the Anaconda Navigator, you can run Jupyter Notebook by clicking the Launch button. How to Create and Run Python Scripts in Jupyter? In the web browser you will see the editor. In the File tab you can see all the folders and files on your computer. The application opens in the default web browser on your computer using the following address: https://localhost:8888/tree I created a jupyter folder on my computer, for gathering together all of my projects. If you go to your folder, you are ready to create your first Python script. On the right expand the New list. Choose Python3. You will see the editor where you write code. Look at the name of this file (Untitled). You can change it by clicking on this name and typing a new title. Click the Rename button to change the name of this file. Now write some code in the editor. For example: print('Hello world!') Click the Run button to run the script. In the browser in the previous tab you can find the list of your files. Notice that the extension .ipynb was added to the name of your script. If you would like to stop your script, go to the Running tab and click the Shutdown button. The result is that the name of your file will not be green. Green tells you that the notebook is running. If you want to perform an operation on your script, in the Files tab select your document. On top of the editor will be displayed a menu bar with options like Edit, View, Remove, Download, and others. Click View and go to the last tab with a list of notebooks. You will see the name of the notebook in green (it denotes that it is running) and the Shutdown button will be displayed in orange. The red icon allows you to delete the file from your folder. If you click the name of the file you can edit it. Features of Jupyter Notebook That Help You Write Code The file format for notebooks in Jupyter is .ipynb. It is a JSON file format that is used to describe the contents of a notebook. Each document consists of cells. Each cell contains text, a figure, or some other element of the document that can be described by text in JSON format. After opening the editor you can see a menu and toolbar in it. These allow you to open a new file, save it, edit it, etc. Below the menu and toolbar there is a set of cells where you can write code or text. You can start typing Python code directly into it. Jupyter allows you to execute code in one cell by clicking on the Run this cell button. You will see the result under the cell. The next empty code cell is created automatically. While typing code, the editor will automatically add closing parentheses, brackets, and quotation marks, which helps to write code faster. The syntax is color-coded so that your code is more readable. If you would like to add a new cell, you can click on the plus icon. The result after adding the new cell: You can change the cell type from Code to Markdown to include explanatory text in your notebook. Click in the new empty cell. Then choose Markdown from the drop-down list on the toolbar. As you notice, in this case the editor didn't add the closing parenthesis. The choice of what option is better depends on your particular needs. If you would like to run all the cells, click the Run button in the toolbar or select from the menu Cell → Run All. In the Code option the result is displayed under the code, in the Markdown option, the code is displayed as the formatted text. If you double click in the cell, you are able to edit your code again. As you see, it is also possible to execute cells below the current cell. To run a cell you can also press Ctrl+Enter. An easy way to add a new cell above or below the current cell is to click in the first cell and select from the menu Insert → Insert Cell Above (or Insert Cell Below). The new cell will be displayed. You can also do this by selecting Insert then typing A for above for B for below. You can scroll through your cells by pressing the arrow keys on the keyboard. If you need to delete a cell, click on the cell and then click on the scissors icon from the toolbar or select from the menu Edit → Cut or Edit → Delete Cells. To insert an image into your notebook, first make sure you are editing the cell in Markdown mode, then select from the menu Edit → Insert Image. Next, download the file from your computer, confirm it, and in the cell you will see a message similar to that below. Run your cell to see the result... You can save your notebook using a checkpoint. Select from the menu File → Save and Checkpoint. Both the notebook and the checkpoint will be updated. What is a checkpoint? It is the content of your notebook at a particular moment. If you don't save the file and something is wrong in the editor, you can retrieve your work by selecting from the menu File → Revert to Checkpoint. The Jupyter editor also provides an option to hide or display the header or toolbar. You choose from the menu View → Toggle Header or View → Toggle Toolbar. In the View menu you can also find on the list Toggle Line Numbers. This allows you to hide or display line numbers in the cells. You can work in the Terminal by choosing from the drop-down list New → Terminal. The Terminal will be displayed in your browser. Now you can write Python commands and write code. Of course Jupyter also displays plots as results. Summary There are even more features in the Jupyter Notebook that you will find useful. In this article, however, I described only some of them like adding images to the document, drawing plots from given data, using and operating on cells in the code, using the Code or Markdown modes, setting line numbering modes, and using the Terminal. You also found out how to install Jupyter Notebook, and how to create and run a Python script. Although this IDE can be used for writing different kinds of applications in Python, it is mainly used for Data Science projects. Therefore, you will find in it all the necessary tools and functions needed for this type of project. I think that this short guide to Jupyter Notebook will help you choose the best editor for writing Python scripts. Try it out today! Tags: python learn Python python basics Jupyter