如何为Python 3设置Jupyter Notebook

介绍 (Introduction)

Jupyter Notebook offers a command shell for interactive computing as a web application. The tool can be used with several languages, including Python, Julia, R, Haskell, and Ruby. It is often used for working with data, statistical modeling, and machine learning.

Jupyter Notebook提供了用于作为Web应用程序进行交互式计算的命令外壳。 该工具可以与多种语言一起使用,包括Python,Julia,R,Haskell和Ruby。 它通常用于处理数据,统计建模和机器学习。

This tutorial will walk you through setting up Jupyter Notebook to run either locally or from an Ubuntu 18.04 server, as well as teach you how to connect to and use the notebook. Jupyter notebooks (or simply notebooks) are documents produced by the Jupyter Notebook app which contain both computer code and rich text elements (paragraph, equations, figures, links, etc.) which aid in presenting and sharing reproducible research.

本教程将引导您设置Jupyter Notebook,使其在本地或从Ubuntu 18.04服务器运行,并教您如何连接和使用笔记本。 Jupyter笔记本(或简称笔记本)是Jupyter Notebook应用程序生成的文档,其中包含计算机代码和丰富的文本元素(段落,方程式,图形,链接等),有助于呈现和共享可重复的研究。

By the end of this guide, you will be able to run Python 3 code using Jupyter Notebook running on a local machine or remote server.

在本指南结束时,您将能够使用在本地计算机或远程服务器上运行的Jupyter Notebook运行Python 3代码。

先决条件 (Prerequisites)

To follow this tutorial, you will need a Python 3 programming environment, either

要遵循本教程,您将需要一个Python 3编程环境,

  • on your local machine, or

    在本地计算机上 ,或

  • on an Ubuntu server.

    在Ubuntu服务器上 。

All the commands in this tutorial should be run as a non-root user. If root access is required for the command, it will be preceded by sudo. Initial Server Setup with Ubuntu 18.04 explains how to add users and give them sudo access.

本教程中的所有命令均应以非root用户身份运行。 如果该命令需要root用户访问权限,那么它将在sudo之前。 Ubuntu 18.04的初始服务器设置介绍了如何添加用户并赋予他们sudo访问权限。

第1步-安装Jupyter Notebook (Step 1 — Installing Jupyter Notebook)

In this section we will install Jupyter Notebook with pip.

在本节中,我们将使用pip安装Jupyter Notebook。

Activate the Python 3 programming environment you would like to install Jupyter Notebook into. In our example, we’ll install it into my_env, so we will ensure we’re in that environment’s directory and activate it like so:

激活要将Jupyter Notebook安装到的Python 3编程环境。 在我们的示例中,我们将其安装到my_env ,因此我们将确保我们位于该环境的目录中,并像下面这样激活它:

  • cd ~/environments

    cd〜/ 环境

  • . my_env/bin/activate

    。 my_env / bin /激活

Next, we can ensure that pip is upgraded to the most recent version:

接下来,我们可以确保将pip升级到最新版本:

  • pip install --upgrade pip

    点安装-升级点

Now we can install Jupyter Notebook with the following command:

现在我们可以使用以下命令安装Jupyter Notebook:

  • pip install jupyter

    pip安装jupyter

At this point Jupyter Notebook is installed into the current programming environment.

此时,Jupyter Notebook已安装到当前的编程环境中。

The next optional step is for those connecting a server installation of the web interface using SSH tunnelling.

下一个可选步骤适用于使用SSH隧道连接来连接Web界面的服务器安装的服务器。

步骤2(可选)—使用SSH隧道连接到服务器安装 (Step 2 (Optional) — Using SSH Tunneling to Connect to a Server Installation)

If you installed Jupyter Notebook on a server, in this section we will learn how to connect to the Jupyter Notebook web interface using SSH tunneling. Since Jupyter Notebook will run on a specific port on the server (such as :8888, :8889 etc.), SSH tunneling enables you to connect to the server’s port securely.

如果您在服务器上安装了Jupyter Notebook,则在本节中,我们将学习如何使用SSH隧道连接到Jupyter Notebook Web界面。 由于Jupyter Notebook将在服务器上的特定端口(例如:8888:8889等)上运行,因此SSH隧道使您能够安全地连接到服务器的端口。

The next two subsections describe how to create an SSH tunnel from 1) a Mac or Linux and 2) Windows. Please refer to the subsection for your local computer.

接下来的两个小节介绍了如何通过1)Mac或Linux和2)Windows创建SSH隧道。 请参考本地计算机的小节。

Mac或Linux的SSH隧道 (SSH Tunneling with a Mac or Linux)

If you are using a Mac or Linux, the steps for creating an SSH tunnel are similar to the How To Use SSH Keys with DigitalOcean Droplets using Linux or Mac guide except there are additional parameters added in the ssh command. This subsection will outline the additional parameters needed in the ssh command to tunnel successfully.

如果您使用的是Mac或Linux,则创建SSH隧道的步骤与“ 如何使用Linux或Mac使用DigitalOcean Droplet的SSH密钥使用指南”相似,但ssh命令中添加了其他参数。 本小节将概述ssh命令成功隧道所需的其他参数。

