1.更换软件源
配置文件是 /etc/apt/sources.list,pip源
2.安装pip,用pip安装jupyter
自带python3,所以不需要安装
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python-pip3
sudo pip3 install --upgrade pip3
sudo pip3 install jupyter
添加环境变量
sudo vi /etc/profile
将下面内容添加到末尾
export PATH=/home/xxx(这里是你自己的用户名)/.local/bin:$PATH
保存退出
3.启动jupyter
source /etc/profile
jupyter notebook
4.配置jupyter
jupyter notebook --generate-config
必须配置
# 解决跨域问题
c.NotebookApp.tornado_settings = {
'headers': {
'Content-Security-Policy': "frame-ancestors self *; report-uri /api/security/csp-report",
}
}
# 可访问的IP地址
c.NotebookApp.ip = '*'
# 端口
c.NotebookApp.port = 8888
# 启动服务端时是否打开浏览器
c.NotebookApp.open_browser = False
# 去掉密码验证
c.NotebookApp.token = ""
# 工作空间
c.NotebookApp.notebook_dir = '/root/engine/stategy'
#Maximum rate at which stream output can be sent on iopub before they are limited.
c.NotebookApp.iopub_data_rate_limit = 90000000000
#(sec) Time window used to check the message and data rate limits.
c.NotebookApp.rate_limit_window = 10000000
#Disable cross-site-request-forgery protection
#Jupyter notebook 4.3.1 introduces protection from cross-site request forgeries, requiring API requests to either:
#originate from pages served by this server (validated with XSRF cookie and token), or authenticate with a token
#Some anonymous compute resources still desire the ability to run code, completely without authentication. These services can disable all authentication and security checks, with the full knowledge of what that implies.
c.NotebookApp.disable_check_xsrf = True
# 内核死了,自动重启内核
#c.KernelManager.autorestart = True
# 是否可以通过前端修改密码
c.NotebookApp.allow_password_change = False
# 前端是否展示退出按钮
c.NotebookApp.quit_button = False
# 关闭自动退出功能
c.NotebookApp.shutdown_no_activity_timeout = 0
jupyter显示内存占用情况
pip install nbresuse
启动的时候加上参数
jupyter notebook --ResourceUseDisplay.mem_limit=204800
可选配置
# 是否开启新建终端
c.NotebookApp.terminals_enabled = False
# 默认打开的目录路径
c.NotebookApp.notebook_dir = "workspace"
修改jupyter css,js
修改jupyter源码
找到路径/usr/local/python3/lib/python3.7/site-packages/notebook/static/custom
1.去掉jupyter头部
vi custom.css
#header-container {
visibility: hidden;
}
2.去掉删除按钮
.delete-button {
visibility:hidden;
}
3.title MindSense
修改 custom.js
/usr/local/python3/lib/python3.7/site-packages/notebook/static/notebook/js
main.min.js
56885行改为document.title = 'MindSense';
savewidget.js
144行改为document.title = 'MindSense';
document.getElementsByTagName("title")[0].innerText = 'MindSense';
window.onfocus = function () {
document.title = 'MindSense'
}
document.onkeydown = function (event) {
var e = event;
if(e && e.keyCode == 13){
document.getElementsByTagName("title")[0].innerText = 'MindSense';
}
}
/usr/local/python3/lib/python3.7/site-packages/notebook/static/base/images
4.导航栏加入new.png的图片
修改page.html
在125行 div #header
里加{%block new_logo%}{%endblock%}
修改notebook.html
39行加入
{%block new_logo%}
<div id="ipython_notebook" class="nav navbar-brand">
<img alt="logo" height="56" width="145" style="margin-right: 5px" src="{{static_url("base/images/new.png")}}"/></div>
{%endblock%}
5.将logo修改为下载里面的图片logo.ico
三个ico
favicon.ico
favicon-file.ico
favicon-notebook.ico
重启就好了
后台启动
nohup jupyter-notebook --allow-root &
6.shutdown
/mnt/ssd1/software/anaconda3/lib/python3.7/site-packages/notebook/static/tree/js
var shutdown_button = $('<button/>')
.addClass('btn btn-warning btn-xs')
.text(i18n._('Shutdown'))
.click(function() {
var path = $(this).parent().parent().parent().data('path');
that.shutdown_notebook(path);
})
.appendTo(running_indicator);
改为
.click(function() {
var path = $(this).parent().parent().parent().parent().data('path');
that.shutdown_notebook(path);
})
5.配置jupyter-notebook远程访问
开放centos的8889端口
firewall-cmd --zone=public --add-port=8889/tcp --permanent
systemctl restart firewalld.service
查看端口号
netstat -ntlp //查看当前所有tcp端口
netstat -ntulp |grep 1935 //查看所有1935端口使用情况·
linux下输入以下命令查看IP
ifconfig
局域网中输入IP:8888就可以访问
6.开机自启动与后台运行
- 查看jupyter-notebook安装位置
whereis jupyter-notebook
推荐采用用户目录,最好不要采用root目录,root用户为 /usr/local/bin/jupyter-notebook 普通用户为/home/$USER/.local/bin/jupyter-notebook
- 新建服务文件
配置文件分三个部分
第一部分:
[Unit] #unit本身的说明,以及与其他相依赖的daemon的设置,包括在什么服务之前或之后启动等设置
- Description #该Unit描述,用systemctl list-units和systemctl status查看服务时候的描述内容就是这里定义的
- Documentation #提供该Unit可以进一步文件查询的地址或者位置
- After(Before) #在哪些之后(前)启动,说明该Unit可以在哪些daemon启动后(前)才能够启动,非强制性,只是推荐规范
- Requires #需要启动哪些,说明启动该Unit需要启动哪些Unit才能启动,强制性的,如果不启动该项定义的daemon则无法启动该Unit
- Wants #想要启动哪些,与Requires相反,说明启动该Unit后想要启动哪些daemon,非强制
- Conflicts #代表该Unit与列表中的daemon有冲突,如果该设置里的服务启动了,那么这个Unit就不能启动
第二部分:[Service],[Socket],[Timer].[Mount],[Path]...等 #不同的unit type就使用相对应的设置项目,这里面设定服务启动的脚本,环境配置文件文件名,重启方式等等
1.[Service]区块用来 Service 的配置,只有 Service 类型的 Unit 才有这个区块。它的主要字段如下。
2.Type:定义启动时的进程行为。它有以下几种值。
3.Type=simple:默认值,执行ExecStart指定的命令,启动主进程
4.Type=forking:以 fork 方式从父进程创建子进程,创建后父进程会立即退出
5.Type=oneshot:一次性进程,Systemd 会等当前服务退出,再继续往下执行
6.Type=dbus:当前服务通过D-Bus启动
7.Type=notify:当前服务启动完毕,会通知Systemd,再继续往下执行
8.Type=idle:若有其他任务执行完毕,当前服务才会运行
9.ExecStart:启动当前服务的命令
10.ExecStartPre:启动当前服务之前执行的命令
11.ExecStartPost:启动当前服务之后执行的命令
12.ExecReload:重启当前服务时执行的命令
13.ExecStop:停止当前服务时执行的命令
14.ExecStopPost:停止当其服务之后执行的命令
15.RestartSec:自动重启当前服务间隔的秒数
16.Restart:定义何种情况 Systemd 会自动重启当前服务,可能的值包括always(总是重启)、on-success、on-failure、on-abnormal、on-abort、on-watchdog
17.TimeoutSec:定义 Systemd 停止当前服务之前等待的秒数
18.Environment:指定环境变量
第三部分:[Install]指定unit安装在哪个target(unit集合)里面去
1.[Install]通常是配置文件的最后一个区块,用来定义如何启动,以及是否开机启动。它的主要字段如下。
2.WantedBy:它的值是一个或多个 Target,当前 Unit 激活时(enable)符号链接会放入/etc/systemd/system目录下面以 Target 名 + .wants后缀构成的子目录中
3.RequiredBy:它的值是一个或多个 Target,当前 Unit 激活时,符号链接会放入/etc/systemd/system目录下面以 Target 名 + .required后缀构成的子目录中
4.Alias:当前 Unit 可用于启动的别名
5.Also:当前 Unit 激活(enable)时,会被同时激活的其他 Unit
sudo vim /lib/systemd/system/jupyter.service
# 加入以下代码
[Unit]
Description=jupyter notebook
After=network.target
[Service]
Type=simple
# 这里填用户名,下同
User=yu
EnvironmentFile=/home/yu/.local/bin/jupyter-notebook
ExecStart=/home/yu/.local/bin/jupyter-notebook
ExecStop=/usr/bin/pkill /home/yu/.local/bin/jupyter-notebook
KillMode=process
Restart=on-failure
RestartSec=30s
[Install]
WantedBy=multi-user.target
3.保存后终端输入以下代码
sudo systemctl daemon-reload
sudo systemctl enable jupyter.service
sudo systemctl start jupyter.service
输入以下代码观察有无报错信息
systemctl status jupyter
配置成功的话jupyter-notebook已经添加进系统服务,同时满足开机自启与后台运行
进入http://localhost:8888或者http://127.0.0.1:8888验证
其他常用控制jupyter命令
#移除jupyter服务
sudo systemctl disable jupyter.service
#重启jupyter服务
sudo systemctl restart jupyter.service
#停止jupyter服务
sudo systemctl stop jupyter.service
(Ubuntu与windows下配置安装jupyter-notebook以及其开机自启、后台运行与远程访问)[https://blog.csdn.net/starfish55555/article/details/96788672]
(Jupyter Notebook二次开发的经验(一)——安装开发版本)[https://blog.csdn.net/weixin_39198406/article/details/90812334]
(Jupyter Notebook介绍、安装及使用教程)[https://blog.csdn.net/gui694278452/article/details/81710184]
(Systemd 入门教程:实战篇)
[http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html]
(jupyter notebook 初步使用配置调整)[https://www.bbsmax.com/A/KE5QVjvPdL/]