01-第一次使用Jupyter

第一次使用Jupyter

具体见个人Python图书馆:https://ds-ebooks.github.io

Contents

  • 安装必要的包:ipython, numpy, scipy,pandas,jupyter notebook
  • 基本环境配置:安装 Anaconda 或者 Miniconda,但事实上可以不用安装,有Python的pip够用了

一、更改Jupyter notebook的工作空间

链接跳转测试

1.直接在工作目录打开

  • 进入工作目录文件夹
  • 键盘Shift+鼠标右键->在此处打开命令窗口-> 在弹出的命令窗口中输入:Jupyter Notebook
  • Jupyter被打开,定位到当前目录!

2.通过快捷方式属性修改

  • 创建Jupyter Notebook快捷方式,在属性中起始位置修改;
    image
  • 然后运行快捷方式就可以是打开的起始界面定位到你设置的目录;
  • BUT,只能通过快捷方式才能这样,如果直接运行jupyter,则直接定位到初始的默认文件夹下;

3.修改config文件

  • 在cmd中输入jupyter notebook –generate-config,找到配置文件目录;
  • 在这两行中修改路径:
# The directory to use for notebooks and kernels.
c.NotebookApp.notebook_dir = u'D:\Jupyter'
  • 但是我没成功,他显示路径错误,在每一个分号前加了莫名奇妙的又加了一个分号,我再次打开配置文件,发现他是一个分号啊,不懂。

所以,最后我选择第一种方式最直接。

二、常用命令

1.误删了jupyter notebook中的代码

  • 方式一:
for line in locals()['In']:
    print(line)
  • 方式二:
history

2.jupyter魔法

# 当前目录
%pwd
u'D:\\Python\\Scripts\\notebook'
  • 运行脚本
%run name.py
# 或者
%matplotlib inline
# matplotlib画图
%matplotlib inline
  • 代码写入脚本
%%writefile foo.py
  • 设置运行的python版本
%%script python
  • debug模式
%debug
  • 自动保存,执行后,每3秒保存一次文件
%autosave 3

三、Jupyter的各种快捷键

  • 执行当前cell,并自动跳到下一个cell:Shift Enter
  • 执行当前cell,执行后不自动调转到下一个cell:Ctrl-Enter
  • 是当前的cell进入编辑模式:Enter
  • 退出当前cell的编辑模式:Esc
  • 删除当前的cell:双D
  • 为当前的cell加入line number:单L
  • 将当前的cell转化为具有一级标题的maskdown:单1
  • 将当前的cell转化为具有二级标题的maskdown:单2
  • 将当前的cell转化为具有三级标题的maskdown:单3
  • 为一行或者多行添加/取消注释:Crtl /
  • 撤销对某个cell的删除:z
  • 浏览器的各个Tab之间切换:Crtl PgUp和Crtl PgDn
  • 快速跳转到首个cell:Crtl Home
  • 快速跳转到最后一个cell:Crtl End

四、Jupyter Notebook如何导入代码

即导入代码到jupyter notebook的cell中

1.将本地的.py文件load到jupyter的一个cell中

问题背景:有一个test.py文件,需要将其载入到jupyter的一个cell中
test.py内容如下:

print "Hello World!"

方法步骤:

# %load test.py
print "HellO World!"

Shift Enter运行后,%load test.py被自动加入了注释符号#,test.py中的所有代码都被load到了当前的cell中.

2.从网络load代码到jupyter

%load https://matplotlib.org/examples/color/color_cycle_demo.html

五、Jupyter运行python文件

  • 利用jupyter的cell是可以运行python文件的,即在cell中运行如下代码:
%run test.py
HellO World!

六、Jupyter一些其他琐碎用法

1.jupyter的cell可以作为unix command使用

# 查看python版本:
!python --version 
Python 2.7.13
# 运行python文件:
!python test.py
HellO World!

2.Magic functions用法

待深究:The cell magic in Ipython

3.获取current working directory

current_path = %pwd 
print current_path
D:\Python\Scripts\notebook

