惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
P
Privacy International News Feed
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 叶小钗
F
Fortinet All Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 最新话题
AWS News Blog
AWS News Blog
Engineering at Meta
Engineering at Meta
Attack and Defense Labs
Attack and Defense Labs
Recent Announcements
Recent Announcements
Recent Commits to openclaw:main
Recent Commits to openclaw:main
PCI Perspectives
PCI Perspectives
Cloudbric
Cloudbric
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
IT之家
IT之家
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
J
Java Code Geeks
M
MIT News - Artificial intelligence
Cisco Talos Blog
Cisco Talos Blog
V2EX - 技术
V2EX - 技术
Webroot Blog
Webroot Blog
Microsoft Security Blog
Microsoft Security Blog
Cyberwarzone
Cyberwarzone
博客园 - 聂微东
G
Google Developers Blog
W
WeLiveSecurity
罗磊的独立博客
P
Privacy & Cybersecurity Law Blog
阮一峰的网络日志
阮一峰的网络日志
A
About on SuperTechFans
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
T
Tailwind CSS Blog
V
Visual Studio Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
Secure Thoughts
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
Google DeepMind News
Google DeepMind News
Google DeepMind News
Google DeepMind News
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
F
Full Disclosure
Blog — PlanetScale
Blog — PlanetScale
The Last Watchdog
The Last Watchdog
P
Proofpoint News Feed

陈少文的网站

巨变与机遇的未来十年 Kubernetes 平台管理软件压力测试方案 使用镜像部署 Hexo 静态页面 终于等到你 - GitHub 镜像仓库服务(ghcr.io) 一起来学 Go --(6)Interface 一起来学 Go --(5)Goroutine 和 Channel 什么是函数式编程 如何在 Kubernetes 集群集成 Kata 柯里化与偏函数 使用 PyGithub 自动创建 Label 软件产品是团队能力的输出 Helm 2 、Helm 3 比较 IoT 变现 Kubernetes 中的 DNS 服务 国内的 Helm 镜像源 Harbor 使用自签证书支持 Https 访问 DevOps 工具链之 Prow 如何使用 kfctl 安装 Kubeflow VS Code 无法下载 Go 插件的工具包 工程师更应具有服务精神 你不知道的 Docker 使用技巧 使用 Docker 运行 Tensorflow 论中国 什么是左移 如何清空 Git 仓库全部历史记录 一禅小和尚 有风吹过厨房 时间的玫瑰 如何在 CentOS 安装 GPU 驱动 开发 Tips(19) 使用 Velero 备份 Kubernetes 集群 Kubernetes Cheat Sheet 开发 Tips(18) 如何构建一个 Java 工程 开发 Tips(17) KubeSpray 安装 Kubernetes 报错 ip in ansible_all_ipv4_addresses 基于 Kubernetes 和 Jenkins 搭建自动化测试系统 在 Kubernetes 上动态创建 Jenkins Slave 使用 Jenkins 进行服务拨测 开发 Tips(16) Kubernetes 签发 Ingress 证书及日常故障运维 Kubernetes 中 Deployment 的基本操作 Kubernetes 中的证书 如何使用 KubeBuilder 开发一个 Operator Kubernetes 1.6.0 安装问题汇总 镜像管理工具 -- Harbor 开发 Tips(15) Docker 如何拉取镜像 开发 Tips(14) 使用 Helm 安装 harbor 开发 Tips(13) 使用 S2I 构建云原生应用 在 Kubernetes 中使用 emptyDir、hostPath、localVolume 开发 Tips(12) 开发 Tips(11) 代码质量分析工具 SonarQube 使用 Kubeadm 安装 Kubernetes 集群 一起来学 Go --(4)常用函数 Kubernetes 中的 Ceph Kubernetes 之 Volumes Kubernetes 之 Labels、Selectors 开发 Tips(10) 开源正在重构商业模式 Kubernetes 之网络 Kubernetes 之 API 使用 Helm 和 Operator 快速部署 Prometheus Kubernetes 复杂有状态应用管理框架 -- Operator Kubernetes 的包管理器 -- Helm 一起来学 Go --(3)Go Modules 如何一步一步地优化博客方案 kubectl 实用指南 Kubernetes 中的基本概念 搭建远程 Kubernetes 开发环境 大公司和小公司的 ToB 思路 开发 Tips(9) Go 入门指南 一起来学 Go --(2)数据与逻辑结构 如何预防 Web 富文本中的 XSS 攻击 django-xss-cleaner 云工作时代 一起来学 Go --(1)背景与特点 SaaS 开发团队的不同阶段 你不知道的 Git 使用技巧 输出既服务 微服务设计 继续奔跑 开发 Tips(8) 从账户安全到二次验证 Django 性能之数据库查询优化 Django 性能之分库分表 敏捷开发之研发流程 打造一致性的团队 开发 Tips(7) Pytest 进阶学习之 Mock PaaS 部署之 buildpack Go 开发配置 领域输出才是 PaaS 的核心竞争力 Pytest 入门学习 开发 Tips(6) 如何使用 Jenkins、Docker、GitLab 搭建 Django 自动化部署流程
Jupyter 安装 Python3、Go 内核
微信公众号 · 2018-09-09 · via 陈少文的网站

