jupyter 基本使用、技巧、插件、主题

629 阅读2分钟

1.基本使用

启动笔记本
jupyter notebook

指定端口号启动
jupyter notebook --port 端口号(默认为 8888)

启动但不打开浏览器
jupyter notebook --no-browser

启动不打开浏览器, 并指定笔记存放的路径(jupyter默认笔记存放在黑窗体启动的路径下)
jupyter notebook --notebook-dir='C:\fastwork\Python\JupyterNotebook' --no-browser

创建配置文件(比如修改笔记存放路径: c.NotebookApp.notebook_dir = r'C:\fastwork\Python\JupyterNotebook')
jupyter notebook --generate-config

2.一些技巧

  • Markdown在文中设置链接并定位
[跳转到a标签那里](#hello)
<a id=hello>这是a标签所在位置</a>
  • 加载指定网页源代码
%load https://www.baidu.com
  • 加载本地Python文件
%load ".py"文件的绝对路径

输入命令后,可以按CTRL 回车来执行命令。第一次执行,是将本地的Python文件内容加载到单元格内。此时,Jupyter Notebook会自动将“%load”命令注释掉(即在前边加井号“#”),以便在执行已加载的文件代码时不重复执行该命令;第二次执行,则是执行已加载文件的代码。

  • 直接运行本地Python文件
%run ".py"文件的绝对路径
!python3 ".py"文件的绝对路径
!python ".py"文件的绝对路径

输入命令后,可以按 control return 来执行命令,执行过程中将不显示本地Python文件的内容,直接显示运行结果。

  • 获取当前笔记存放目录的路径(eg: C:\fastwork\Python\JupyterNotebook)
%pwd
!pwd
pwd
  • 隐藏笔记本输入单元格
from IPython.display import display
from IPython.display import HTML
import IPython.core.display as di # Example: di.display_html('<h3>%s:</h3>' % str, raw=True)

# 这行代码的作用是:当文档作为HTML格式输出时,将会默认隐藏输入单元格。
di.display_html('<script>jQuery(function() {if (jQuery("body.notebook_app").length == 0) { jQuery(".input_area").toggle(); jQuery(".prompt").toggle();}});</script>', raw=True)

# 这行代码将会添加“Toggle code”按钮来切换“隐藏/显示”输入单元格。
di.display_html('''<button onclick="jQuery('.input_area').toggle(); jQuery('.prompt').toggle();">Toggle code</button>''', raw=True)

3.安装插件

安装插件管理包
pip install jupyter_contrib_nbextensions
pip install jupyter_nbextensions_configurator

安装一些插件并启用插件管理器, 之后需要重启jupyter
jupyter nbextensions_configurator install --user
jupyter nbextensions_configurator enable --user

必备插件

Table of Contents   目录
Code prettify       代码规范
Hinterland          代码补全
Nofity              会在代码执行完成后提醒
Execute Time        上次执行时间
Codefolding         代码折叠
Move selected cells 使用键盘快捷键 Alt-up 和 Alt-down 移动选定的单元格
Gist it             一键发布到 Github gists
  • Hide input all 隐藏/显示 代码输入框

image.png

4.主题

安装
pip install jupytertheme
查看主题列表
jt -l
设置主题
jt -t <theme_name>
回归默认主题...
jt -r

5.参考

Jupyter Notebook介绍、安装及使用教程 👉 zhuanlan.zhihu.com/p/33105153
jupyter安装扩展插件 👉 blog.csdn.net/weixin_4590…
Windows 设置开机启动(常用软件、jupyter、redis) 👉 juejin.cn/post/703914…
开发工具 | 你真的会用jupyter吗? 👉 zhuanlan.zhihu.com/p/83252017