4.使用Matplotlib绘图

# 有时是弹不出图像框的,此时,可以在开头加入:
%matplotlib inline

七、Jupyter中的Markdown

1.链接跳转

## 一、更改Jupyter notebook的工作空间
[链接跳转](#更改Jupyter notebook的工作空间)
...
<a id='七、Jupyter中的Markdown'></a>
## 七、Jupyter中的Markdown

2.添加目录功能

$ pip2 install jupyter_nbextensions_configurator
$ pip2 install jupyter_contrib_nbextensions

3.在Jupyter中打开md文件

jupyter notebook 生成md这个大家都会,可是在github当中有很多很好的md文件,如果不能在jupyter notebook当中打开体验,实在是太让人难过了。

  • 安装notedown
pip install notedown
  • 打开配置文件jupyter_notebook_config.py,添加:
c.NotebookApp.contents_manager_class = ‘notedown.NotedownContentsManager
  • 重启jupyter notebook,OK

八、主题配置

pip install --upgrade jupyterthemes
jt -l     //查看能够使用得主题
jt -t chesterish -T -N    //配置主题,chesterish是主题名
jt -r   //恢复默认主题

更详细配置参考:jupyter-themes

九、Ubuntu上面存在权限问题

修改权限

//问题1:jupyter无法访问python
sudo chmod 777 ~/.local/share/jupyter/
cd ~/.local/share/jupyter/
ls
sudo chmod 777 runtime/
cd runtime/
ls

//2.jupyter无法访问笔记本
//chown命令可以修改文件或目录所属的用户
$ sudo chown 用户 目录或文件名
//chgrp命令可以修改文件或目录所属的组
$ sudo chgrp 组 目录或文件名
  • Ubuntu上面直接在nbextensions_configurator修改不会成功,因为浏览器根本没有保存你的修改,这个问题有待解决.
  • 所以我暂时用Win10做笔记.
  • 另外,可以利用mkdoc将notebook发布到GitHub Pages上面,这里不再展开,和静态博客的搭建类似.

查看token

jupyter-notebook list

十、Welcome to MkDocs

For full documentation visit mkdocs.org.

Commands

  • mkdocs new [dir-name] - Create a new project.
  • mkdocs serve - Start the live-reloading docs server.
  • mkdocs build - Build the documentation site.
  • mkdocs help - Print this help message.

Project layout

mkdocs.yml    # The configuration file.
docs/
    index.md  # The documentation homepage.
    ...       # Other markdown pages, images and other files.

十一、Git基本命令

//建立本地仓库

//初始化本地仓库
$ git init

//添加到暂存区
$ git add .

//提交到工作区
$ git commit -m "first commit"

//添加远程Git仓库
$ git remote add origin https://github.com/ds19991999/VerCodeMFC.git

//删除远程Git仓库
$  git remote rm origin

//合并pull两个不同的项目解决fatal: refusing to merge unrelated histories
$ git pull origin master --allow-unrelated-histories

//使用强制push的方法:
$ git push -u origin master -f

补充

指定图表格式

Jupyter NotebookMatplotlib 画出来那一坨糊糊的东西会不会跟我一样浑身难受,在画图表的时候加上最后一行就行了,指定他为'svg'格式:

import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'svg'

导出md格式去掉代码

假如你的jupyter notebook是导出一个报告给业务人员看的,他们不想看到那些密密麻麻的代码,只想留下markdown和图表,在jupyter notebook加入下面这段代码就好:

import IPython.core.display as di
di.display_html('<script>jQuery(function(){if (jQuery("body.notebook_app").length == 0) { jQuery(".input_area").toggle();jQuery(".prompt").toggle();}});
                </script>', raw=True)

matplotlib显示中文

配置文件中加入:

Python27

import seaborn as sns
import sys# print sys.getdefaultencoding()# ipython notebook中默认是ascii编码 
reload(sys)
sys.setdefaultencoding('utf8')

具体参见:装扮你的Jupyter

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值