专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

Jupyter Notebook 使用入门

Jupyter Notebook官方介绍是基于网页的用于交互计算的应用程序。其可被应用于全过程计算:开发、文档编写、运行代码和展示结果。简单的说以网页的形式编写代码并且展示代码运行结果。

1 安装

先使用下面命令安装:

(jupyter) ➜  python -m pip install --upgrade pip
(jupyter) ➜  python -m pip install jupyter

然后启动:

(jupyter) ➜  notebook jupyter notebook

更多详情直接参考:官方安装指南

推荐使用虚拟环境进行安装,参考: Python虚拟环境指南2019版

2 使用

notebook使用比较简单,新建一个notebook,解释器默认python3就好。每一个notebook,都是由一系列的cell组成,每个cell可以独立运行,也可以互相依赖。而每个cell又分3种类型,初期关注markdown和code两种类型就好了。markdown cell就是支持md语法的cell,运行后展示成MD文件效果,了解md语法就好,没什么好介绍的。code cell,可以分下面2种:

  • 系统命令
  • python语法

2.1 code cell使用系统命令

比如下面具有下面3个系统指令的cell

!pwd
!pip list
!!pip install requests

运行后效果大概如下:

/Users/cc/codes/notebook
Package            Version  
------------------ ---------
appnope            0.1.0    
backcall           0.1.0    
bitarray           0.9.3 
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: requests in /Users/tu/codes/venv/jupyter/lib/python3.7/site-packages (2.19.1)
Requirement already satisfied: certifi>=2017.4.17 in /Users/tu/codes/venv/jupyter/lib/python3.7/site-packages (from requests) (2018.8.24)
Requirement already satisfied: idna<2.8,>=2.5 in /Users/tu/codes/venv/jupyter/lib/python3.7/site-packages (from requests) (2.7)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /Users/tu/codes/venv/jupyter/lib/python3.7/site-packages (from requests) (3.0.4)
Requirement already satisfied: urllib3<1.24,>=1.21.1 in /Users/tu/codes/venv/jupyter/lib/python3.7/site-packages (from requests) (1.23)

我们使用**!**这个符号标注要执行的命令

2.2 code cell使用python语法

上面我们安装了requests包,在下面的cell中就可以使用requests这个包了:

import time
import requests
url = "https://github.com/"
response = requests.post(url, timeout=5)
print(response.status_code)
print(response)

这段代码运行后的结果是:

404
<Response [404]>

这样jupyter notebook就上手了, 玩玩就熟练了,非常适合做各种教程。直接的感受可以看:

github.com/julienr/ipy…

3 其它

3.1 python2.7环境启动异常

python2.7 启动可能遇到:

jupyter notebook
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 4: ordinal not in range(128)

可以使用下面命令:

LANG=zn jupyter notebook

3.2 常用命令

自定义IP,启动网络服务:

jupyter notebook --ip=0.0.0.0

有时候网页过期后需要token,可以使用下面命令找回token:

(jupyter) ➜  notebook jupyter notebook list
Currently running servers:
http://localhost:8888/?token=522595b39819bb6d7c89c3b416b8ac91a18f57d6594b0b76 :: /Users/tu/codes/notebook

list指令如果异常,使用 pip install notebook --upgrade 升级notebook可以解决。

查看帮助:

jupyter notebook --help

前段时间工作比较忙,又赶上一些其它事情,写文章的状态就打断了。一旦那口气松掉,很长时间都找不回来,所以先从一篇简单的笔记开始,慢慢恢复吧。

文章永久链接:https://tech.souyunku.com/43760

未经允许不得转载:搜云库技术团队 » Jupyter Notebook 使用入门

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们