0
点赞
收藏
分享

微信扫一扫

jupyter简单使用

孟佳 2022-09-18 阅读 34

Jupyter notebook是一个交互式的Python shell,也是IPython的封装版,非常适合用来进行数据分析和机器学习。也可以用来编辑python与markdown文档。

1. 安装以及简单测试

  1. 安装

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple jupyter

  1. jupyter 支持console、noteook 等交互方式

E:\pyspace>jupyter
usage: jupyter [-h] [--version] [--config-dir] [--data-dir] [--runtime-dir] [--paths] [--json] [--debug] [subcommand]

Jupyter: Interactive Computing

positional arguments:
subcommand the subcommand to launch

options:
-h, --help show this help message and exit
--version show the versions of core jupyter packages and exit
--config-dir show Jupyter config dir
--data-dir show Jupyter data dir
--runtime-dir show Jupyter runtime dir
--paths show all Jupyter paths. Add --json for machine-readable format.
--json output paths as machine-readable json
--debug output debug information about paths

Available subcommands: bundlerextension console dejavu execute kernel kernelspec migrate nbconvert nbextension notebook qtconsole run serverextension troubleshoot trust

Please specify a subcommand or one of the optional arguments.

  1. 测试

E:\pyspace>md jupyter_test

E:\pyspace>cd jupyter_test

E:\pyspace\jupyter_test>ls

E:\pyspace\jupyter_test>jupyter notebook
[I 14:08:27.113 NotebookApp] Serving notebooks from local directory: E:\pyspace\jupyter_test
[I 14:08:27.114 NotebookApp] Jupyter Notebook 6.4.12 is running at:
[I 14:08:27.114 NotebookApp] http://localhost:8888/?token=25605e2dd46385d2f02c49ecc4b3e9d402e03cbff7723aea
[I 14:08:27.114 NotebookApp] or http://127.0.0.1:8888/?token=25605e2dd46385d2f02c49ecc4b3e9d402e03cbff7723aea
[I 14:08:27.115 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 14:08:27.600 NotebookApp]

To access the notebook, open this file in a browser:
file:///C:/Users/xxx/AppData/Roaming/jupyter/runtime/nbserver-22392-open.html
Or copy and paste one of these URLs:
http://localhost:8888/?token=25605e2dd46385d2f02c49ecc4b3e9d402e03cbff7723aea
or http://127.0.0.1:8888/?token=25605e2dd46385d2f02c49ecc4b3e9d402e03cbff7723aea

如果正常会打开一个浏览,也可以复制任何一个链接到浏览器都可以访问到。 且该简易版server 代理的文件是相对于执行启动notebook的路径。

启动jupyter notebook时,位置在哪,浏览器目录就是当前目录。(浏览器中home的位置就是你启动jupyter notebook 的位置)

  1. 空的目录如下:

jupyter简单使用_json

点击New 可以新建目录、Terminal、TextFile等选项。

  1. python简单测试

New->python3。 然后重命名之后。第一行输入下面内容,点击Ctrl+Enter 即可查看运行结果

jupyter简单使用_json_02

上面可视化界面可以选择是代码还是Markdown 等文件, 也有一些其他操作按钮:允许、插入等按钮。

  1. jupyter 导出为py文件和md文件

E:\pyspace\jupyter_test>jupyter nbconvert --to script test1.ipynb
[NbConvertApp] Converting notebook test1.ipynb to script
[NbConvertApp] Writing 133 bytes to test1.py

导出为markdown:

E:\pyspace\jupyter_test>jupyter nbconvert --to markdown test1.ipynb
[NbConvertApp] Converting notebook test1.ipynb to markdown
[NbConvertApp] Writing 32 bytes to test1.md

也可以从浏览器选择file之后下载为pdf、md、py、html等文件格式。

2. jupyter模式简介

有两种模式:命令模式和编辑模式。

在一个cell中按下Enter就进入edit mode 可以对单元格里面的代码进行编辑,按下Esc进入command mode。

1. 命令模式

jupyter简单使用_5e_03

该模式下常用操作以及快捷键:

  1. 删除: 双击d
  2. 取消:z
  3. 多选:Shift+上下箭或者Shift+鼠标实现多选。 多选后可以dd 删除,z取消删除,Shift+M 合并, z取消合并(合并的第一个cell 需要自己删除合并的内容)
  4. 上部添加cell:a - above
  5. 下部添加cell:b-below
  6. 复制: c
  7. 粘贴: v - 下面粘贴; Shift + v - 上面粘贴
  8. 剪切:x
  9. 将单元格格式由代码改为markdown: m
  10. 行号显示与取消显示: l - line 缩写

2. 编辑模式

jupyter简单使用_5e_04

该模式下,单元格左边的颜色为绿色,并且cell内存在光标,则我们可以修改该cell下的代码,在此模式下,点击“Esc”则可退出编辑模式进入命令模式。

常有操作以及快捷键:

运行相关:

  1. 运行当前cell: ctrl+enter
  2. 运行当前cell并切换到下部cell(如果下部没有会创建新的cell): shift+enter
  3. 运行并插入新的一行并且选中:Alt+Enter

代码提示:

  1. 代码补全: tab
  2. 代码api以及参数解释:Shift + Tab

3. jupyter中的魔法函数

%lsmagic - 列出所有的魔法函数
%pwd - 显示Jupyter当前的工作空间
%history - 显示历史执行过的命令
%time - 计算当前代码行的运行时长。
%ls - 显示当前目录下相关文件

。。。 还有其他魔法函数,可以调用js、python 等其他脚本。

【当你用心写完每一篇博客之后,你会发现它比你用代码实现功能更有成就感!】



举报

相关推荐

0 条评论