jupyter notebook安装与使用入门(windows)常用快捷键、代码补全、代码块折叠

安装与卸载(windows端)

pandas开发中,代码编辑工具jupyter notebook是个不错的选择,它可以更加具体的展示DataFrame的结构,下面我们介绍如何安装jupyter notebook

第一种方式:通过Anaconda安装

这种方式比较简单,直接在我们的电脑上安装Anaconda就行,安装完成后,就可以看到配套的jupyter notebook,可以通过启动Anaconda后点击内部的jupyter notebook启动,或者绕过Anaconda直接找到Anconda文件夹下的jupyter notebook快捷方式启动。

第二种方式:通过Python安装

这里主讲第二种方式,因为笔者本人比较喜欢轻量化的东西,笔者只用jupyter notebook进行pandas相关代码的编程,没必要为此再安装一个conda环境。

  1. 安装

    笔者开发python代码都是在项目各自的虚拟环境中进行的,默认环境的三方模块极少。 jupyter notebook的依赖模块极多,且笔者将其作为一种在网页上的编译器使用,所以选择为其创建一个新的虚拟环境jupyter。(虚拟环境的创建与删除
    笔者重装时发现,最新版本配置方式可能与本文档有差异,所以这里给出笔者使用的版本。

    mkvirtualenv jupyter
    pip install jupyter notebook==6.5.6
    
  2. 配置工作目录

    打开命令行,切换到你虚拟环境的Scripts目录,以笔者的为例

    cd C:\workspace\venv\jupyter\Scripts
    

    运行如下命令生成配置文件

    jupyter notebook --generate-config
    

    结果如下图,红框即为生成的配置文件所在位置
    配置文件所在位置

    我们找到该文件,在内容中查找c.NotebookApp.notebook_dir,修改它的值''为你的工作目录,以笔者为例:c.NotebookApp.notebook_dir = 'C:\\workspace\\jupyter',这里需注意转义字符。

  3. 启动

    打开命令行,切换到你虚拟环境的Scripts目录启动,以笔者的为例

    jupyter notebook
    

    看到下面结果即为启动成功,启动后会自动打开默认浏览器到jupyter notebook页面
    命令行启动成功示例

    这样启动是不是有些麻烦,笔者决定写一个bat脚本文件来静默启动jupyter notebook

    @echo off
    % 静默运行,如果不需要可以将下方3行代码删除 %
    if "%1"=="h" goto begin
    start mshta vbscript:createobject("wscript.shell").run("""%~nx0"" h",0)(window.close)&&exit
    :begin
    % 启动 jupyter notebook %
    cd /d C:\workspace\venv\jupyter\Scripts
    jupyter notebook
    

    如果静默运行,如何关闭呢?打开任务管理器 - 详细信息,我们可以看到一个jupyter-notebook.exe进程,右键结束进程即可
    jupyter-notebook进程名称

    或者bat脚本文件关闭

    @echo off
    % 静默运行,如果不需要可以将下方3行代码删除 %
    if "%1"=="h" goto begin
    start mshta vbscript:createobject("wscript.shell").run("""%~nx0"" h",0)(window.close)&&exit
    :begin
    taskkill /f /im jupyter-notebook.exe
    

    这里笔者将二者合一,编写了一个自动判断进程状态来决定启动还是关闭的bat脚本

    @echo off
    ::命令行输出中文乱码解决方案
    ::文件另存为,下方选择ANSI编码
    ::chcp 65001
    
    ::判断 jupyter-notebook.exe 进程是否存在
    tasklist | find /i "jupyter-notebook.exe"
    if %errorlevel%==0 ( 
    	goto 1
    ) else (
    	goto 2
    )
    
    :1
    echo jupyter notebook 正在运行
    echo 开始关闭 jupyter notebook
    ::静默运行,如果不需要可以将下方3行代码删除
    if "%1"=="h" goto begin
    start mshta vbscript:createobject("wscript.shell").run("""%~nx0"" h",0)(window.close)&&exit
    :begin
    taskkill /f /im jupyter-notebook.exe
    ping 127.0.0.1 /n 2 > nul
    exit
    
    :2
    echo jupyter notebook 未启动
    echo 开始启动 jupyter notebook
    ::静默运行,如果不需要可以将下方3行代码删除
    if "%1"=="h" goto begin
    start mshta vbscript:createobject("wscript.shell").run("""%~nx0"" h",0)(window.close)&&exit
    :begin
    cd /d C:\workspace\venv\jupyter\Scripts
    jupyter notebook
    ping 127.0.0.1 /n 2 > nul
    exit
    
  4. 卸载

    # pip uninstall jupyter notebook是无法完全卸载jupyter的
    # 而且网上搜到的许多卸载方法,都是治标不治本,经测试无法将依赖模块完全删干净
    # 猜测:可能未被卸载的依赖可以独立使用,无法判断用户是否在使用
    # 所以单独为其创建一个虚拟环境就很nice,此时我们直接删除该虚拟环境即可
    rmvirtualenv jupyter
    

使用

快捷键

通用

功能快捷键
编辑模式 转到 命令模式Esc
命令模式 转到 编辑模式Enter

编辑模式

功能快捷键
执行单元格Ctrl + Enter
执行并移动到下一单元格Shift + Enter
执行并向下新建、移动到下一单元格Alt + Enter

命令模式

功能快捷键
删除单元格命令模式 + D,D
剪切单元格命令模式 + X
显示行号命令模式 + L
查找与替换命令模式 + F
中断内核命令模式 + I,I
合并单元格命令模式 + Shift + M

代码补全、代码块折叠

进入jupyter notebook所在的虚拟环境安装jupyter_contrib_nbextensions插件

workon jupyter
pip install jupyter_contrib_nbextensions
jupyter nbextensions_configurator enable --user
jupyter contrib nbextension install --user --skip-running-check

重启jupyter notebook,菜单栏会出现Nbextensions插件菜单,取消勾选disable ...,勾选下方Hinterland选项,如下图所示,就此可以开始代码补全之旅了;另外,勾选Codefolding选项表示开启代码块折叠
Nbextensions

自定义主题

进入jupyter notebook所在的虚拟环境安装jupyterthemes插件

workon jupyter
pip install jupyterthemes
pip install --upgrade jupyterthemes  # 用于更新主题
jt -h # 查看帮助,包括下方启用主题时附加的一些参数的描述
jt -l # 查看主题
# 其中暗色主题:chesterish、gruvboxd、monokai、oceans16、onedork、solarizedd
# 推荐两种字体:Hack、DejaVu Sans Mono
# 对应应用主题时的参数是:hack、dejavu
# 博主目前主题配置
jt -t oceans16 -f hack -fs 14 -ofs 10 -dfs 10 -nfs 14 -tfs 14 -cursw 3 -kl -TN -cellw 80%
# 当设置自定义主题后,同时开启行号显示和代码块折叠后,行号会被挤到代码cell里😂
# 有没有大佬知道怎么解决,可以留言帮忙解决一下
  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值