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

推荐订阅源

WordPress大学
WordPress大学
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
Webroot Blog
Webroot Blog
GbyAI
GbyAI
S
SegmentFault 最新的问题
Cyberwarzone
Cyberwarzone
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
J
Java Code Geeks
Google DeepMind News
Google DeepMind News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 【当耐特】
S
Secure Thoughts
酷 壳 – CoolShell
酷 壳 – CoolShell
AWS News Blog
AWS News Blog
Engineering at Meta
Engineering at Meta
S
Security Affairs
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
D
DataBreaches.Net
云风的 BLOG
云风的 BLOG
Hugging Face - Blog
Hugging Face - Blog
Google DeepMind News
Google DeepMind News
Spread Privacy
Spread Privacy
T
Threatpost
Forbes - Security
Forbes - Security
C
Cisco Blogs
Scott Helme
Scott Helme
Attack and Defense Labs
Attack and Defense Labs
Simon Willison's Weblog
Simon Willison's Weblog
腾讯CDC
The Last Watchdog
The Last Watchdog
Cloudbric
Cloudbric
Last Week in AI
Last Week in AI
Recorded Future
Recorded Future
小众软件
小众软件
V
Vulnerabilities – Threatpost
美团技术团队
人人都是产品经理
人人都是产品经理
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
Hacker News - Newest:
Hacker News - Newest: "LLM"
I
Intezer
月光博客
月光博客
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 司徒正美
C
Cybersecurity and Infrastructure Security Agency CISA
Martin Fowler
Martin Fowler
博客园 - 聂微东

陈少文的网站

巨变与机遇的未来十年 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
微信公众号 · 2017-12-26 · via 陈少文的网站

Please enable Javascript to view the contents

1. 简介

