Jupyter笔记本是一个开源 Web 应用程序,可让您创建和共享交互式代码、可视化等。该工具可与多种编程语言一起使用,包括 Python、Julia、R、Haskell 和 Ruby。它通常用于处理数据、统计建模和机器学习。
本教程将引导您完成设置 Jupyter Notebook 以从 Ubuntu 18.04 服务器运行,并教您如何连接和使用 Notebook。Jupyter 笔记本(或者简单地笔记本电脑)是由 Jupyter Notebook 应用程序生成的文档,其中包含计算机代码和富文本元素(段落、方程、图形、链接等),有助于呈现和共享可重复的研究。在本指南的最后一步中,您将使用在远程服务器上运行的 Jupyter Notebook 运行 Python 3 代码。
为了完成本指南,您需要一个带有防火墙的 Ubuntu 18.04 服务器实例和一个配置了 sudo 权限的非 root 用户。您可以通过运行我们的初始服务器设置教程.
要开始此过程,请从 Ubuntu 存储库安装 Python 编程环境的依赖项。 Ubuntu 18.04 预装了 Python 3.6.9。稍后您将使用 Python 包管理器 pip 安装其他组件。
首先更新本地apt
封装索引:
接下来,安装 pip 和 Python 头文件,这些文件由 Jupyter 的一些依赖项使用:
-
sudo apt installpython3-pip python3-dev
您现在可以继续设置 Python 虚拟环境。
安装一些依赖项后,您可以创建一个 Python 虚拟环境来管理您的项目。 Jupyter 将安装到该虚拟环境中。
您将需要访问virtualenv
可以使用 pip 安装的命令。
首先,输入以下命令升级 pip:
-
sudo -H pip3 install - 升级 pip
然后安装virtualenv
用这个命令:
The -H
标志确保安全策略设置home
环境变量到目标用户的主目录。
With virtualenv
安装完毕后,您就可以开始构建您的环境了。创建并移动到可以保存项目文件的目录。以下示例目录名为my_project_dir
。使用对您和您正在从事的工作有意义的名称。
通过键入以下命令创建目录:
Move into the my_project_dir
with this command:
Within the project directory, create a Python virtual environment. This tutorial will refer to the environment as my_project_env
but, again, you can call it something that is relevant to your project.
- virtualenv my_project_env
This creates a directory called my_project_env
within your my_project_dir
directory. Inside, it will install a local version of Python and a local version of pip. You can use this to install and configure an isolated Python environment for Jupyter.
安装 Jupyter 之前,请激活虚拟环境。您可以通过输入以下内容来做到这一点:
-
source my_project_env/bin/activate
您的提示符应该会更改,以表明您现在正在类似于以下内容的 Python 虚拟环境中进行操作:
现在您已准备好将 Jupyter 安装到此虚拟环境中。
在虚拟环境处于活动状态时,使用 pip 的本地实例安装 Jupyter。
Note:当虚拟环境被激活时(当你的提示符出现时)(my_project_env)
之前),使用pip
代替pip3
,即使您使用的是 Python 3。该工具的虚拟环境副本始终命名为pip
,无论 Python 版本如何。
至此,您已经成功安装了运行 Jupyter 所需的所有软件。您现在可以启动笔记本服务器。
您现在拥有运行 Jupyter Notebook 所需的一切。要运行它,请执行以下命令:
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 run will usually use port 8888
. To check the specific port number Jupyter Notebook is running on, refer to the output of the command used to start it:
Output
[I 21:23:21.198 NotebookApp] Writing notebook server cookie secret to /home/sammy/.local/share/jupyter/runtime/notebook_cookie_secret
[I 21:23:21.361 NotebookApp] Serving notebooks from local directory: /home/sammy/my_project_dir
[I 21:23:21.361 NotebookApp] The Jupyter Notebook 6.4.10 is running at:
[I 21:23:21.361 NotebookApp] http://localhost:8888/?token=1fefa6ab49a498a3f37c959404f7baf16b9a2eda3eaa6d72
[I 21:23:21.361 NotebookApp] or http://127.0.0.1:8888/?token=1fefa6ab49a498a3f37c959404f7baf16b9a2eda3eaa6d72
[I 21:23:21.361 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 21:23:21.361 NotebookApp] No web browser found: could not locate runnable browser.
[C 21:23:21.361 NotebookApp]
To access the notebook, open this file in a browser:
file:///home/sammy/.local/share/jupyter/runtime/nbserver-7924-open.html
Or copy and paste one of these URLs:
http://localhost:8888/?token=1fefa6ab49a498a3f37c959404f7baf16b9a2eda3eaa6d72
or http://127.0.0.1:8888/?token=1fefa6ab49a498a3f37c959404f7baf16b9a2eda3eaa6d72
如果您在本地计算机(而不是服务器)上运行 Jupyter Notebook,则可以导航到显示的 URL 以连接到 Jupyter Notebook。如果您在服务器上运行 Jupyter Notebook,则需要使用 SSH 隧道连接到服务器,如下一节所述。
此时,您可以保持 SSH 连接打开并保持 Jupyter Notebook 运行,也可以退出应用程序并在设置 SSH 隧道后重新运行它。
如果您想停止 Jupyter Notebook 进程,请按CTRL + C
, type Y
, 进而ENTER
确认。将显示以下输出:
Output
[C 19:23:34.939 NotebookApp] Shutdown confirmed
[I 19:23:34.940 NotebookApp] Shutting down 0 kernels
[I 19:23:34.941 NotebookApp] Shutting down 0 terminals
您现在可以设置 SSH 隧道来访问笔记本。
Jupyter Notebook 在服务器上的特定端口上运行(例如:8888
, :8889
ETC。)。 SSH 隧道使您能够安全地连接到服务器的端口。
接下来的两小节介绍如何从 Mac、Linux 和 Windows 操作系统创建 SSH 隧道。请参阅您本地计算机的小节。
如果您使用的是 Mac 或 Linux,创建 SSH 隧道的步骤与使用 SSH 登录远程服务器类似,只不过在ssh
命令。
可以通过在新的本地终端窗口中运行以下 SSH 命令来完成 SSH 隧道:
-
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.
Optionally change port 8888
to one of your choosing to avoid using a port already in use by another process.
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.
For example, for the username sammy
and the server address 203.0.113.0
, the command would be:
-
ssh -L 8888:localhost:8888 sammy@203.0.113.0
如果运行后没有出现错误信息ssh -L
命令,您可以进入编程环境并运行 Jupyter Notebook:
您将收到带有 URL 的输出。从本地计算机上的 Web 浏览器中,打开 Jupyter Notebook Web 界面,其 URL 开头为http://localhost:8888
。确保包含令牌编号,或者在出现提示时输入令牌编号字符串http://localhost:8888
:
Output
...
[I 21:23:21.361 NotebookApp] The Jupyter Notebook 6.4.10 is running at:
[I 21:23:21.361 NotebookApp] http://localhost:8888/?token=1fefa6ab49a498a3f37c959404f7baf16b9a2eda3eaa6d72
...
如果您使用的是 Windows,则可以使用以下命令创建 SSH 隧道Putty.
首先,在地址栏中输入服务器URL或IP地址主机名(或 IP 地址)字段如图:
Next, click SSH on the bottom of the left pane to expand the menu, and then click Tunnels. Enter the local port number you want 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.
点击Add按钮,端口应出现在转发端口 list:
Finally, click the Open button to connect to the server via SSH and tunnel the desired ports. Navigate to http://localhost:8000
(or the 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
.
无论您是在 Mac/Linux 操作系统还是 Windows 中进行连接,您现在都应该通过 SSH 隧道连接到您的服务器。您现在已准备好在网络浏览器中使用 Jupyter Notebook。
如果您当前没有运行 Jupyter Notebook,请使用以下命令启动它jupyter notebook
命令:
您现在应该使用网络浏览器连接到它。 Jupyter Notebook 是一个非常强大的工具,具有许多功能。本节将概述一些帮助您开始使用笔记本的基本功能。 Jupyter Notebook 将显示其运行目录中的所有文件和文件夹,因此当您处理项目时,请确保从项目目录启动它。
要创建新的笔记本文件,请选择New > Python 3从右上角的下拉菜单中:
这将打开一个笔记本。您现在可以在单元格中运行 Python 代码或将单元格更改为 markdown。例如,通过单击更改第一个单元格以接受 MarkdownCell > 细胞类型 > Markdown从顶部导航栏。您现在可以使用 Markdown 编写笔记,甚至可以包含用以下语言编写的方程式LaTeX通过将它们放在$$
符号。例如,将其更改为 markdown 后,在单元格中键入以下内容:
# First Equation
Let us now implement the following equation:
$$ y = x^2$$
where $x = 2$
要将 Markdown 转换为富文本,请按CTRL + ENTER
,结果应如下:
您可以使用 Markdown 单元格来做笔记并记录您的代码。要实现该方程并打印结果,请单击顶部单元格,然后按ALT + ENTER
在其下方添加一个单元格。在新单元格中输入以下代码。
要运行代码,请按CTRL + ENTER
。您将收到以下结果:
您现在有能力导入模块并像使用任何其他 Python 开发环境一样使用 Notebook。
您现在应该能够使用 Jupyter Notebook 在 Markdown 中编写可重现的 Python 代码和注释。要从界面中快速浏览 Jupyter Notebook,请选择Help > 用户界面导览从顶部导航菜单了解更多信息。
从这里,您可以通过阅读开始数据分析和可视化项目Python 3 中使用 pandas 和 Jupyter Notebook 进行数据分析和可视化.
如果您有兴趣深入了解,可以阅读我们的系列文章时间序列可视化和预测.