前面提到 Jupyter Notebook 是一个交互式笔记本,支持运行 40 多种编程语言,非常适合教学。最近,学习 Go 语言,就想起了 Jupyter。本文主要描述如何在 Jupyter 里面安装 Python3 和 Go 内核。

1. Jupyter 安装

在 CentOS 7 中,默认安装了 Python 2.7,有现成的 Python 环境可用。

  • 安装 Jupyter
  • 生成密码

在 IPython 中,利用 passwd 函数生成密码 sha1 值。

ipython
In [1]: from IPython.lib import passwd

In [2]: passwd()
Enter password:
Verify password:
Out[2]: 'sha1:************************************************'
  • 生成配置文件

执行命令,jupyter notebook --generate-config,生成 ~/.jupyter/jupyter_notebook_config.py 配置文件。

编辑 jupyter_notebook_config 文件,新增如下内容:

1
2
3
4
5
c.NotebookApp.allow_root = True
c.NotebookApp.open_browser = False
c.NotebookApp.port = 9999
c.NotebookApp.password = u'sha1:************************************************''
c.ContentsManager.root_dir = '/root/jupyter/data'

其中,passwd 为刚才生成的密码 sha1 值,root_dir 为 Jupyter Notebook 数据存放根目录。

  • 启动 Jupyter

至此,基本就完成了 Jupyter 的安装和配置,可以启动 Jupyter。

  • 非 roor 启动时,报错

如果以非 root 权限启动,会报错:OSError: [Errno 13] Permission denied: ‘/run/user/0/jupyter’。这是由于没有权限所致。只需要在有权限的目录,使用 virtualenv 创建 Python 环境 /home/jupyter/py2

执行命令,修改 XDG_RUNTIME_DIR 值:

1
export XDG_RUNTIME_DIR="/home/jupyter/py2/jupyter"

查看 runtime 路径:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
jupyter --path
config:
    /root/.jupyter
    /usr/etc/jupyter
    /usr/local/etc/jupyter
    /etc/jupyter
data:
    /root/.local/share/jupyter
    /usr/local/share/jupyter
    /usr/share/jupyter
runtime:
    /home/jupyter/py2/jupyter

2. supervisor 托管 Jupyter

在服务器端运行 Jupyter,为了能够在程序 dump 时,自动拉起,需要将 Jupyter 托管给 supervisor。

  • 安装 supervisor
  • 创建配置文件目录
1
mkdir -p /etc/supervisor/conf.d
  • 创建 supervisor 配置文件
1
echo_supervisord_conf > /etc/supervisor/supervisord.conf
  • 编辑 supervisor 配置文件

编辑 /etc/supervisor/supervisord.conf 文件,新增如下内容:

