How to run jupiter notebook

How to run jupiter notebook

Getting Started With Jupyter Notebook for Python

In the following tutorial you will be guided through the process of installing Jupyter Notebook. Furthermore we’ll explore the basic functionality of Jupyter Notebook and you’ll be able to try out first examples.

This is at the same time the beginning of a series of Python-related tutorial on CodingTheSmartWay.com. From the very beginning you’ll learn everything to need to know to use Python for scientific computing and machine learning use cases.

Jupyter Notebook is a web application that allows you to create and share documents that contain:

Jupyter Notebook is great for the following use cases:

Let’s get started and install Jupyter Notebook on your computer …

Setting Up Jupyter Notebook

The first step to get started is to visit the project’s website at http://www.jupyter.org:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

Here you’ll find two options:

With the first option Try it in your browser you can access a hosted version of Jupyter Notebook. This will get you direct access without needing to install it on your computer.

The second option Install the Notebook will take you to another page which gives you detailed instruction for the installation. There are two different ways:

Especially if you’re new to Python and would like to set up your development environment from scratch using the Anaconda distribution is a great choice. If you follow the link (https://www.anaconda.com/download/) to the Anaconda download page you can choose between installers for Windows, macOS, and Linux:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

Download and execute the installer of your choice. Having installed the Anaconda distribution we can now start Jupyter Notebook by using the following command:

You’ll see the following response on the command line:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

The web server is started and the Jupyter Notebook application is opened in your default browser automatically. You should be able to see a browser output which is similar to the following screenshot:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

As you can see the user interface of Jupyter Notebook is split up into three sections (tabs):

The default view is the Files tab from where you can open or create notebooks.

Creating A New Notebook

Creating a new Jupyter Notebook is easy. Just use the New dropdown menu and you’ll see the following options:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

Select option Python 3 to open a new Jupyter Notebook for Python. The notebook is created and you should be able to see something similar to:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

Switching back to the Files tab you’ll be able to see a new file notebook01.ipynb:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

Because this notebook file is opened right now the file is marked with status Running. From here you can decided to shutdown this notebook by clicking on button Shutdown.

However before shutting down the notebook let’s switch back to the notebook view and try out a few things to get familiar with the notebook concept.

Working With The Notebook

The notebook itself consists of cells. A first empty cell is already available after having created the new notebook:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

This cell is of type “Code” and you can start typing in Python code directly. Executing code in this cell can be done by either clicking on the run cell button or hitting Shift + Return keys:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

The resulting output becomes visible right underneath the cell.

The next empty code cell is created automatically and you can continue to add further code to that cell. Just another example:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

You can change the cell type from Code to Markdown to include explanatory text in your notebook. To change the type you can use the dropdown input control:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

Once switched the type to Markdown you can start typing in markdown code:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

After having entered the markdown code you can compile the cell by hitting Shift + Return once again. The markdown editor cell is then replaced with the output:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

If you want to change the markdown code again you can simply click into the compiled result and the editor mode opens again.

Edit And Command Mode

If a cell is active two modes distinguished:

If you just click in one cell the cell is opened in command mode which is indicated by a blue border on the left:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

The edit mode is entered if you click into the code area of that cell. This mode is indicated by a green border on the left side of the cell:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

If you’d like to leave edit mode and return to command mode again you just need to hit ESC.

To get an overview of functions which are available in command and in edit mode you can open up the overview of key shortcuts by using menu entry Help → Keyboard Shortcuts:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

Checkpoints

Another cool function of Jupyter Notebook is the ability to create checkpoint. By creating a checkpoint you’re storing the current state of the notebook so that you can later on go back to this checkpoint and revert changes which have been made to the notebook in the meantime.

To create a new checkpoint for your notebook select menu item Save and Checkpoint from the File menu. The checkpoint is created and the notebook file is saved. If you want to go back to that checkpoint at a later point in time you need to select the corresponding checkpoint entry from menu File → Revert to Checkpoint.

Exporting The Notebook

Jupyter Notebook gives you several options to export your notebook. Those options can be found in menu File → Download as:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

What’s Next?

Now that you have a sound understanding of Jupyter Notebook you can start creating your own notebook and use it for your next Python project. Jupyter Notebook is perfect for using Python for scientific computing and data analysis with libraries like numpy, pandas, and matplotlib.

However, if you’re totally new to Python, just wait for our next tutorial in which you’ll learn Python basics by using Jupyter Notebook from the very beginning. Stay tuned …

Want to dive deeper into Python for Data Science and Machine Learning. Check out the following online course:

#1 Online Course: Python for Data Science and Machine Learning Bootcamp

Disclaimer: This post contains affiliate links, which means that if you click on one of the product links, I’ll receive a small commission. This helps support this blog!

Как настроить Jupyter Notebook для Python 3

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

Jupyter Notebook — это командная оболочка для интерактивных вычислений. Этот инструмент может использоваться не только с Python, но и другими языками программирования: Julia, R, Haskell и Ruby. Он часто используется для работы с данными, статистическим моделированием и машинным обучением.

В статье мы рассмотрим, как настроить Jupyter Notebook для локального запуска или запуска на сервере под управлением Ubuntu 16.04. Этот инструмент поможет создавать файлы (notebooks), которые содержат не только компьютерный код, но и другие элементы (заметки, уравнения, диаграммы, ссылки и т.д.), которыми можно потом поделиться с заказчиками или друзьями.

После прочтения этой статьи вы будете знать, как запускать код Python 3 при помощи Jupyter Notebook локально или на удалённом сервере.

Требования

Нам понадобится среда программирования для Python 3, установленная либо на локальной машине, либо на сервере Ubuntu 16.04.

Шаг 1. Установка Jupyter Notebook

Теперь мы готовы создать виртуальное окружение. Выбираем каталог, в который все будет установлено, или создаем новый каталог с mkdir :

Как только вы переместитесь в нужную вам директорию, введите следующую команду в терминал:

По сути, pyvenv создает новый каталог, содержащий несколько элементов, которые мы можем просмотреть с помощью команды ls :

Для активации виртуального окружения вам осталось ввести последнюю команду:

Затем нужно убедиться, что pip был обновлен до последней версии:

Теперь можно установить Jupyter Notebook следующей командой:

После этой строчки кода Jupyter Notebook будет установлен в активное виртуальное окружение.

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

Следующий шаг предназначен для тех, кто хочет подключиться к веб-интерфейсу, используя SSH-туннелирование.

Шаг 2 (необязательный). Запуск Jupiter Notebook на сервере

Для тех, кто установил Jupyter Notebook на удаленный сервер, нужно подключаться к веб-интерфейсу через SSH-туннель. Приложение использует порт 8888 (или же 8889 ), а SSH-туннель поможет обезопасить соединение с сервером.

SSH-туннелирование с помощью Mac или Linux

Для тех, кто работает с macOS или Linux, нужно выполнить следующую команду в окне терминала:

Команда ssh создаст SSH-подключение, а флаг –L перенаправит порт локального или клиентского хоста на хост и порт уделенного сервера. То есть все, что работает на порте 8888 с серверной стороны, будет работать на порте 8888 вашей локальной машины.

При необходимости можно изменить порт 8888 на один из ваших вариантов, чтобы избежать использования порта, который уже используется другим процессом:

Например, для имени пользователя sammy и адреса сервера 203.0.113.0 команда будет следующей:

После этого, вы получите результат содержащий URL-адрес. Введите его в окне браузера и перейдите к веб-интерфейсу Jupyter Notebook ( http://localhost:8888 ).

SSH-туннелирование с помощью Windows и Putty

Пользователи Windows могут создать туннель SSH, используя программу Putty.

Сначала нужно ввести URL-адрес сервера или IP-адрес имени хоста, как показано ниже на скриншоте:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

Теперь нажмите кнопку Add (добавить), и порты должны появиться в списке Forwarded ports (Переадресованные порты):

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

И после этого переходим по адресу http://localhost:8000 (или любым другим портом, который вы выбрали) в веб-браузере, чтобы подключиться к Jupyter Notebook, работающему на сервере.

Шаг 3. Запуск Jupyter Notebook локально

После окончания установки Jupyter Notebook можно запустить при помощи следующей команды в терминале:

Если вы используете Jupyter Notebook на локальном компьютере, а не на сервере, то при открытии веб-приложения Jupyter Notebook должен открыться ваш браузер по умолчанию. Если этого не произошло, можно открыть браузер вручную и перейти к http://localhost: 8888 для подключения.

Вы получите следующий результат:

Jupyter Notebook остановлен.

Шаг 4. Использование Jupyter Notebook

Пришло время начать использование Jupyter Notebook.

Теперь можно подключиться к нему с помощью веб-браузера. Jupyter Notebook — очень мощный инструмент и имеет множество функций. В этом разделе описываются некоторые основные функции, позволяющие начать работу с ним. Jupyter Notebook покажет все файлы и папки в каталоге, из которого он запущен, поэтому, когда начинается работа над проектом, обязательно нужно запускать его из каталога проекта.

Чтобы создать документ выбираем New → Python 3 в верхнем выпадающем меню:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

Чтобы переформатировать Markdown в форматированный текст, нажимаем CTRL + ENTER и получаем следующие результаты:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

Также можно использовать ячейки Markdown для написания кода. Для теста напишем небольшое уравнение и выведем результат. Нажимаем на верхнюю ячейку, нажимаем ALT+ENTER для создания ячейки под ней и вводим следующий код в новую ячейку:

Чтобы запустить код, нажимаем CTRL + ENTER и получаем следующие результаты:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

Теперь у вас есть возможность импортировать модули и использовать Jupyter Notebook так же, как и в любой другой среде разработки Python!

Вывод

Поздравляем! Теперь вы можете написать воспроизводимый код Python и заметки в Markdown с помощью Jupyter Notebook. Для получения справки по использованию Jupyter Notebook нажмите Help → User Interface Tour в главном меню навигации.

Getting started with Jupyter Notebooks

This post is for those who want to get started with Jupyter Notebooks. You don’t need to know anything beyond Python to start using Jupyter notebook. In this post I will be covering below topics.

What is a Jupyter Notebook?

Jupyter Notebook is a web application where you can create and share documents that contain source code, equations, visualizations as well as text. It is an ideal tool to help you to gain the data science skills you need. “Jupyter” is a loose acronym meaning Julia, Python and R. These programming languages were the first target languages of Jupyter application.

Jupyter Notebook App

It is a server-client application, which allows you to edit and run notebooks via a web browser. Its two main components are the kernel and a dashboard. A kernel is a program that runs and examines the code. The dashboard of the UI application which shows you the notebook documents that you have created and is used to manage the kernels.

How to install Jupyter Notebook

Running Jupyter Notebooks with the Anaconda Python Distribution:- One of the basic requirement for running Jupyter notebook is Python(either Python 3.3 or greater or Python 2.7). The general recommendation is that you use the Anaconda distribution to install both Python and the notebook application. The advantage of Anaconda is that you have access to over 720 packages that can easily be installed with Anaconda’s Conda, a package, dependency, and environment manager. You can download and follow the instructions for the installation of Anaconda here.

Running Jupyter Notebook using Pip :- If you don’t want to install Anaconda, you just have to make sure that you have the latest version of pip. Once you have pip, you can just run

# Python2 pip install jupyter # Python 3 pip3 install jupyter

Running Jupyter Notebooks in Docker Containers:- Docker is an excellent platform to run software in containers. To run the Jupyter Notebook image in your Docker container, give in the below command in your Docker Quickstart Terminal

The newest Jupyter HTML Notebook image will be downloaded and it will be started.

How to use Jupyter Notebook

To launch a Jupyter Notebook application run the following command in the command prompt from the working directory.

This will start the Jupyter server and open a new window in your browser (if that’s not the case, go to URL: http://localhost:8888). Below is the screenshot of Jupyter application.

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook Jupyter application

For closing the notebook server, go to the command prompt where you launched the server from, and press Ctrl+C.

The notebook dashboard consists of several tabs.

A notebook is a file and Kernel is a process. The kernel receives snippets of code from the Notebook interface, executes them, and sends the outputs and possible errors back to the Notebook interface.

Jupyter notebook user interface

To start a new notebook, Click on the New drop down in the Files tab. You see the options make a regular text file, a folder and a terminal. Lastly, you will see an option to make a Python 3 notebook. This option will depend on the version of python that you have installed. If you want to start on a new notebook, click on the “Python3” option in the ‘Notebook’ category.

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook Sample Jupyter User Interface

You can import libraries that you need, you can add, remove and edit the cells according to your needs. You can also add explanatory text, titles and subtitles to explain the code.

Share your Jupyter Notebooks

Sometimes you may want to share your notebook with colleagues or friends. Notebook documents are JSON documents that contain text, source code, rich media output and metadata. Each segment of the document is stored in a cell.

There are different ways to share notebook document with others:

Sharing notebooks via GitHub Gist and nbviewer

The below example notebook gender_wise_marks.ipynb has been created with a sample code for matplotlib usage. Below are the steps to share this notebook to Github Gist.

You can view the notebook now in nbviewer here

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook Jupyter nbviewer page

How to use a shared Jupyter Notebook

Loading Python Files – You can also load a pre-existing Python file into a Jupyter Notebook cell by simply typing

This loads up a new cell containing the contents of sample.py. There is one other useful built-in tool for working with Python files:

This will run sample.py and load the output into a Notebook cell.

Built-in magic commands in Jupyter Notebook

Jupyter kernels support a set of predefined ‘magic functions’. The builtin magics are two kinds line-oriented and cell-oriented.

Line magics are prefixed with the % character and work much like OS command-line calls: they get as an argument the rest of the line, where arguments are passed without parentheses or quotes.
Cell magics are prefixed with a double %%, and they are functions that get as an argument not only the rest of the line, but also the lines below it in a separate argument.

The built-in magics include:

Hope this post helps you get started with Jupyter Notebooks!

If you are a startup finding it hard to hire technical QA engineers, learn more about Qxf2 Services.

References:

I am an engineer with about 10 years of experience in software development and testing. Prior to joining Qxf2, I worked with reputed companies like Dell and Infosys. They helped me gain good experience in the manufacturing and healthcare domains. My QA career started at Dell. I got exposure to various testing tools, processes and methodologies, got an opportunity to work on various platforms like JMS Hermes, SOAP UI, data integration, queues, etc. I chose Qxf2 because it allowed remote working. My interests are vegetable gardening using organic methods, listening to music and reading books.

How to Install and Run Jupyter Python Notebook [Complete Guide with Example]

In this tutorial, you will learn Python commands to install and to run Jupyter Python Notebook in Windows/Linux/MacOS using pip tool.

Jupyter is an open source project which provides the interactive platform for executing programming code. It allows you to add a comment and to make it more explanatory.

If you want to install Jupyter for Python, Jupyter is available as python module for Python version 2 and 3. It is easy to install it using pip tool.

Table of Contents:

Installing Jupyter Python Notebook For Python 2 and 3

Pip is the default package management system or tool for installing/uninstalling and managing different packages in Python. It provides an OS independent system, so you can use it for any of the operating systems like Windows, Linux (Ubuntu), MacOS, etc…

You can install the Jupyter Notebook from the Python standard library with simple commands.

Python 3.7

Here, the first command is to upgrade the pip. It is always good practice to upgrade pip before installing any Python module.

Note: Managing Python module is crucial for Python developer. Learn more about managing Python packages using Pip.

Running Notebook Jupyter

Once you installed it, run following command to run the notebook.

Python 3 (older versions)

Remember, pip is package manager tool by default comes with Python binaries. Older Python 3 version has tool pip3. So you have to use pip3 instead of pip.

Running Jupyter Notebook

Python 2

Commands for installing Python packages for Python 2 versions are pretty straightforward.

Running Jupyter Notebook

Jupyter creates the localhost server to execute the Python programs. Running Jupyter notebook command will open the notebook page in the new tab of the browser as localhost.

How to Write and Run Jupyter Python Notebook | Your First Program

You can see the local directories in your browser notebook.

Follow the steps for Writing Your First Python Program in Notebook:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

In above Image, you can see the different cell where you can split your code.

It’s pretty easy to execute Python program, isn’t it? You don’t need to execute any command line code. Just click on Run, you have done with your Job.

Let’s find little more about using different types of cell in Jupyter.

Different Types of Cell in Jupyter Python Notebook

There are majorly three types of cell in Jupyter.

Code:

This cell is to write and execute actual Python code.

Markdown:

This is advance feature comes with Jupyter where you can write simple HTML commands to decorate the text inside the browser.

Heading:

As its name, with this cell, you can add heading to your Python Jupyter program. If your program has a large number of lines and cells, it is good practice to add headings. It is easy for understanding and maintaining Python code.

Keynotes for Using Python Jupyter Notebook

That’s all from this tutorial to install and run Jupyter Python Notebook.

Advantages of Using Jupyter Notebook

Once you start using it, you will just love. Running Python program in Jupyter is pretty easy in the browser as compared to running Python code in the different text editor. You don’t need to run command every-time to execute the code.

This tool is the best to explore data science libraries and projects.

If you are writing or practicing coding, adding explanatory comments for your code will help. Many of the Python tutors use Jupyter in their classroom to teach Python and other programming languages. Even I am using Jupyter in my Data Science course.

This article had covered all the detail required to execute your Python code in the Jupyter notebook. For more detail, you can visit Jupyter Official website.

What’s your experience or the best thing you like about Jupyter?

How to Use Jupyter Notebook: A Beginner’s Tutorial

What is Jupyter Notebook?

The Jupyter Notebook is an incredibly powerful tool for interactively developing and presenting data science projects. This article will walk you through how to use Jupyter Notebooks for data science projects and how to set it up on your local machine.

First, though: what is a “notebook”?

A notebook integrates code and its output into a single document that combines visualizations, narrative text, mathematical equations, and other rich media. In other words: it’s a single document where you can run code, display the output, and also add explanations, formulas, charts, and make your work more transparent, understandable, repeatable, and shareable.

Using Notebooks is now a major part of the data science workflow at companies across the globe. If your goal is to work with data, using a Notebook will speed up your workflow and make it easier to communicate and share your results.

Best of all, as part of the open source Project Jupyter, Jupyter Notebooks are completely free. You can download the software on its own, or as part of the Anaconda data science toolkit.

Although it is possible to use many different programming languages in Jupyter Notebooks, this article will focus on Python, as it is the most common use case. (Among R users, R Studio tends to be a more popular choice).

How to Follow This Tutorial

To get the most out of this tutorial you should be familiar with programming — Python and pandas specifically. That said, if you have experience with another language, the Python in this article shouldn’t be too cryptic, and will still help you get Jupyter Notebooks set up locally.

Jupyter Notebooks can also act as a flexible platform for getting to grips with pandas and even Python, as will become apparent in this tutorial.

(In fact, this article was written as a Jupyter Notebook! It’s published here in read-only form, but this is a good example of how versatile notebooks can be. In fact, most of our programming tutorials and even our Python courses were created using Jupyter Notebooks).

Example Data Analysis in a Jupyter Notebook

First, we will walk through setup and a sample analysis to answer a real-life question. This will demonstrate how the flow of a notebook makes data science tasks more intuitive for us as we work, and for others once it’s time to share our work.

So, let’s say you’re a data analyst and you’ve been tasked with finding out how the profits of the largest companies in the US changed historically. You find a data set of Fortune 500 companies spanning over 50 years since the list’s first publication in 1955, put together from Fortune’s public archive. We’ve gone ahead and created a CSV of the data you can use here.

As we shall demonstrate, Jupyter Notebooks are perfectly suited for this investigation. First, let’s go ahead and install Jupyter.

Installation

The easiest way for a beginner to get started with Jupyter Notebooks is by installing Anaconda.

Anaconda is the most widely used Python distribution for data science and comes pre-loaded with all the most popular libraries and tools.

Some of the biggest Python libraries included in Anaconda include NumPy, pandas, and Matplotlib, though the full 1000+ list is exhaustive.

Anaconda thus lets us hit the ground running with a fully stocked data science workshop without the hassle of managing countless installations or worrying about dependencies and OS-specific (read: Windows-specific) installation issues.

To get Anaconda, simply:

If you are a more advanced user with Python already installed and prefer to manage your packages manually, you can just use pip:

Creating Your First Notebook

In this section, we’re going to learn to run and save notebooks, familiarize ourselves with their structure, and understand the interface. We’ll become intimate with some core terminology that will steer you towards a practical understanding of how to use Jupyter Notebooks by yourself and set us up for the next section, which walks through an example data analysis and brings everything we learn here to life.

Running Jupyter

On Windows, you can run Jupyter via the shortcut Anaconda adds to your start menu, which will open a new tab in your default web browser that should look something like the following screenshot.

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

This isn’t a notebook just yet, but don’t panic! There’s not much to it. This is the Notebook Dashboard, specifically designed for managing your Jupyter Notebooks. Think of it as the launchpad for exploring, editing and creating your notebooks.

Be aware that the dashboard will give you access only to the files and sub-folders contained within Jupyter’s start-up directory (i.e., where Jupyter or Anaconda is installed). However, the start-up directory can be changed.

It is also possible to start the dashboard on any system via the command prompt (or terminal on Unix systems) by entering the command jupyter notebook ; in this case, the current working directory will be the start-up directory.

Jupyter’s Notebooks and dashboard are web apps, and Jupyter starts up a local Python server to serve these apps to your web browser, making it essentially platform-independent and opening the door to easier sharing on the web.

(If you don’t understand this yet, don’t worry — the important point is just that although Jupyter Notebooks opens in your browser, it’s being hosted and run on your local machine. Your notebooks aren’t actually on the web until you decide to share them.)

The dashboard’s interface is mostly self-explanatory — though we will come back to it briefly later. So what are we waiting for? Browse to the folder in which you would like to create your first notebook, click the “New” drop-down button in the top-right and select “Python 3”:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

Hey presto, here we are! Your first Jupyter Notebook will open in new tab — each notebook uses its own tab because you can open multiple notebooks simultaneously.

If you switch back to the dashboard, you will see the new file Untitled.ipynb and you should see some green text that tells you your notebook is running.

What is an ipynb File?

You can edit this yourself — if you know what you are doing! — by selecting “Edit > Edit Notebook Metadata” from the menu bar in the notebook. You can also view the contents of your notebook files by selecting “Edit” from the controls on the dashboard

However, the key word there is can. In most cases, there’s no reason you should ever need to edit your notebook metadata manually.

The Notebook Interface

Now that you have an open notebook in front of you, its interface will hopefully not look entirely alien. After all, Jupyter is essentially just an advanced word processor.

Why not take a look around? Check out the menus to get a feel for it, especially take a few moments to scroll down the list of commands in the command palette, which is the small button with the keyboard icon (or Ctrl + Shift + P ).

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

There are two fairly prominent terms that you should notice, which are probably new to you: cells and kernels are key both to understanding Jupyter and to what makes it more than just a word processor. Fortunately, these concepts are not difficult to understand.

Cells

We’ll return to kernels a little later, but first let’s come to grips with cells. Cells form the body of a notebook. In the screenshot of a new notebook in the section above, that box with the green outline is an empty cell. There are two main cell types that we will cover:

The first cell in a new notebook is always a code cell.

The result should look like this:

The output of a code cell also forms part of the document, which is why you can see it in this article. You can always tell the difference between code and Markdown cells because code cells have that label on the left and Markdown cells do not.

The “In” part of the label is simply short for “Input,” while the label number indicates when the cell was executed on the kernel — in this case the cell was executed first.

Run the cell again and the label will change to In [2] because now the cell was the second to be run on the kernel. It will become clearer why this is so useful later on when we take a closer look at kernels.

From the menu bar, click Insert and select Insert Cell Below to create a new code cell underneath your first and try out the following code to see what happens. Do you notice anything different?

In general, the output of a cell comes from any text data specifically printed during the cell’s execution, as well as the value of the last line in the cell, be it a lone variable, a function call, or something else. For example:

You’ll find yourself using this almost constantly in your own projects, and we’ll see more of it later on.

Keyboard Shortcuts

One final thing you may have observed when running your cells is that their border turns blue, whereas it was green while you were editing. In a Jupyter Notebook, there is always one “active” cell highlighted with a border whose color denotes its current mode:

Keyboard shortcuts are a very popular aspect of the Jupyter environment because they facilitate a speedy cell-based workflow. Many of these are actions you can carry out on the active cell when it’s in command mode.

Below, you’ll find a list of some of Jupyter’s keyboard shortcuts. You don’t need to memorize them all immediately, but this list should give you a good idea of what’s possible.

Go ahead and try these out in your own notebook. Once you’re ready, create a new Markdown cell and we’ll learn how to format the text in our notebooks.

Markdown

Markdown is a lightweight, easy to learn markup language for formatting plain text. Its syntax has a one-to-one correspondence with HTML tags, so some prior knowledge here would be helpful but is definitely not a prerequisite.

Remember that this article was written in a Jupyter notebook, so all of the narrative text and images you have seen so far were achieved writing in Markdown. Let’s cover the basics with a quick example:

Here’s how that Markdown would look once you run the cell to render it:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

(Note that the alt text for the image is displayed here because we didn’t actually use a valid image URL in our example)

When attaching images, you have three options:

There is plenty more to Markdown, especially around hyperlinking, and it’s also possible to simply include plain HTML. Once you find yourself pushing the limits of the basics above, you can refer to the official guide from Markdown’s creator, John Gruber, on his website.

Kernels

Behind every notebook runs a kernel. When you run a code cell, that code is executed within the kernel. Any output is returned back to the cell to be displayed. The kernel’s state persists over time and between cells — it pertains to the document as a whole and not individual cells.

For example, if you import libraries or declare variables in one cell, they will be available in another. Let’s try this out to get a feel for it. First, we’ll import a Python package and define a function:

Once we’ve executed the cell above, we can reference np and square in any other cell.

This will work regardless of the order of the cells in your notebook. As long as a cell has been run, any variables you declared or libraries you imported will be available in other cells.

You can try it yourself, let’s print out our variables again.

No surprises here! But what happens if we change the value of y?

If we run the cell above, what do you think would happen?

And if we ever wish to reset things, there are several incredibly useful options from the Kernel menu:

If your kernel is ever stuck on a computation and you wish to stop it, you can choose the Interrupt option.

Choosing a Kernel

You may have noticed that Jupyter gives you the option to change kernel, and in fact there are many different options to choose from. Back when you created a new notebook from the dashboard by selecting a Python version, you were actually choosing which kernel to use.

There kernels for different versions of Python, and also for over 100 languages including Java, C, and even Fortran. Data scientists may be particularly interested in the kernels for R and Julia, as well as both imatlab and the Calysto MATLAB Kernel for Matlab.

The SoS kernel provides multi-language support within a single notebook.

Each kernel has its own installation instructions, but will likely require you to run some commands on your computer.

Example Analysis

Now we’ve looked at what a Jupyter Notebook is, it’s time to look at how they’re used in practice, which should give us clearer understanding of why they are so popular.

It’s finally time to get started with that Fortune 500 data set mentioned earlier. Remember, our goal is to find out how the profits of the largest companies in the US changed historically.

It’s worth noting that everyone will develop their own preferences and style, but the general principles still apply. You can follow along with this section in your own notebook if you wish, or use this as a guide to creating your own approach.

Naming Your Notebooks

Before you start writing your project, you’ll probably want to give it a meaningful name. file name Untitled in the upper left of the screen to enter a new file name, and hit the Save icon (which looks like a floppy disk) below it to save.

Note that closing the notebook tab in your browser will not “close” your notebook in the way closing a document in a traditional application will. The notebook’s kernel will continue to run in the background and needs to be shut down before it is truly “closed” — though this is pretty handy if you accidentally close your tab or browser!

If the kernel is shut down, you can close the tab without worrying about whether it is still running or not.

The easiest way to do this is to select “File > Close and Halt” from the notebook menu. However, you can also shutdown the kernel either by going to “Kernel > Shutdown” from within the notebook app or by selecting the notebook in the dashboard and clicking “Shutdown” (see image below).

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

Setup

It’s common to start off with a code cell specifically for imports and setup, so that if you choose to add or change anything, you can simply edit and re-run the cell without causing any side-effects.

We’ll import pandas to work with our data, Matplotlib to plot charts, and Seaborn to make our charts prettier. It’s also common to import NumPy but in this case, pandas imports it for us.

That first line isn’t a Python command, but uses something called a line magic to instruct Jupyter to capture Matplotlib plots and render them in the cell output. We’ll talk a bit more about line magics later, and they’re also covered in our advanced Jupyter Notebooks tutorial.

For now, let’s go ahead and load our data.

It’s sensible to also do this in a single cell, in case we need to reload it at any point.

Save and Checkpoint

Now we’ve got started, it’s best practice to save regularly. Pressing Ctrl + S will save our notebook by calling the “Save and Checkpoint” command, but what is this checkpoint thing?

By default, Jupyter will autosave your notebook every 120 seconds to this checkpoint file without altering your primary notebook file. When you “Save and Checkpoint,” both the notebook and checkpoint files are updated. Hence, the checkpoint enables you to recover your unsaved work in the event of an unexpected issue.

You can revert to the checkpoint from the menu via “File > Revert to Checkpoint.”

Investigating Our Data Set

Now we’re really rolling! Our notebook is safely saved and we’ve loaded our data set df into the most-used pandas data structure, which is called a DataFrame and basically looks like a table. What does ours look like?

YearRankCompanyRevenue (in millions)Profit (in millions)
019551General Motors9823.5806
119552Exxon Mobil5661.4584.8
219553U.S. Steel3250.4195.4
319554General Electric2959.1212.6
419555Esmark2510.819.1
YearRankCompanyRevenue (in millions)Profit (in millions)
254952005496Wm. Wrigley Jr.3648.6493
254962005497Peabody Energy3631.6175.4
254972005498Wendy’s International3630.457.8
254982005499Kindred Healthcare3616.670.6
254992005500Cincinnati Financial3614.0584

Looking good. We have the columns we need, and each row corresponds to a single company in a single year.

Let’s just rename those columns so we can refer to them later.

Next, we need to explore our data set. Is it complete? Did pandas read it as expected? Are any values missing?

Okay, that looks good — that’s 500 rows for every year from 1955 to 2005, inclusive.

Let’s check whether our data set has been imported as we would expect. A simple check is to see if the data types (or dtypes) have been correctly interpreted.

Uh oh. It looks like there’s something wrong with the profits column — we would expect it to be a float64 like the revenue column. This indicates that it probably contains some non-integer values, so let’s take a look.

yearrankcompanyrevenueprofit
2281955229Norton135.0N.A.
2901955291Schlitz Brewing100.0N.A.
2941955295Pacific Vegetable Oil97.9N.A.
2961955297Liebmann Breweries96.0N.A.
3521955353Minneapolis-Moline77.4N.A.

Just as we suspected! Some of the values are strings, which have been used to indicate missing data. Are there any other values that have crept in?

That makes it easy to interpret, but what should we do? Well, that depends how many values are missing.

It’s a small fraction of our data set, though not completely inconsequential as it is still around 1.5%.

If rows containing N.A. are, roughly, uniformly distributed over the years, the easiest solution would just be to remove them. So let’s have a quick look at the distribution.

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

At a glance, we can see that the most invalid values in a single year is fewer than 25, and as there are 500 data points per year, removing these values would account for less than 4% of the data for the worst years. Indeed, other than a surge around the 90s, most years have fewer than half the missing values of the peak.

For our purposes, let’s say this is acceptable and go ahead and remove these rows.

We should check that worked.

Great! We have finished our data set setup.

If we were going to present your notebook as a report, we could get rid of the investigatory cells we created, which are included here as a demonstration of the flow of working with notebooks, and merge relevant cells (see the Advanced Functionality section below for more on this) to create a single data set setup cell.

This would mean that if we ever mess up our data set elsewhere, we can just rerun the setup cell to restore it.

Plotting with matplotlib

Next, we can get to addressing the question at hand by plotting the average profit by year. We might as well plot the revenue as well, so first we can define some variables and a method to reduce our code.

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

Wow, that looks like an exponential, but it’s got some huge dips. They must correspond to the early 1990s recession and the dot-com bubble. It’s pretty interesting to see that in the data. But how come profits recovered to even higher levels post each recession?

Maybe the revenues can tell us more.

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

That adds another side to the story. Revenues were not as badly hit — that’s some great accounting work from the finance departments.

With a little help from Stack Overflow, we can superimpose these plots with +/- their standard deviations.

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

That’s staggering, the standard deviations are huge! Some Fortune 500 companies make billions while others lose billions, and the risk has increased along with rising profits over the years.

Perhaps some companies perform better than others; are the profits of the top 10% more or less volatile than the bottom 10%?

There are plenty of questions that we could look into next, and it’s easy to see how the flow of working in a notebook can match one’s own thought process. For the purposes of this tutorial, we’ll stop our analysis here, but feel free to continue digging into the data on your own!

This flow helped us to easily investigate our data set in one place without context switching between applications, and our work is immediately shareable and reproducible. If we wished to create a more concise report for a particular audience, we could quickly refactor our work by merging cells and removing intermediary code.

Sharing Your Notebooks

When people talk about sharing their notebooks, there are generally two paradigms they may be considering.

Most often, individuals share the end-result of their work, much like this article itself, which means sharing non-interactive, pre-rendered versions of their notebooks. However, it is also possible to collaborate on notebooks with the aid of version control systems such as Git or online platforms like Google Colab.

Before You Share

A shared notebook will appear exactly in the state it was in when you export or save it, including the output of any code cells. Therefore, to ensure that your notebook is share-ready, so to speak, there are a few steps you should take before sharing:

This will ensure your notebooks don’t contain intermediary output, have a stale state, and execute in order at the time of sharing.

Exporting Your Notebooks

Jupyter has built-in support for exporting to HTML and PDF as well as several other formats, which you can find from the menu under “File > Download As.”

If you wish to share your notebooks with a small private group, this functionality may well be all you need. Indeed, as many researchers in academic institutions are given some public or internal webspace, and because you can export a notebook to an HTML file, Jupyter Notebooks can be an especially convenient way for researchers to share their results with their peers.

GitHub

Once you have a GitHub account, the easiest way to share a notebook on GitHub doesn’t actually require Git at all. Since 2008, GitHub has provided its Gist service for hosting and sharing code snippets, which each get their own repository. To share a notebook using Gists:

This should look something like the following:

How to run jupiter notebook. Смотреть фото How to run jupiter notebook. Смотреть картинку How to run jupiter notebook. Картинка про How to run jupiter notebook. Фото How to run jupiter notebook

If you created a public Gist, you will now be able to share its URL with anyone, and others will be able to fork and clone your work.

Creating your own Git repository and sharing this on GitHub is beyond the scope of this tutorial, but GitHub provides plenty of guides for you to get started on your own.

Nbviewer

Having grown to render hundreds of thousands of notebooks every week by 2015, NBViewer is the most popular notebook renderer on the web. If you already have somewhere to host your Jupyter Notebooks online, be it GitHub or elsewhere, NBViewer will render your notebook and provide a shareable URL along with it. Provided as a free service as part of Project Jupyter, it is available at nbviewer.jupyter.org.

The URL NBViewer displays when displaying a notebook is a constant based on the URL of the notebook it is rendering, so you can share this with anyone and it will work as long as the original files remain online — NBViewer doesn’t cache files for very long.

If you don’t like Nbviewer, there are other similar options — here’s a thread with a few to consider from our community.

Extras: Jupyter Notebook Extensions

We’ve already covered everything you need to get rolling in Jupyter Notebooks.

What Are Extensions?

Extensions are precisely what they sound like — additional features that extend Jupyter Notebooks’s functionality. While a base Jupyter Notebook can do an awful lot, extensions offer some additional features that may help with specific workflows, or that simply improve the user experience.

For example, one extension called «Table of Contents» generates a table of contents for your notebook, to make large notebooks easier to visualize and navigate around.

Another one, called Variable Inspector, will show you the value, type, size, and shape of every variable in your notebook for easy quick reference and debugging.

Another, called ExecuteTime, lets you know when and for how long each cell ran — this can be particularly convenient if you’re trying to speed up a snippet of your code.

These are just the tip of the iceberg; there are many extensions available.

Where Can You Get Extensions?

To get the extensions, you need to install Nbextensions. You can do this using pip and the command line. If you have Anaconda, it may be better to do this through Anaconda Prompt rather than the regular command line.

Once you’ve done that, start up a notebook and you should seen an Nbextensions tab. Clicking this tab will show you a list of available extensions. Simply tick the boxes for the extensions you want to enable, and you’re off to the races!

Installing Extensions

Once Nbextensions itself has been installed, there’s no need for additional installation of each extension. However, if you’ve already installed Nbextensons but aren’t seeing the tab, you’re not alone. This thread on Github details some common issues and solutions.

Extras: Line Magics in Jupyter

We mentioned magic commands earlier when we used %matplotlib inline to make Matplotlib charts render right in our notebook. There are many other magics we can use, too.

How to Use Magics in Jupyter

A good first step is to open a Jupyter Notebook, type %lsmagic into a cell, and run the cell. This will output a list of the available line magics and cell magics, and it will also tell you whether «automagic» is turned on.

If automagic is on, you can run a magic simply by typing it on its own line in a code cell, and running the cell. If it is off, you will need to put % before line magics and %% before cell magics to use them.

Many magics require additional input (much like a function requires an argument) to tell them how to operate. We’ll look at an example in the next section, but you can see the documentation for any magic by running it with a question mark, like so:

When you run the above cell in a notebook, a lengthy docstring will pop up onscreen with details about how you can use the magic.

A Few Useful Magic Commands

We cover more in the advanced Jupyter tutorial, but here are a few to get you started:

Magic CommandWhat it does
%runRuns an external script file as part of the cell being executed.

For example, if %run myscript.py appears in a code cell, myscript.py will be executed by the kernel as part of that cell.%timeitCounts loops, measures and reports how long a code cell takes to execute.%writefileSave the contents of a cell to a file.

For example, %savefile myscript.py would save the code cell as an external file called myscript.py.%storeSave a variable for use in a different notebook.%pwdPrint the directory path you’re currently working in.%%javascriptRuns the cell as JavaScript code.

Final Thoughts

Starting from scratch, we have come to grips with the natural workflow of Jupyter Notebooks, delved into IPython’s more advanced features, and finally learned how to share our work with friends, colleagues, and the world. And we accomplished all this from a notebook itself!

It should be clear how notebooks promote a productive working experience by reducing context switching and emulating a natural development of thoughts during a project. The power of using Jupyter Notebooks should also be evident, and we covered plenty of leads to get you started exploring more advanced features in your own projects.

If you’d like further inspiration for your own Notebooks, Jupyter has put together a gallery of interesting Jupyter Notebooks that you may find helpful and the Nbviewer homepage links to some really fancy examples of quality notebooks.

More Great Jupyter Notebooks Resources

About the author

Benjamin Pryke

Python and web developer with a background in computer science and machine learning. Co-founder of FinTech firm Machina Capital. Part-time gymnast and digital bohemian.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *