PythonUbuntu 云服务器部署 Jupyter notebook
Posted Fxtack
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PythonUbuntu 云服务器部署 Jupyter notebook相关的知识,希望对你有一定的参考价值。
Ubuntu 云服务器部署 Jupyter notebook
文章目录
一. Jupyter notebook 简介
Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行多种语言。不过我们主要是用来做 Python 相关的内容。
Jupyter Notebook 的本质是一个 Web 应用程序,便于创建和共享文学化程序文档,支持实时代码,数学方程,可视化和 markdown。 用途包括:数据清理和转换,数值模拟,统计建模,机器学习等等。
二. Jupyter notebook 安装与部署
本文介绍的是在 ubuntu 云服务器上安装与部署 jupyter notebook 服务,从而使得有网络的地方都可以使用云端的 jupyter notebook。
1. 安装 Python3 与 Jupyter notebook
在 Ubuntu 中使用以下指令安装 Python3。在安装了 Python3 之后使用 pip3 安装 jupyter notebook。
sudo apt-get install python3
pip3 install jupyter notebook
2. 配置 jupyter notebook
使用以下指令生成 jupyther 配置文件。注意,jupyter 的配置文件是 .py
类型的。
$ jupyter notebook --generate-config
使用以下指令编辑 jupyter 配置文件。
$ vim ~/.jupyter/jupyter_notebook_config.py
在配置文件末尾加上一系列的配置。
![](https://image.cha138.com/20221122/80ddcb170eb44e51b1f10fac7fb1bcaa.jpg)
-
c.NotebookApp.ip = '*'
设置允许访问 jupyter notebook 的 ip。“*” 设置为为任意主机可访问。
*c.NotebookApp.port = 8888
设置 jupyter notebook 服务端口。8888 本来就是 jupyter notebook 的默认端口。
*c.NotebookApp.open_browser = False
如果该属性设置为 False,你将无法在本机使用浏览器访问 jupyter notebook。
-
c.NotebookApp.notebook_dir ='/root/jupyter_projects'
这里是设置 jupyter notebook 的根目录,若不设置将默认root的根目录。
jupyter notebook 的根目录相当于其工作目录,如下图。
-
-
c.NotebookApp.allow_root = True
jupyter notebook 默认情况下无法由 root 用户启动。通过设置该属性为 True 则可以使用 root 用户启动 jupyter notebook。
3. 准备网络
上述配置中设置什么端口,就把防火墙的什么端口打开。并且在云服务器控制台的安全组中配置规则,开放端口。
当然你也可以直接把端口都开了,防火墙关了,不过这样会不安全,谨慎考虑。
4. 开启 jupyter notebook 服务
$ jupyter notebook
如图,把 jupyter notebook 给开了
![](https://image.cha138.com/20221122/c71777e921794eb18b9eb7dcf6c0dc48.jpg)
注意
这里只是方便说明如何安装 jupyter notebook 才这样启动。这样启动 jupyter notebook 会导致当关闭终端连接时 jupyter notebook 服务进程终止。如果想 jupyter notebook 在安装配置好后持续的运行,得使用
nohup jupyter notebook &
指令使其在后台运行。
5. 远程访问 jupyter notebook 服务
在自己的电脑浏览器上输入云服务器的公网 ip 和端口(如果有解析好的域名,就用域名访问),访问 jupyter notebook。会要求输入密码。
而下方则提示重设密码。重设密码需要使用 token 值。token 值在后台终端上显示的 jupyter URL 中有显示。
![](https://image.cha138.com/20221122/3b114c909f0540fe97fa8b4d2f8444df.jpg)
如下图为后台终端,我把我的 token 值打码了。将 token 的值输入到上图中的 Token 字段,就可以重设新密码了。此后就可以使用密码登录。当然你也可以每次都用 token 值直接登录(不推荐)。
![](https://image.cha138.com/20221122/04fda97d25fa47cabaed5d999964e6da.jpg)
![](https://image.cha138.com/20221122/78dd1f4b91ed431584742ccffeb23c99.jpg)
三. 测试 Python3 “Hello World”
在根目录的右侧的 New 下拉列表中选中 Python3,然后就可以进行代码书写了。
简单写了一个测试。可见 markdown 可被翻译,Python3 也可运行。(注意在 cell 中必须 Shift
+Enter
才能运行)。
更多 Jupyter notebook 使用细节请读者自行学习。
提示
Jupyter notebook 的 Python3 运行内核在服务器上。所以如果你在 Jupyter notebook 中想用的包出错显示找不到时,你得到服务器上
pip3 install
来下载安装所需的包。
以上是关于PythonUbuntu 云服务器部署 Jupyter notebook的主要内容,如果未能解决你的问题,请参考以下文章
PythonUbuntu 云服务器部署 Jupyter notebook
从零学习PythonUbuntu14.10下Python开发环境配置