1
2
[include]
files = /etc/supervisor/conf.d/*.ini

/etc/supervisor/conf.d/ 文件夹下的所有配置文件,都会被导入 。这样是为了方便管理,可以为每个进程或相关的几个进程单独写成一个配置文件。

  • 新增 Jupyter 配置 jupyter.ini

新增文件 /etc/supervisor/conf.d/jupyter.ini,内容如下:

1
2
3
4
5
6
7
8
9
[program:jupyter]
command=jupyter notebook --allow-root
user=root
autostart=true
;autorestart=true
;startsecs=30
;startretries=5
stdout_logfile=/var/log/jupyter/jupyter-access.log
stderr_logfile=/var/log/jupyter/jupyter-error.log
  • 创建日志目录
  • 启动 supervisor

可以简单的执行,启动 supervisor:

也可以指定配置文件启动

1
/usr/bin/supervisord -c /etc/supervisord.conf
  • 查看状态 supervisorctl

supervisorctl 是一个用于与 supervisord 交互的程序。通过 supervisorctl ,我们可以完成对托管进程的管理。下面是一些基本的命令:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# 停止某一个进程,program_name 为 [program:x] 里的 x
supervisorctl stop program_name
# 启动某个进程
supervisorctl start program_name
# 重启某个进程
supervisorctl restart program_name
# 结束所有属于名为 groupworker 这个分组的进程 (start,restart 同理)
supervisorctl stop groupworker:
# 结束 groupworker:name1 这个进程 (start,restart 同理)
supervisorctl stop groupworker:name1
# 停止全部进程,注:start、restart、stop 都不会载入最新的配置文件
supervisorctl stop all
# 载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程
supervisorctl reload
# 根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启
supervisorctl update
# 查看进程状态
supervisorctl status

3. Jupyter 配置 Go Kernel

gophernotes 是 Jupyter 的 Go 内核。下图是,配置完内核后,在 Jupyter 中执行 Go 的示例:

  • 安装依赖项

gophernotes 有四个依赖项:

  1. Go 1.9+
  2. Jupyter Notebook or nteract
  3. ZeroMQ 4.X.X
  4. pkg-config

在 CentOS 7 上只需安装 ZeroMQ 和 Go, 即可,满足依赖。

  • 安装 gopherdata
1
2
3
go get -u github.com/gopherdata/gophernotes
mkdir -p ~/.local/share/jupyter/kernels/gophernotes
cp $GOPATH/src/github.com/gopherdata/gophernotes/kernel/* ~/.local/share/jupyter/kernels/gophernotes
  • 没有加入 PATH,找不到 gophernotes

错误提示:OSError: [Errno 2] No such file or directory

1
ln -s $HOME/go/bin/gophernotes /go/bin/gophernotes

可以按照 GitHub 上面的提示创建软链接,也可以直接将 gophernotes 拷贝到 /usr/bin/ 目录。

4. Jupyter 配置 Python3 Kernel

配置完 Go,顺手又配置了一下 Python3 。下图是,配置完内核之后,在 Jupyter 执行 Python3 的示例:

  • 查看可用的 Python3 版本
  • 安装 Python36
  • 安装 pip3
1
2
wget https://bootstrap.pypa.io/get-pip.py
python36 get-pip.py
  • 安装 kernel
python36 -m pip install ipykernel
python36 -m ipykernel install
  • widgetsnbextension 没有安装并启用,报错

前端错误提示,404,找不到 /jupyter/nbextensions/widgets/notebook/js/extension.js

安装并启动 widgetsnbextension ,即可。

1
2
jupyter nbextension install --py widgetsnbextension
jupyter nbextension enable widgetsnbextension --py

5. Markdown 增强

notedown 插件,可以使用 Markdown 创建 Jupyter 笔记,同时支持在线新建、编辑 Markdown 文件。

  • 安装
1
pip install https://github.com/aaren/notedown/tarball/master
  • 配置

编辑 ~/.jupyter/jupyter_notebook_config.py,新增如下内容:

1
c.NotebookApp.contents_manager_class = 'notedown.NotedownContentsManager'

6. 参考