Jupyter Notebook(前称 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言。

Jupyter Notebook 实际上是一个 Web 应用程序,可以创建和共享程序文档,支持实时代码,数学方程,可视化和 Markdown。 用途包括:数据清理和转换,数值模拟,统计建模,机器学习等。

2. 本地安装

2.1 本地安装

推荐使用 Anaconda,自带 Numpy、Scipy、Matplotlib 等多种 Python 开发包和 Jupyter notebook。

如果使用单独的 Python 解释器,则需要安装 Jupyter:

2.2 本地运行

3. Docker 安装 Jupyter

官方提供了 base-notebook、minimal-notebook、all-spark-notebook、pyspark-notebook、scipy-notebook、datascience-notebook、tensorflow-notebook 以及 r-notebook 可选。根据自己的需要选择合适的镜像。

3.1 安装 Docker

1
2
3
4
# CentOS 7 上安装 Docker
yum install docker
# 启动 docker 服务进程
service docker start

3.2 运行 Jupyter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
docker run -d --name jupyter \
-p 8888:8888 \
--user root \
-e GRANT_SUDO=yes \
-e NB_UID=1000 \
-e NB_GID=100 \
-v /home/jupyter:/home/jovyan/work \
jupyter/tensorflow-notebook start-notebook.sh \
--NotebookApp.password='sha1:080ffe3b42b2:f592d77b83d318bad8ee771ba44a51569af552d8'
# 增加 NB_UID 对目录的权限
chown 1000 /home/jupyter

参数解释:

  • \ 表示换行,把一条命令拆成多行以方便阅读
  • -d 表示启动的容器进入到后台运行;
  • -p 表示指定端口,这里把宿主机的 8888 端口映射到容器的 8888 端口;
  • –user=root,允许运行 sudo;
  • -e 指定 jovyan 用户相关权限 ID;
  • –name 表示给启动的容器设定名字;
  • -v 表示把宿主机的目录挂载到容器中。Jupyter Docker 的文档目录是 /home/jovyan/work,为了使得容器被销毁时,文档不受影响,将本地目录 /home/local/jupyter 挂载到 /home/jovyan/work;
  • -NotebookApp.password 是登录的密码,可以在 Ipython 中使用如下命令生成:
1
2
3
4
5
In [1]: from notebook.auth import passwd
In [2]: passwd()
Enter password: 123456
Verify password: 123456
Out[2]: 'sha1:080ffe3b42b2:f592d77b83d318bad8ee771ba44a51569af552d8'

3.3 配置 Nginx

通过 nginx -t 命令找到 Nginx 的配置文件地址。在 nginx.conf 文件中,新增如下内容:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    upstream notebook {
        server localhost:8888;
    }
    server{
        listen 80;
        server_name yourdomain.com;
    location / {
        proxy_pass            http://notebook;
        proxy_set_header      Host $host;
        }

    location ~ /api/kernels/ {
        proxy_pass            http://notebook;
        proxy_set_header      Host $host;
        # websocket support
        proxy_http_version    1.1;
        proxy_set_header      Upgrade "websocket";
        proxy_set_header      Connection "Upgrade";
        proxy_read_timeout    86400;
        }
    location ~ /terminals/ {
        proxy_pass            http://notebook;
        proxy_set_header      Host $host;
        # websocket support
        proxy_http_version    1.1;
        proxy_set_header      Upgrade "websocket";
        proxy_set_header      Connection "Upgrade";
        proxy_read_timeout    86400;
        }
    }

使用 nginx -s reload ,重启 Nginx 服务后生效。

4. 使用

Jupyter 的基本单元是编程 cell 组成,也就是一个 In[ ]:

Jupyter 有三种类型的 cells:code,markdown cells,raw cells,常用的是 code cells 和 markdown cells 类型。

Cells 状态分为命令模式和编辑模式,Enter 进入编辑模式,ESC 进入命令模式,命令模式和编辑模式下支持很多操作快捷键。

常用命令模式快捷键:

1
2
3
4
5
6
7
8
- y: 单元进入代码状态
- m: 转入markdown状态
- r:转入raw状态
- a: 上方插入新单元
- b:下方插入新单元
- x:剪切选中单元
- c: 复制选中单元
- shift-v:粘贴到上方单元
  • 插入 Markdown

直接输入 Markdown ,然后 Run 即可渲染结果。支持标题,文本,视频,图片等。

  • 插入 LaTeX 公式

创建行内公式

1
$\Gamma(n) = (n-1)!\quad\forall n\in\mathbb N$。

块级公式

1
$x = \dfrac{-b \pm \sqrt{b^2 - 4ac}}{2a} $$
  • 代码块

可以直接在页面输出代码块,只需要在前后加上```代码块 ```即可。

  • 嵌入图片
1
2
from IPython.display import Image
Image(filename="yourpath.jpg")
  • 嵌入音乐

可以嵌入本地音乐和网络音乐

1
2
from IPython.display import Audio
Audio(filename="yourpath.wma")
1
2
from IPython.display import Audio
Audio(url="http://yourpath.wma")
  • 嵌入本地视频
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import io
import base64
from IPython.display import HTML

video = io.open('/home/test.mp4', 'r+b').read()
encoded = base64.b64encode(video)

HTML(data='''<video alt="test" controls>
     <source src="data:video/mp4;base64,{0}" type="video/mp4" />
     </video>'''.format(encoded.decode('ascii')))
  • 嵌入网页
1
2
from IPython.display import IFrame
IFrame('http://yourpath.com', width='100%', height=350)
  • 嵌入链接
1
2
from IPython.display import FileLink
FileLink('./test/a.ipynb')
  • 魔法命令

所有以 % 开头的方法,都是所谓的魔术方法 (Magic function),也就是 IPython 内置的一些方法。需要注意的是,魔术方法有%和 %% 之分,比如 %timeit 和 %% timeit。在 IPython 中有专门的叫法,前者叫 line magic 后者叫cell magic。顾名思义,前者是专门针对一行的命令,后者针对多行的命令。

通过 %lsmagic可以查看所有的 magic 命令,使用 ? 或者 ?? 可以查看该命令的信息,后者可以查看源码。如: %alias?,会出现该方法的描述。

5. 参考


微信公众号