SSH tunneling can be done by running the following SSH command in a new local terminal window:

可以通过在新的本地终端窗口中运行以下SSH命令来完成SSH隧道:

  • ssh -L 8888:localhost:8888 your_server_username@your_server_ip

    ssh -L 8888 :localhost: 8888 your_server_username @ your_server_ip

The ssh command opens an SSH connection, but -L specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side (server). This means that whatever is running on the second port number (e.g. 8888) on the server will appear on the first port number (e.g. 8888) on your local computer.

ssh命令打开SSH连接,但-L指定将本地(客户端)主机上的给定端口转发到远程(服务器)上的给定主机和端口。 这意味着服务器上第二个端口号(例如8888 )上运行的任何内容都将出现在本地计算机上的第一个端口号(例如8888 )上。

Optionally change port 8888 to one of your choosing to avoid using a port already in use by another process.

(可选)将端口8888更改为您选择的端口之一,以避免使用另一个进程已使用的端口。

server_username is your username (e.g. sammy) on the server which you created and your_server_ip is the IP address of your server.

server_username是您在创建的服务器上的用户名(例如sammy ), your_server_ip是服务器的IP地址。

For example, for the username sammy and the server address 203.0.113.0, the command would be:

例如,对于用户名sammy和服务器地址203.0.113.0 ,命令将为:

  • ssh -L 8888:localhost:8888 [email protected]

    ssh -L 8888 :localhost: 8888 sammy @ 203.0.113.0

If no error shows up after running the ssh -L command, you can move into your programming environment and run Jupyter Notebook:

如果在运行ssh -L命令后没有出现错误,则可以进入编程环境并运行Jupyter Notebook:

  • jupyter notebook

    jupyter笔记本

You’ll receive output with a URL. From a web browser on your local machine, open the Jupyter Notebook web interface with the URL that starts with http://localhost:8888. Ensure that the token number is included, or enter the token number string when prompted at http://localhost:8888.

您将收到带有URL的输出。 在本地计算机上的Web浏览器中,使用以http://localhost:8888开头的URL打开Jupyter Notebook Web界面。 确保包含令牌号,或者在http://localhost:8888提示时输入令牌号字符串。

Windows和Putty的SSH隧道 (SSH Tunneling with Windows and Putty)

If you are using Windows, you can create an SSH tunnel using Putty as outlined in How To Use SSH Keys with PuTTY on DigitalOcean Droplets (Windows users).

如果您使用Windows,则可以使用Putty创建SSH隧道,如如何在DigitalOcean Droplet上将SSH密钥与PuTTY一起使用(Windows用户)中所述 。

First, enter the server URL or IP address as the hostname as shown:

首先,输入服务器URL或IP地址作为主机名,如下所示:

Next, click SSH on the bottom of the left pane to expand the menu, and then click Tunnels. Enter the local port number to use to access Jupyter on your local machine. Choose 8000 or greater to avoid ports used by other services, and set the destination as localhost:8888 where :8888 is the number of the port that Jupyter Notebook is running on.

接下来,单击左窗格底部的SSH以展开菜单,然后单击Tunnels 。 输入用于访问本地计算机上的Jupyter的本地端口号。 选择8000或更高版本以避免端口被其他服务使用,并将目标设置为localhost: 8888 ,其中:8888是Jupyter Notebook运行所在的端口号。

Now click the Add button, and the ports should appear in the Forwarded ports list:

现在,单击添加按钮,端口应出现在“ 转发的端口”列表中:

Finally, click the Open button to connect to the server via SSH and tunnel the desired ports. Navigate to http://localhost:8000 (or whatever port you chose) in a web browser to connect to Jupyter Notebook running on the server. Ensure that the token number is included, or enter the token number string when prompted at http://localhost:8000.

最后,单击“ 打开”按钮以通过SSH连接到服务器并建立所需端口的通道。 在网络浏览器中导航到http://localhost: 8000 (或您选择的任何端口)以连接到服务器上运行的Jupyter Notebook。 确保包含令牌号,或者在http://localhost:8000出现提示时输入令牌号字符串。

步骤3 —运行Jupyter Notebook (Step 3 — Running Jupyter Notebook)

With Jupyter Notebook installed, you can run it in your terminal. To do so, execute the following command:

安装Jupyter Notebook后,您可以在终端中运行它。 为此,执行以下命令:

  • jupyter notebook

    jupyter笔记本

A log of the activities of the Jupyter Notebook will be printed to the terminal. When you run Jupyter Notebook, it runs on a specific port number. The first notebook you are running will usually run on port 8888. To check the specific port number Jupyter Notebook is running on, refer to the output of the command used to start it:

Jupyter Notebook的活动日志将被打印到终端上。 当您运行Jupyter Notebook时,它将在特定的端口号上运行。 您正在运行的第一个笔记本通常将在端口8888上运行。 要检查正在运行的Jupyter Notebook的特定端口号,请参阅用于启动该端口的命令的输出:


   
     
     
     
     
Output
[I NotebookApp] Serving notebooks from local directory: /home/sammy [I NotebookApp] 0 active kernels [I NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/ [I NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). ...

If you are running Jupyter Notebook on a local computer (not on a server), your default browser should have opened the Jupyter Notebook web app. If not, or if you close the window, you can navigate to the URL provided in the output, or navigate to localhost:8888 to connect.

如果您在本地计算机(而不是服务器)上运行Jupyter Notebook,则默认浏览器应该已打开Jupyter Notebook Web应用程序。 如果不是,或者关闭窗口,则可以导航到输出中提供的URL,或者导航到localhost :8888进行连接。

Whenever you would like to stop the Jupyter Notebook process, press CTRL+C, type Y when prompted, and then hit ENTER to confirm.

每当您想停止Jupyter Notebook进程时,请按CTRL+C ,在出现提示时键入Y ,然后按ENTER进行确认。

You’ll receive the following output:

您将收到以下输出:


   
     
     
     
     
Output
[C 12:32:23.792 NotebookApp] Shutdown confirmed [I 12:32:23.794 NotebookApp] Shutting down kernels

Jupyter Notebook is now no longer running.

Jupyter Notebook现在不再运行。

步骤4 —使用Jupyter Notebook (Step 4 — Using Jupyter Notebook)

This section goes over the basics of using Jupyter Notebook. If you don’t currently have Jupyter Notebook running, start it with the jupyter notebook command.

本节介绍了使用Jupyter Notebook的基础知识。 如果您当前没有运行Jupyter Notebook,请使用jupyter notebook命令启动它。

You should now be connected to it using a web browser. Jupyter Notebook is very powerful and has many features. This section will outline a few of the basic features to get you started using the notebook. Jupyter Notebook will show all of the files and folders in the directory it is run from, so when you’re working on a project make sure to start it from the project directory.

现在,您应该使用网络浏览器连接到它。 Jupyter Notebook非常强大,并具有许多功能。 本节将概述一些基本功能,以帮助您开始使用笔记本电脑。 Jupyter Notebook将在运行它的目录中显示所有文件和文件夹,因此当您在处理项目时,请确保从项目目录中启动它。

To create a new notebook file, select New > Python 3 from the top right pull-down menu:

要创建新的笔记本文件,请从右上方的下拉菜单中选择“ 新建” >“ Python 3 ”:

This will open a notebook. We can now run Python code in the cell or change the cell to markdown. For example, change the first cell to accept Markdown by clicking Cell > Cell Type > Markdown from the top navigation bar. We can now write notes using Markdown and even include equations written in LaTeX by putting them between the $$ symbols. For example, type the following into the cell after changing it to markdown:

这将打开一个笔记本。 现在,我们可以在单元格中运行Python代码,或将单元格更改为markdown。 例如,通过单击顶部导航栏中的“ 单元格” >“ 单元格类型” >“ Markdown” ,将第一个单元格更改为接受Markdown。 现在,我们可以使用Markdown编写笔记,甚至可以通过将它们放在$$符号之间来包含用LaTeX编写的方程式。 例如,将其更改为markdown后,在单元格中键入以下内容:

# Simple Equation

Let us now implement the following equation:
$$ y = x^2$$

where $x = 2$

To turn the markdown into rich text, press CTRL+ENTER, and the following should be the results:

要将markdown转换为富文本,请按CTRL+ENTER ,结果应为:

You can use the markdown cells to make notes and document your code. Let’s implement that simple equation and print the result. Click on the top cell, then press ALT+ENTER to add a cell below it. Enter the following code in the new cell.

您可以使用markdown单元格做笔记并记录代码。 让我们实现该简单方程式并打印结果。 单击顶部的单元格,然后按ALT+ENTER在其下方添加一个单元格。 在新单元格中输入以下代码。

x = 2
y = x**2
print(y)

To run the code, press CTRL+ENTER. You’ll receive the following results:

要运行代码,请按CTRL+ENTER 。 您会收到以下结果:

You now have the ability to import modules and use the notebook as you would with any other Python development environment!

现在,您可以像导入其他Python开发环境一样导入模块并使用笔记本!

结论 (Conclusion)

Congratulations! You should now be able to write reproducible Python code and notes in Markdown using Jupyter Notebook. To get a quick tour of Jupyter Notebook from within the interface, select Help > User Interface Tour from the top navigation menu to learn more.

恭喜你! 您现在应该可以使用Jupyter Notebook在Markdown中编写可重现的Python代码和注释。 要从界面中快速浏览Jupyter Notebook,请从顶部导航菜单中选择“ 帮助” >“ 用户界面浏览 ”以了解更多信息。

From here, you may be interested to read our series on Time Series Visualization and Forecasting.

从这里开始,您可能有兴趣阅读我们有关时间序列的可视化和预测的系列文章 。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-set-up-jupyter-notebook-for-python-3

你可能感兴趣的:(python,linux,java,ubuntu,深度学习)