Python初学者,写一段linux 重命名文件夹URL并且储存的代码,但是出了一个问题并且百思不得其解

&figure&&img src=&https://pic3.zhimg.com/85a7b95ce836a3c094d3_b.jpg& data-rawwidth=&800& data-rawheight=&600& class=&origin_image zh-lightbox-thumb& width=&800& data-original=&https://pic3.zhimg.com/85a7b95ce836a3c094d3_r.jpg&&&/figure&&a href=&https://link.zhihu.com/?target=https%3A//github.com/amueller/word_cloud/tree/master/examples& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&word_cloud/examples at master · amueller/word_cloud · GitHub&/a&&p&上面是官方样例。&b&这一篇里的大部分尝试都基于这些样例进行修改&/b&。前提是你已经完成了安装,依照上一篇修改了 FONT_PATH 。&/p&&p&还记得 &a href=&http://zhuanlan.zhihu.com/32734& class=&internal&&&span class=&invisible&&http://&/span&&span class=&visible&&zhuanlan.zhihu.com/6666&/span&&span class=&invisible&&66/&/span&&span class=&ellipsis&&&/span&&/a& 里提到的中文分词方法吧,这次我们就不再赘述对文本的预处理了。有所不同的是,在上次的 pytagcloud 库中我们要求传入字典,而这次我们要求传入数组。所以需要做一点小小的改动。&/p&&p&上一次我们是这么写的:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&wd = {}
fp=codecs.open(&rsa.txt&, &r&,'utf-8');
alllines=fp.readlines();
fp.close();
for eachline in alllines:
line = eachline.split('
#print eachline,
wd[line[0]] = int(line[1])
&/code&&/pre&&/div&&p&这次我们需要将其中生成字典的部分变为数组:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&# -*- coding: utf-8 -*-
import codecs
fp=codecs.open(&rs300.txt&, &r&,'utf-8');
alllines=fp.readlines();
fp.close();
for eachline in alllines:
line = eachline.split('\\')
line[1]=int(line[1])
wd.append(line)
#print eachline,
&/code&&/pre&&/div&&p&文本预处理好了以后,我们就可以开始对照样例依葫芦画瓢,尝试生成词云了。在此过程中会强调一些比较重要的方法,请自行对照上一篇的文档理解。&/p&&br&&p&word_cloud 生成词云有两个方法。from text 和 from frequencies 。如果我们处理英文文档的话,就可以直接使用第一个方法,而不需要提前预处理文本,由于我们要做的是中文文本,所以我们必须自己分词,并处理为数组,使用第二种方法。&/p&&p&让我们从一个最简单的例子开始,也就是官方文档的 &a href=&https://link.zhihu.com/?target=https%3A//github.com/amueller/word_cloud/blob/master/examples/simple.py& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&simple.py&/a&&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span& -*- coding: utf-8 -*-
Minimal Example
===============
Generating a square wordcloud from the US constitution using default arguments.
from os import path
from wordcloud import WordCloud
d = path.dirname(__file__)
# Read the whole text.
此处原为处理英文文本,我们修改为传入中文数组
#text = open(path.join(d, 'constitution.txt')).read()
frequencies = [(u'知乎',5),(u'小段同学',4),(u'曲小花',3),(u'中文分词',2),(u'样例',1)]
# Generate a word cloud image 此处原为 text 方法,我们改用 frequencies
#wordcloud = WordCloud().generate(text)
wordcloud = WordCloud().fit_words(frequencies)
# Display the generated image:
# the matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud)
plt.axis(&off&)
# take relative word frequencies into account, lower max_font_size
#wordcloud = WordCloud(max_font_size=40, relative_scaling=.5).generate(text)
wordcloud = WordCloud(max_font_size=40, relative_scaling=.5).fit_words(frequencies)
plt.figure()
plt.imshow(wordcloud)
plt.axis(&off&)
plt.show()
# The pil way (if you don't have matplotlib)
#image = wordcloud.to_image()
#image.show()
&/code&&/pre&&/div&&p&运行代码得到结果:&/p&&p&&figure&&img src=&https://pic1.zhimg.com/8e99c3e9b9da3a9a83bc4c_b.jpg& data-rawwidth=&1228& data-rawheight=&552& class=&origin_image zh-lightbox-thumb& width=&1228& data-original=&https://pic1.zhimg.com/8e99c3e9b9da3a9a83bc4c_r.jpg&&&/figure&长的很像我们在第一篇中输出的结果。但是我们可以注意到,上次的结果如右图,词语间是不会发生嵌套的,而这次输出的左图,字间空隙也可以插入词语了。&/p&&p&这一步成功以后起码说明我们中文环境配好了,可以开始更深层次的尝试了。&/p&&p&&b&在下面的演示中,我为了节省力气,会直接使用一些英文的文档,这样省去处理中文分词的步骤。&/b&&/p&&p&&b&大家在用的时候只要记得修改 FONT_PATH 、 utf-8 编码、处理数据为数组格式使用 frequencies 方法这三点就可以了。&/b&&/p&&p&接下来我们来到 &a href=&https://link.zhihu.com/?target=https%3A//github.com/amueller/word_cloud/blob/master/examples/masked.py& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&masked.py&/a&&/p&&p&如文件名所示,这一次我们要自定义遮罩形状来生成真正的词云了!&/p&&p&根据文档说明,遮罩图片的白色部分将被视作透明,只在非白色部分区域作图。于是我们找到一张黑白素材图。&/p&&p&&figure&&img src=&https://pic4.zhimg.com/f4f846691ade18c2bfea80b89051cb5d_b.jpg& data-rawwidth=&3117& data-rawheight=&4142& class=&origin_image zh-lightbox-thumb& width=&3117& data-original=&https://pic4.zhimg.com/f4f846691ade18c2bfea80b89051cb5d_r.jpg&&&/figure&然后对这张图进行处理。&br&&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&# -*- coding: utf-8 -*-
Masked wordcloud
================
Using a mask you can generate wordclouds in arbitrary shapes.
from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS
d = path.dirname(__file__)
# Read the whole text.
text = open(path.join(d, 'alice.txt')).read()
# read the mask image
# taken from
# http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg
alice_mask = np.array(Image.open(path.join(d, &huge.jpg&)))
wc = WordCloud(background_color=&white&, max_words=2000, mask=alice_mask,
stopwords=STOPWORDS.add(&said&))
# generate word cloud
wc.generate(text)
# store to file
wc.to_file(path.join(d, &alice.png&))
plt.imshow(wc)
plt.axis(&off&)
plt.figure()
plt.imshow(alice_mask, cmap=plt.cm.gray)
plt.axis(&off&)
plt.show()
&/code&&/pre&&/div&&figure&&img src=&https://pic1.zhimg.com/0f3d740be80e14b54b8ef_b.jpg& data-rawwidth=&870& data-rawheight=&548& class=&origin_image zh-lightbox-thumb& width=&870& data-original=&https://pic1.zhimg.com/0f3d740be80e14b54b8ef_r.jpg&&&/figure&&figure&&img src=&https://pic4.zhimg.com/18edfdc732febbae9faa5a_b.jpg& data-rawwidth=&800& data-rawheight=&600& class=&origin_image zh-lightbox-thumb& width=&800& data-original=&https://pic4.zhimg.com/18edfdc732febbae9faa5a_r.jpg&&&/figure&&p&图做好了,可是总觉得哪里怪怪的。可能是颜色太过鲜艳吧,配色总感觉不是那么舒服,有没有更逼格的处理?自然是有的。我们来到 &a href=&https://link.zhihu.com/?target=https%3A//github.com/amueller/word_cloud/blob/master/examples/a_new_hope.py& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&a_new_hope.py&/a&&br&&/p&&p&还是上面的图,直接处理。&br&&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&&&&
Using custom colors
====================
Using the recolor method and custom coloring functions.
import numpy as np
from PIL import Image
from os import path
import matplotlib.pyplot as plt
import random
from wordcloud import WordCloud, STOPWORDS
def grey_color_func(word, font_size, position, orientation, random_state=None, **kwargs):
return &hsl(0, 0%%, %d%%)& % random.randint(60, 100)
d = path.dirname(__file__)
# read the mask image
# taken from
# http://www.stencilry.org/stencils/movies/star%20wars/storm-trooper.gif
mask = np.array(Image.open(path.join(d, &huge.jpg&)))
# movie script of &a new hope&
# http://www.imsdb.com/scripts/Star-Wars-A-New-Hope.html
# May the lawyers deem this fair use.
text = open(&a_new_hope.txt&).read()
# preprocessing the text a little bit
text = text.replace(&HAN&, &Han&)
text = text.replace(&LUKE'S&, &Luke&)
# adding movie script specific stopwords
stopwords = STOPWORDS.copy()
stopwords.add(&int&)
stopwords.add(&ext&)
wc = WordCloud(max_words=1000, mask=mask, stopwords=stopwords, margin=10,
random_state=1).generate(text)
# store default colored image
default_colors = wc.to_array()
plt.title(&Custom colors&)
plt.imshow(wc.recolor(color_func=grey_color_func, random_state=3))
wc.to_file(&a_new_hope.png&)
plt.axis(&off&)
plt.figure()
plt.title(&Default colors&)
plt.imshow(default_colors)
plt.axis(&off&)
plt.show()
&/code&&/pre&&/div&&figure&&img src=&https://pic3.zhimg.com/31b762627bef_b.jpg& data-rawwidth=&863& data-rawheight=&546& class=&origin_image zh-lightbox-thumb& width=&863& data-original=&https://pic3.zhimg.com/31b762627bef_r.jpg&&&/figure&&figure&&img src=&https://pic4.zhimg.com/aa3f11cd206b384a7b10d2eebf0c52d9_b.jpg& data-rawwidth=&596& data-rawheight=&600& class=&origin_image zh-lightbox-thumb& width=&596& data-original=&https://pic4.zhimg.com/aa3f11cd206b384a7b10d2eebf0c52d9_r.jpg&&&/figure&&p&这样的灰调颜色比上面花花绿绿的感觉好多了。可是总不能老用黑白啊,需要色彩的时候怎么办?最后一站我们来到 &a href=&https://link.zhihu.com/?target=https%3A//github.com/amueller/word_cloud/blob/master/examples/colored.py& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&colored.py&/a&&/p&&p&为了突出我们鲜明且令人舒服的色调,我挑选了一幅小黄人的图片进行处理。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&#!/usr/bin/env python2
Image-colored wordcloud
========================
You can color a word-cloud by using an image-based coloring strategy implemented in
ImageColorGenerator. It uses the average color of the region occupied by the word
in a source image. You can combine this with masking - pure-white will be interpreted
as 'don't occupy' by the WordCloud object when passed as mask.
If you want white as a legal color, you can just pass a different image to &mask&,
but make sure the image shapes line up.
from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
d = path.dirname(__file__)
# Read the whole text.
text = open(path.join(d, 'alice.txt')).read()
# read the mask / color image
# taken from http://jirkavinse.deviantart.com/art/quot-Real-Life-quot-Alice-
alice_coloring = np.array(Image.open(path.join(d, &xhr.jpg&)))
wc = WordCloud(background_color=&white&, max_words=2000, mask=alice_coloring,
stopwords=STOPWORDS.add(&said&),
max_font_size=40, random_state=42)
# generate word cloud
wc.generate(text)
# create coloring from image
image_colors = ImageColorGenerator(alice_coloring)
plt.imshow(wc)
plt.axis(&off&)
plt.figure()
# recolor wordcloud and show
# we could also give color_func=image_colors directly in the constructor
plt.imshow(wc.recolor(color_func=image_colors))
plt.axis(&off&)
plt.figure()
plt.imshow(alice_coloring, cmap=plt.cm.gray)
plt.axis(&off&)
plt.show()
&/code&&/pre&&/div&&p&&figure&&img src=&https://pic2.zhimg.com/2e44c4f1d2d8a0f9bd322_b.jpg& data-rawwidth=&983& data-rawheight=&553& class=&origin_image zh-lightbox-thumb& width=&983& data-original=&https://pic2.zhimg.com/2e44c4f1d2d8a0f9bd322_r.jpg&&&/figure&效果还可以,但是和我想的…不太一样…我们尝试调整一些参数。……当然,最佳效果可能需要很多次的尝试才会出现。比如我将字号调大以后就会出现一些……奇怪的输出,难道是因为白色白的不够纯净吗。&br&&figure&&img src=&https://pic4.zhimg.com/787b27aa83c0f4a914dd3e5a5783ce6c_b.jpg& data-rawwidth=&887& data-rawheight=&552& class=&origin_image zh-lightbox-thumb& width=&887& data-original=&https://pic4.zhimg.com/787b27aa83c0f4a914dd3e5a5783ce6c_r.jpg&&&/figure&&figure&&img src=&https://pic4.zhimg.com/90a3dae93de93c88c7a7df12d6106bc3_b.jpg& data-rawwidth=&598& data-rawheight=&600& class=&origin_image zh-lightbox-thumb& width=&598& data-original=&https://pic4.zhimg.com/90a3dae93de93c88c7a7df12d6106bc3_r.jpg&&&/figure&总体上讲这样的配色已经比上面那种花花绿绿的配色好多了…至于为什么白色区域还会有词语,我刚看了一下小黄人的原图,白色区域的确不完全是 255,255,255 ,应该是这个原因。&/p&&br&&p&好了,到这里我们四幅样例就都做完了。最后我发现缺少一张封面图,那就再多来一张,顺便尝试一下 scale 参数。&/p&&p&我们尝试在如下位置加入 scale 参数,设置为 1.5 倍。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&wc = WordCloud(background_color=&white&, max_words=2000, mask=alice_coloring,
stopwords=STOPWORDS.add(&said&),
max_font_size=40, random_state=42,scale =1.5 )
&/code&&/pre&&/div&&p&&figure&&img src=&https://pic3.zhimg.com/eefbecbd57bc23e6ee2d_b.jpg& data-rawwidth=&656& data-rawheight=&545& class=&origin_image zh-lightbox-thumb& width=&656& data-original=&https://pic3.zhimg.com/eefbecbd57bc23e6ee2d_r.jpg&&&/figure&&figure&&img src=&https://pic3.zhimg.com/22897feb500f239feeeda_b.jpg& data-rawwidth=&656& data-rawheight=&553& class=&origin_image zh-lightbox-thumb& width=&656& data-original=&https://pic3.zhimg.com/22897feb500f239feeeda_r.jpg&&&/figure&可以看到输出的图片的确是原图的 2 倍。&/p&&br&&br&&p&再次强调使用 word_cloud 的几个要点吧:&/p&&p&1、FONT_PATH&/p&&p&2、中文要实现分词,输出为数组,使用 frequencies 方法&/p&&p&3、scale 的用法,由于程序运行时间较长,如果生成大幅图片会很慢,可以使用 scale 调节大小,但是会牺牲词语对形状的拟合度&/p&&br&&br&&p&到此标签云这一系列就能告一段落了。我们由中文分词引入,为了可视化展示,试用了两个标签云生成器。下面简单总结比较一下 pytagcloud 和 word_cloud 的优点:&/p&&p&pytagcloud :&/p&&p&优点:依赖较少;可以同时添加多个字体,在代码中选择;提供相对完善的配色方案。&/p&&p&word_cloud:&/p&&p&优点:文字空隙可以嵌套词语;支持自定义词云形状;支持通过图片上色。&/p&&p&缺点:依赖较多;程序运行较慢;默认颜色花花绿绿实在是丑…&/p&&br&&br&&p&最后谈一下对做标签云的看法:&/p&&p&分词、取词是关键和基础。图片毕竟只是一个可视化的展示手段,内容更重要。分词前要想清楚是否屏蔽某些词性( jieba 提供这种功能,但是学习的还不透彻,以后可以专门记录一些分词的东西),分词以后也可以手动删除一些词语,使重点突出。&/p&&p&遮罩图片的选择很重要。首先需要是大面积白底,否则会整张图片铺满词语。其次,图片的颜色要丰富,最好能有渐变度,这样的配色会很舒服。但是符合条件的图片实在是太难找了…可遇而不可求。&/p&&p&最后,完美的图片需要很多次地微调参数。词语的数量,字体的大小等,都会对整张图片的效果和词云形状的拟合度产生影响。&/p&&br&&p&这些只是对现有工具的简单学习应用,并没有什么创新与创造。&/p&&p&希望这几篇文章能对感兴趣的人有所帮助。&/p&&p&#这一篇主要是对官方样例的重复,比较简单,代码也是现成的。图片可以根据喜好自己去找,就不提供这次用到的文件了。&/p&
上面是官方样例。这一篇里的大部分尝试都基于这些样例进行修改。前提是你已经完成了安装,依照上一篇修改了 FONT_PATH 。还记得
里提到的中文分词方法吧,这次我们就…
&figure&&img src=&https://pic3.zhimg.com/v2-f9fe566bac65ff3f44c184af_b.jpg& data-rawwidth=&1920& data-rawheight=&1280& class=&origin_image zh-lightbox-thumb& width=&1920& data-original=&https://pic3.zhimg.com/v2-f9fe566bac65ff3f44c184af_r.jpg&&&/figure&&p&&/p&&figure&&img src=&https://pic1.zhimg.com/v2-ffad61af265d2f2fa0becd_b.jpg& data-size=&normal& data-rawwidth=&10836& data-rawheight=&7264& class=&origin_image zh-lightbox-thumb& width=&10836& data-original=&https://pic1.zhimg.com/v2-ffad61af265d2f2fa0becd_r.jpg&&&figcaption&目录&/figcaption&&/figure&&hr&&h2&&b&一、什么是Jupyter Notebook?&/b&&/h2&&h2&&b&1. 简介&/b&&/h2&&blockquote&Jupyter Notebook是基于网页的用于交互计算的应用程序。其可被应用于全过程计算:开发、文档编写、运行代码和展示结果。——&a href=&http://link.zhihu.com/?target=https%3A//jupyter-notebook.readthedocs.io/en/stable/notebook.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Jupyter Notebook官方介绍&/a&&/blockquote&&p&简而言之,Jupyter Notebook是以网页的形式打开,可以在网页页面中&b&直接编写代码&/b&和&b&运行代码&/b&,代码的&b&运行结果&/b&也会直接在代码块下显示的程序。如在编程过程中需要编写说明文档,可在同一个页面中直接编写,便于作及时的说明和解释。&/p&&h2&&b&2. 组成部分&/b&&/h2&&h2&&b&① 网页应用&/b&&/h2&&p&网页应用即基于网页形式的、结合了编写说明文档、数学公式、交互计算和其他富媒体形式的工具。&b&简言之,网页应用是可以实现各种功能的工具。&/b&&/p&&h2&&b&② 文档&/b&&/h2&&p&即Jupyter Notebook中所有交互计算、编写说明文档、数学公式、图片以及其他富媒体形式的输入和输出,都是以文档的形式体现的。&/p&&p&这些文档是保存为后缀名为&code&.ipynb&/code&的&code&JSON&/code&格式文件,不仅便于版本控制,也方便与他人共享。&/p&&p&此外,文档还可以导出为:HTML、LaTeX、PDF等格式。&/p&&h2&&b&3. Jupyter Notebook的主要特点&/b&&/h2&&p&① 编程时具有&b&语法高亮&/b&、&i&缩进&/i&、&i&tab补全&/i&的功能。&/p&&p&② 可直接通过浏览器运行代码,同时在代码块下方展示运行结果。&/p&&p&③ 以富媒体格式展示计算结果。富媒体格式包括:HTML,LaTeX,PNG,SVG等。&/p&&p&④ 对代码编写说明文档或语句时,支持Markdown语法。&/p&&p&⑤ 支持使用LaTeX编写数学性说明。&/p&&h2&&b&二、安装Jupyter Notebook&/b&&/h2&&h2&&b&0. 先试用,再决定&/b&&/h2&&p&如果看了以上对Jupyter Notebook的介绍你还是拿不定主意究竟是否适合你,那么不要担心,你可以先&b&免安装试用体验&/b&一下,&a href=&http://link.zhihu.com/?target=https%3A//try.jupyter.org/& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&戳这里&/a&,然后再做决定。&/p&&p&值得注意的是,官方提供的同时试用是有限的,如果你点击链接之后进入的页面如下图所示,那么不要着急,过会儿再试试看吧。 &/p&&figure&&img src=&https://pic3.zhimg.com/v2-b33ebefcafdf942_b.jpg& data-size=&normal& data-rawwidth=&1436& data-rawheight=&618& class=&origin_image zh-lightbox-thumb& width=&1436& data-original=&https://pic3.zhimg.com/v2-b33ebefcafdf942_r.jpg&&&figcaption&试用满线&/figcaption&&/figure&&p&如果你足够幸运,那么你将看到如下界面,就可以开始体验啦。&/p&&figure&&img src=&https://pic3.zhimg.com/v2-d07ddde1d96cd3a71a5ecffe_b.jpg& data-size=&normal& data-rawwidth=&2332& data-rawheight=&1378& class=&origin_image zh-lightbox-thumb& width=&2332& data-original=&https://pic3.zhimg.com/v2-d07ddde1d96cd3a71a5ecffe_r.jpg&&&figcaption&主界面&/figcaption&&/figure&&figure&&img src=&https://pic2.zhimg.com/v2-8fa489d28f5c0aa29e78dc_b.jpg& data-size=&normal& data-rawwidth=&2004& data-rawheight=&1774& class=&origin_image zh-lightbox-thumb& width=&2004& data-original=&https://pic2.zhimg.com/v2-8fa489d28f5c0aa29e78dc_r.jpg&&&figcaption&编辑页面&/figcaption&&/figure&&h2&&b&1. 安装&/b&&/h2&&h2&&b&① 安装前提&/b&&/h2&&p&安装Jupyter Notebook的前提是需要安装了Python(3.3版本及以上,或2.7版本)。&/p&&h2&&b&② 使用Anaconda安装&/b&&/h2&&p&如果你是小白,那么建议你通过安装Anaconda来解决Jupyter Notebook的安装问题,因为Anaconda已经自动为你安装了Jupter Notebook及其他工具,还有python中超过180个科学包及其依赖项。&/p&&p&你可以通过进入Anaconda的&a href=&http://link.zhihu.com/?target=https%3A//www.anaconda.com/download/%23macos& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&官方下载页面&/a&自行选择下载;如果你对阅读&b&英文文档&/b&感到头痛,或者对&b&安装步骤&/b&一无所知,甚至也想快速了解一下&b&什么是Anaconda&/b&,那么可以前往我的另一篇文章:&/p&&a href=&https://zhuanlan.zhihu.com/p/& data-draft-node=&block& data-draft-type=&link-card& data-image=&https://pic2.zhimg.com/v2-4ec3ac92a0051720dbf20f626dd44fb1_180x120.jpg& data-image-width=&1000& data-image-height=&750& class=&internal&&豆豆:Anaconda介绍、安装及使用教程&/a&&p&你想要的,都在里面!&/p&&p&常规来说,安装了Anaconda发行版时已经自动为你安装了Jupyter Notebook的,但如果没有自动安装,那么就在终端(Linux或macOS的“终端”,Windows的“Anaconda Prompt”,以下均简称“终端”)中输入以下命令安装:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&conda install jupyter notebook
&/code&&/pre&&/div&&h2&&b&③ 使用pip命令安装&/b&&/h2&&p&如果你是有经验的Python玩家,想要尝试用pip命令来安装Jupyter&br&Notebook,那么请看以下步骤吧!接下来的命令都输入在终端当中的噢!&/p&&p&&b&1. 把pip升级到最新版本&/b&&/p&&ul&&li&Python 3.x&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&pip3 install --upgrade pip
&/code&&/pre&&/div&&ul&&li&Python 2.x&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&pip install --upgrade pip
&/code&&/pre&&/div&&ul&&li&注意:老版本的pip在安装Jupyter Notebook过程中或面临依赖项无法同步安装的问题。因此&b&强烈建议&/b&先把pip升级到最新版本。&/li&&/ul&&p&&b&2. 安装Jupyter Notebook&/b&&/p&&ul&&li&Python 3.x&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&pip3 install jupyter
&/code&&/pre&&/div&&ul&&li&Python 2.x&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&pip install jupyter
&/code&&/pre&&/div&&h2&&b&三、运行Jupyter Notebook&/b&&/h2&&h2&&b&0. 帮助&/b&&/h2&&p&如果你有任何jupyter notebook命令的疑问,可以考虑查看官方帮助文档,命令如下:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&jupyter notebook --help
&/code&&/pre&&/div&&p&或&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&jupyter notebook -h
&/code&&/pre&&/div&&h2&&b&1. 启动&/b&&/h2&&h2&&b&① 默认端口启动&/b&&/h2&&p&在终端中输入以下命令:&/p&&div class=&highlight&&&pre&&code class=&language-bash&&&span&&/span&jupyter notebook
&/code&&/pre&&/div&&p&执行命令之后,在终端中将会显示一系列notebook的服务器信息,同时浏览器将会自动启动Jupyter Notebook。&/p&&p&启动过程中终端显示内容如下:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&$ jupyter notebook
[I 08:58:24.417 NotebookApp] Serving notebooks from local directory: /Users/catherine
[I 08:58:24.417 NotebookApp] 0 active kernels
[I 08:58:24.417 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/
[I 08:58:24.417 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
&/code&&/pre&&/div&&ul&&li&注意:之后在Jupyter Notebook的所有操作,都请保持终端&b&不要关闭&/b&,因为一旦关闭终端,就会断开与本地服务器的链接,你将无法在Jupyter Notebook中进行其他操作啦。&/li&&/ul&&p&浏览器地址栏中默认地将会显示:&code&http://localhost:8888&/code&。其中,“localhost”指的是本机,“8888”则是端口号。 &/p&&figure&&img src=&https://pic3.zhimg.com/v2-3e14e522a1e_b.jpg& data-size=&normal& data-rawwidth=&1439& data-rawheight=&226& class=&origin_image zh-lightbox-thumb& width=&1439& data-original=&https://pic3.zhimg.com/v2-3e14e522a1e_r.jpg&&&figcaption&URL&/figcaption&&/figure&&p&如果你&b&同时&/b&启动了多个Jupyter Notebook,由于默认端口“8888”被占用,因此地址栏中的数字将从“8888”起,每多启动一个Jupyter Notebook数字就加1,如“8889”、“8890”……&/p&&h2&&b&② 指定端口启动&/b&&/h2&&p&如果你想自定义端口号来启动Jupyter Notebook,可以在终端中输入以下命令:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&jupyter notebook --port &port_number&
&/code&&/pre&&/div&&p&其中,“&port_number&”是自定义端口号,直接以数字的形式写在命令当中,数字两边不加尖括号“&&”。如:&code&jupyter notebook --port 9999&/code&,即在端口号为“9999”的服务器启动Jupyter Notebook。&/p&&h2&&b&③ 启动服务器但不打开浏览器&/b&&/h2&&p&如果你只是想启动Jupyter Notebook的服务器但不打算立刻进入到主页面,那么就无需立刻启动浏览器。在终端中输入:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&jupyter notebook --no-browser
&/code&&/pre&&/div&&p&此时,将会在终端显示启动的服务器信息,并在服务器启动之后,显示出打开浏览器页面的链接。当你需要启动浏览器页面时,只需要复制链接,并粘贴在浏览器的地址栏中,轻按回车变转到了你的Jupyter Notebook页面。 &/p&&figure&&img src=&https://pic3.zhimg.com/v2-e43d4ba2c2a8c2cdcb6da3afeb0e1bca_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&894& data-rawheight=&567& class=&origin_image zh-lightbox-thumb& width=&894& data-original=&https://pic3.zhimg.com/v2-e43d4ba2c2a8c2cdcb6da3afeb0e1bca_r.jpg&&&/figure&&p&例图中由于在完成上面内容时我同时启动了多个Jupyter Notebook,因此显示我的“8888”端口号被占用,最终分配给我的是“8889”。&/p&&h2&&b&2. 主页面&/b&&/h2&&h2&&b&① 主页面内容&/b&&/h2&&p&当执行完启动命令之后,浏览器将会进入到Notebook的主页面,如下图所示。&/p&&figure&&img src=&https://pic2.zhimg.com/v2-358ef24acd0c0bdf93ce5_b.jpg& data-size=&normal& data-rawwidth=&1440& data-rawheight=&789& class=&origin_image zh-lightbox-thumb& width=&1440& data-original=&https://pic2.zhimg.com/v2-358ef24acd0c0bdf93ce5_r.jpg&&&figcaption&默认主页面&/figcaption&&/figure&&p&如果你的主页面里边的文件夹跟我的不同,或者你在疑惑为什么首次启动里边就已经有这么多文件夹,不要担心,这里边的文件夹全都是你的家目录里的目录文件。你可以在终端中执行以下2步来查看:&/p&&p&① &code&cd&/code& 或 &code&cd -&/code& 或 &code&cd ~&/code& 或&code&cd /Users/&user_name&&/code&&/p&&ul&&li&这个命令将会进入你的家目录。&/li&&li&“&user_name&” 是用户名。用户名两边不加尖括号“&&”。&/li&&/ul&&p&② &code&ls&/code& &/p&&ul&&li&这个命令将会展示你家目录下的文件。&/li&&/ul&&h2&&b&② 设置Jupyter Notebook文件存放位置&/b&&/h2&&p&如果你不想把今后在Jupyter Notebook中编写的所有文档都直接保存在家目录下,那你需要修改Jupyter Notebook的文件存放路径。&/p&&h2&&b&⑴ 创建文件夹/目录&/b&&/h2&&ul&&li&Windows用户在想要存放Jupyter Notebook文件的&b&磁盘&/b&中&b&新建文件夹&/b&并为该文件夹命名;双击进入该文件夹,然后复制地址栏中的路径。&/li&&li&Linux/macOS用户在想要存放Jupyter Notebook文件的位置&b&创建目录&/b&并为目录命名,命令为:&code&mkdir &directory_name&&/code&;进入目录,命令为:&code&cd &directory_name&&/code&;查看目录的路径,命令为:&code&pwd&/code&;复制该路径。&/li&&li&注意:“&directory_name&”是自定义的目录名。目录名两边不加尖括号“&&”。&/li&&/ul&&h2&&b&⑵ 配置文件路径&/b&&/h2&&ul&&li&一个便捷获取配置文件所在路径的命令:&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&jupyter notebook --generate-config
&/code&&/pre&&/div&&ul&&li&注意: 这条命令虽然可以用于查看配置文件所在的路径,但主要用途是是否将这个路径下的配置文件&b&替换&/b&为&b&默认配置文件&/b&。 如果你是第一次查询,那么&b&或许&/b&不会出现下图的提示;若文件已经存在或被修改,使用这个命令之后会出现询问“Overwrite /Users/raxxie/.jupyter/jupyter_notebook_config.py with default config? [y/N]”,即“用默认配置文件覆盖此路径下的文件吗?”,如果按“y”,则完成覆盖,那么之前所做的修改都将失效;如果只是为了查询路径,那么一定要输入“N”。&/li&&/ul&&figure&&img src=&https://pic4.zhimg.com/v2-da0d33d371c00fe08985cbce2b23bcc7_b.jpg& data-size=&normal& data-rawwidth=&719& data-rawheight=&112& class=&origin_image zh-lightbox-thumb& width=&719& data-original=&https://pic4.zhimg.com/v2-da0d33d371c00fe08985cbce2b23bcc7_r.jpg&&&figcaption&配置文件所在路径&/figcaption&&/figure&&p&常规的情况下,Windows和Linux/macOS的配置文件所在路径和配置文件名如下所述:&/p&&ul&&li&Windows系统的配置文件路径:&code&C:\Users\&user_name&\.jupyter\&/code&&/li&&li&Linux/macOS系统的配置文件路径:&code&/Users/&user_name&/.jupyter/&/code& 或 &code&~/.jupyter/&/code&&/li&&li&配置文件名:&code&jupyter_notebook_config.py&/code&&/li&&/ul&&p&注意:&/p&&p&① “&user_name&”为你的用户名。用户名两边不加尖括号“&&”。&/p&&p&② Windows和Linux/macOS系统的配置文件存放路径其实是相同的,只是系统不同,表现形式有所不同而已。&/p&&p&③ Windows和Linux/macOS系统的配置文件也是相同的。文件名以“.py”结尾,是Python的可执行文件。&/p&&p&④ 如果你不是通过一步到位的方式前往配置文件所在位置,而是一层一层进入文件夹/目录的,那么当你进入家目录后,用&code&ls&/code&命令会发现找不到“.jupyter”文件夹/目录。这是因为凡是以“.”开头的目录都是隐藏文件,你可以通过&code&ls -a&/code&命令查看当前位置下所有的隐藏文件。&/p&&h2&&b&⑶ 修改配置文件&/b&&/h2&&ul&&li&Windows系统的用户可以使用文档编辑工具或IDE打开“jupyter_notebook_config.py”文件并进行编辑。常用的文档编辑工具和IDE有记事本、Notepad++、vim、Sublime&br&Text、PyCharm等。其中,vim是没有图形界面的,是一款学习曲线较为陡峭的编辑器,其他工具在此不做使用说明,因为上手相对简单。通过vim修改配置文件的方法请继续往下阅读。&/li&&li&Linux/macOS系统的用户建议直接通过终端调用vim来对配置文件进行修改。具体操作步骤如下:&/li&&/ul&&h2&&b&(a) 打开配置文件&/b&&/h2&&p&打开终端,输入命令:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&vim ~/.jupyter/jupyter_notebook_config.py
&/code&&/pre&&/div&&figure&&img src=&https://pic3.zhimg.com/v2-ec79adec540c62b244bd44df41f6c0ae_b.jpg& data-size=&normal& data-rawwidth=&811& data-rawheight=&93& class=&origin_image zh-lightbox-thumb& width=&811& data-original=&https://pic3.zhimg.com/v2-ec79adec540c62b244bd44df41f6c0ae_r.jpg&&&figcaption&命令详解&/figcaption&&/figure&&p&执行上述命令后便进入到配置文件当中了。&/p&&h2&&b&(b) 查找关键词&/b&&/h2&&p&进入配置文件后查找关键词“c.NotebookApp.notebook_dir”。查找方法如下:&/p&&p&进入配置文件后不要按其他键,用&b&英文半角&/b&直接输入&code&/c.NotebookApp.notebook_dir&/code&,这时搜索的关键词已在文档中高亮显示了,按回车,光标从底部切换到文档正文中被查找关键词的首字母。&/p&&h2&&b&(c) 编辑配置文件&/b&&/h2&&p&按&b&小写i&/b&进入编辑模式,底部出现“--INSERT--”说明成功进入编辑模式。使用方向键把光标定位在第二个单引号上(光标定位在哪个字符,就在这个字符前开始输入),把“⑴ 创建文件夹/目录”步骤中复制的路径粘贴在此处。&/p&&h2&&b&(d) 取消注释&/b&&/h2&&p&把该行行首的&b&井号(#)&/b&删除。因为配置文件是Python的可执行文件,在Python中,井号(#)表示注释,即在编译过程中不会执行该行命令,所以为了使修改生效,需要删除井号(#)。 &/p&&h2&&b&(e) 保存配置文件&/b&&/h2&&p&先按&code&esc&/code&键,从编辑模式退出,回到命令模式。&/p&&p&再用&b&英文半角&/b&直接输入&code&:wq&/code&,回车即成功保存且退出了配置文件。&/p&&p&注意:&/p&&ul&&li&&b&冒号(:)&/b& 一定要有,且也是&b&英文半角&/b&。&/li&&li&w:保存。&/li&&li&q:退出。&/li&&/ul&&h2&&b&(f) 验证&/b&&/h2&&p&在终端中输入命令&code&jupyter notebook&/code&打开Jupyter Notebook,此时你会看到一个清爽的界面,恭喜! &/p&&figure&&img src=&https://pic1.zhimg.com/v2-8d185cb051aea662a829e88ba486dbc4_b.jpg& data-size=&normal& data-rawwidth=&1170& data-rawheight=&391& class=&origin_image zh-lightbox-thumb& width=&1170& data-original=&https://pic1.zhimg.com/v2-8d185cb051aea662a829e88ba486dbc4_r.jpg&&&figcaption&配置后主页面&/figcaption&&/figure&&h2&&b&(g) 注意&/b&&/h2&&ul&&li&以上所有命令均以&b&英文半角&/b&格式输入,若有报错,请严格检查这两个条件,&b&英文&/b&且&b&半角&/b&。&/li&&li&这里仅介绍了vim编辑器修改配置文件的方法,没有对vim编辑器的详细使用进行讲解,所以无需了解vim编辑器的具体使用方法,只需要按照上述步骤一定可以顺利完成修改!&/li&&li&推荐有时间和经历时学习一下vim编辑器的使用。这款强大的编辑器将会成为你未来工作中的利器。&/li&&/ul&&h2&&b&四、Jupyter Notebook的基本使用&/b&&/h2&&h2&&b&1. Files页面&/b&&/h2&&figure&&img src=&https://pic1.zhimg.com/v2-cb526cea874c6bc44c70_b.jpg& data-size=&normal& data-rawwidth=&1437& data-rawheight=&451& class=&origin_image zh-lightbox-thumb& width=&1437& data-original=&https://pic1.zhimg.com/v2-cb526cea874c6bc44c70_r.jpg&&&figcaption&Files页面&/figcaption&&/figure&&p&此时你的界面当中应该还没有“Conda”和“Nbextensions”类目。不要着急,这两个类目将分别在“五、拓展功能”中的“&a href=&https://zhuanlan.zhihu.com/p//edit#conda& class=&internal&&1.关联Jupyter Notebook和conda的环境和包——‘nb_conda’&/a&”和“&a href=&https://zhuanlan.zhihu.com/p//edit#nbextensions& class=&internal&&2.Markdown生成目录&/a&”中安装。&/p&&p&Files页面是用于管理和创建文件相关的类目。&/p&&p&对于现有的文件,可以通过勾选文件的方式,对选中文件进行复制、重命名、移动、下载、查看、编辑和删除的操作。&/p&&p&同时,也可以根据需要,在“New”下拉列表中选择想要创建文件的环境,进行创建“ipynb”格式的笔记本、“txt”格式的文档、终端或文件夹。如果你创建的环境没有在下拉列表中显示,那么你需要依次前往“五、拓展功能”中的“&a href=&https://zhuanlan.zhihu.com/p//edit#conda& class=&internal&&1.关联Jupyter Notebook和conda的环境和包——‘nb_conda’&/a&”和“&a href=&https://zhuanlan.zhihu.com/p//edit#ipykernel& class=&internal&&六、增加内核——‘ipykernel’&/a&”中解决该问题。&/p&&h2&&b&① 笔记本的基本操作&/b&&/h2&&figure&&img src=&https://pic1.zhimg.com/v2-3c250eeed80d456487cc_b.jpg& data-size=&normal& data-rawwidth=&1440& data-rawheight=&424& class=&origin_image zh-lightbox-thumb& width=&1440& data-original=&https://pic1.zhimg.com/v2-3c250eeed80d456487cc_r.jpg&&&figcaption&笔记本页面注解图&/figcaption&&/figure&&p&上图展示的是笔记本的基本结构和功能。根据图中的注解已经可以解决绝大多数的使用问题了!&/p&&p&工具栏的使用如图中的注解一样直观,在此不过多解释。需要特别说明的是“单元格的状态”,有Code,Markdown,Heading,Raw NBconvert。其中,最常用的是前两个,分别是代码状态,Markdown编写状态。Jupyter Notebook已经取消了Heading状态,即标题单元格。取而代之的是Markdown的一级至六级标题。而Raw NBconvert目前极少用到,此处也不做过多讲解。&/p&&p&菜单栏涵盖了笔记本的所有功能,即便是工具栏的功能,也都可以在菜单栏的类目里找到。然而,并不是所有功能都是常用的,比如Widgets,Navigate。Kernel类目的使用,主要是对内核的操作,比如中断、重启、连接、关闭、切换内核等,由于我们在创建笔记本时已经选择了内核,因此切换内核的操作便于我们在使用笔记本时切换到我们想要的内核环境中去。由于其他的功能相对比较常规,根据图中的注解来尝试使用笔记本的功能已经非常便捷,因此不再做详细讲解。&/p&&h2&&b&② 笔记本重命名的两种方式&/b&&/h2&&h2&&b&⑴ 笔记本内部重命名&/b&&/h2&&p&在使用笔记本时,可以直接在其内部进行重命名。在左上方“Jupyter”的图标旁有程序默认的标题“Untitled”,点击“Untitled”然后在弹出的对话框中输入自拟的标题,点击“Rename”即完成了重命名。&/p&&h2&&b&⑵ 笔记本外部重命名&/b&&/h2&&p&若在使用笔记本时忘记了重命名,且已经保存并退出至“Files”界面,则在“Files”界面勾选需要重命名的文件,点击“Rename”然后直接输入自拟的标题即可。&/p&&h2&&b&⑶ 演示&/b&&/h2&&figure&&img src=&https://pic4.zhimg.com/v2-ece3abb50a4_b.jpg& data-size=&normal& data-rawwidth=&1438& data-rawheight=&567& data-thumbnail=&https://pic1.zhimg.com/v2-ece3abb50a4_b.jpg& class=&origin_image zh-lightbox-thumb& width=&1438& data-original=&https://pic1.zhimg.com/v2-ece3abb50a4_r.jpg&&&figcaption&笔记本重命名演示图&/figcaption&&/figure&&p&&br&&/p&&p&&br&&/p&&h2&&b&2. Running页面&/b&&/h2&&p&Running页面主要展示的是当前正在运行当中的终端和“ipynb”格式的笔记本。若想要关闭已经打开的终端和“ipynb”格式的笔记本,仅仅关闭其页面是无法彻底退出程序的,需要在Running页面点击其对应的“Shutdown”。更多关闭方法可以查阅“八、关闭和退出”中的“&a href=&https://zhuanlan.zhihu.com/p//edit#quit& class=&internal&&1.关闭笔记本和终端&/a&”。&/p&&figure&&img src=&https://pic4.zhimg.com/v2-ca7cb6901327_b.jpg& data-size=&normal& data-rawwidth=&1438& data-rawheight=&419& data-thumbnail=&https://pic4.zhimg.com/v2-ca7cb6901327_b.jpg& class=&origin_image zh-lightbox-thumb& width=&1438& data-original=&https://pic4.zhimg.com/v2-ca7cb6901327_r.jpg&&&figcaption&Running页面功能演示图&/figcaption&&/figure&&p&&br&&/p&&p&&br&&/p&&h2&&b&3. Clusters页面&/b&&/h2&&blockquote&Clusters tab is now provided by IPython parallel. See '&a href=&http://link.zhihu.com/?target=https%3A//github.com/ipython/ipyparallel& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&IPython parallel&/a&' for&br&installation details.&/blockquote&&p&Clusters类目现在已由IPython parallel对接,且由于现阶段使用频率较低,因此在此不做详细说明,想要了解更多可以访问&a href=&http://link.zhihu.com/?target=https%3A//github.com/ipython/ipyparallel& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&IPython parallel的官方网站&/a&。&/p&&h2&&b&4. Conda页面&/b&&/h2&&p&Conda页面主要是Jupyter Notebook与Conda关联之后对Conda环境和包进行直接操作和管理的页面工具。详细信息请直接查阅“五、拓展功能”中的“&a href=&https://zhuanlan.zhihu.com/p//edit#conda& class=&internal&&1.关联Jupyter Notebook和conda的环境和包——‘nb_conda’&/a&”。这是目前使用Jupyter Notebook的必备环节,因此请务必查阅。&/p&&h2&&b&5. Nbextensions页面&/b&&/h2&&p&Nbextensions页面提供了多个Jupyter Notebook的插件,使其功能更加强大。该页面中主要使用的插件有nb_conda,nb_present,Table of Contents(2)。这些功能我们无需完全掌握,也无需安装所有的扩展功能,根据本文档提供的学习思路,我们只需要安装Talbe of Contents(2)即可,该功能可为Markdown文档提供目录导航,便于我们编写文档。该安装指导请查阅“五、拓展功能”中的“&a href=&https://zhuanlan.zhihu.com/p//edit#nbextensions& class=&internal&&2.Markdown生成目录&/a&”。&/p&&figure&&img src=&https://pic2.zhimg.com/v2-a7a2f4b1f06aed21df09b6e3b957e191_b.jpg& data-size=&normal& data-rawwidth=&1440& data-rawheight=&592& class=&origin_image zh-lightbox-thumb& width=&1440& data-original=&https://pic2.zhimg.com/v2-a7a2f4b1f06aed21df09b6e3b957e191_r.jpg&&&figcaption&Nbextensions页面&/figcaption&&/figure&&h2&&b&五、拓展功能&/b&&/h2&&h2&&b&1. 关联Jupyter Notebook和conda的环境和包——“nb_conda”☆&/b&&/h2&&h2&&b&① 安装&/b&&/h2&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&conda install nb_conda
&/code&&/pre&&/div&&p&执行上述命令能够将你conda创建的环境与Jupyter Notebook相关联,便于你在Jupyter Notebook的使用中,在不同的环境下创建笔记本进行工作。&/p&&h2&&b&② 使用&/b&&/h2&&ul&&li&可以在Conda类目下对conda环境和包进行一系列操作。 &/li&&/ul&&figure&&img src=&https://pic2.zhimg.com/v2-75db6aec2cf08a75f17d_b.jpg& data-size=&normal& data-rawwidth=&1439& data-rawheight=&716& class=&origin_image zh-lightbox-thumb& width=&1439& data-original=&https://pic2.zhimg.com/v2-75db6aec2cf08a75f17d_r.jpg&&&figcaption&Conda页面注解图&/figcaption&&/figure&&ul&&li&可以在笔记本内的“Kernel”类目里的“Change&br&kernel”切换内核。 &/li&&/ul&&figure&&img src=&https://pic2.zhimg.com/v2-6ab5fd5e51dff7941dfd_b.jpg& data-size=&normal& data-rawwidth=&1440& data-rawheight=&435& class=&origin_image zh-lightbox-thumb& width=&1440& data-original=&https://pic2.zhimg.com/v2-6ab5fd5e51dff7941dfd_r.jpg&&&figcaption&切换内核&/figcaption&&/figure&&h2&&b&③ 卸载&/b&&/h2&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&canda remove nb_conda
&/code&&/pre&&/div&&p&执行上述命令即可卸载nb_conda包。&/p&&h2&&b&2. Markdown生成目录&/b&&/h2&&ul&&li&不同于有道云笔记的Markdown编译器,Jupyter Notebook无法为Markdown文档通过特定语法添加目录,因此需要通过安装扩展来实现目录的添加。&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&conda install -c conda-forge jupyter_contrib_nbextensions
&/code&&/pre&&/div&&ul&&li&执行上述命令后,启动Jupyter Notebook,你会发现导航栏多了“Nbextensions”的类目,点击“Nbextensions”,勾选“Table&br&of Contents ⑵”&/li&&/ul&&figure&&img src=&https://pic2.zhimg.com/v2-a7a2f4b1f06aed21df09b6e3b957e191_b.jpg& data-size=&normal& data-rawwidth=&1440& data-rawheight=&592& class=&origin_image zh-lightbox-thumb& width=&1440& data-original=&https://pic2.zhimg.com/v2-a7a2f4b1f06aed21df09b6e3b957e191_r.jpg&&&figcaption&Nbextensions页面&/figcaption&&/figure&&ul&&li&之后再在Jupyter Notebook中使用Markdown,点击下图的图标即可使用啦。&/li&&/ul&&figure&&img src=&https://pic2.zhimg.com/v2-dd9b3a1f62cf6d24bf01a1_b.jpg& data-size=&normal& data-rawwidth=&1435& data-rawheight=&712& class=&origin_image zh-lightbox-thumb& width=&1435& data-original=&https://pic2.zhimg.com/v2-dd9b3a1f62cf6d24bf01a1_r.jpg&&&figcaption&目录&/figcaption&&/figure&&h2&&b&3. Markdown在文中设置链接并定位&/b&&/h2&&p&在使用Markdown编辑文档时,难免会遇到需要在文中设定链接,定位在文档中的其他位置便于查看。因为Markdown可以完美的兼容html语法,因此这种功能可以通过html语法当中“a标签”的索引用法来实现。&/p&&p&语法格式如下:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&[添加链接的正文](#自定义索引词)
&a id=自定义索引词&跳转提示&/a&
&/code&&/pre&&/div&&p&注意:&/p&&ol&&ol&&li&语法格式当中所有的符号均是&b&英文半角&/b&。&/li&&li&“自定义索引词”最好是英文,较长的词可以用下划线连接。&/li&&li&“a标签”出现在想要被跳转到的文章位置,html标签除了单标签外均要符合“有头(&code&&a&&/code&)必有尾(&code&&/a&&/code&)”的原则。头尾之间的“跳转提示”是可有可无的。&/li&&li&“a标签”中的“id”值即是为正文中添加链接时设定的“自定义索引值”,这里通过“id”的值实现从正文的链接跳转至指定位置的功能。&/li&&/ol&&/ol&&p&例:&/p&&figure&&img src=&https://pic4.zhimg.com/v2-33dd1aeea782e414db9b703f75f0f32e_b.jpg& data-size=&normal& data-rawwidth=&1438& data-rawheight=&712& data-thumbnail=&https://pic3.zhimg.com/v2-33dd1aeea782e414db9b703f75f0f32e_b.jpg& class=&origin_image zh-lightbox-thumb& width=&1438& data-original=&https://pic3.zhimg.com/v2-33dd1aeea782e414db9b703f75f0f32e_r.jpg&&&figcaption&有跳转提示语&/figcaption&&/figure&&p&&br&&/p&&p&&br&&/p&&figure&&img src=&https://pic2.zhimg.com/v2-c89c7bb57428_b.jpg& data-size=&normal& data-rawwidth=&1436& data-rawheight=&711& data-thumbnail=&https://pic1.zhimg.com/v2-c89c7bb57428_b.jpg& class=&origin_image zh-lightbox-thumb& width=&1436& data-original=&https://pic1.zhimg.com/v2-c89c7bb57428_r.jpg&&&figcaption&无跳转提示语&/figcaption&&/figure&&p&&br&&/p&&p&&br&&/p&&h2&&b&4. 加载指定网页源代码&/b&&/h2&&h2&&b&① 使用场景&/b&&/h2&&p&想要在Jupyter Notebook中直接加载指定网站的源代码到笔记本中。&/p&&h2&&b&② 方法&/b&&/h2&&p&执行以下命令:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&%load URL
&/code&&/pre&&/div&&p&其中,URL为指定网站的地址。&/p&&h2&&b&③ 例&/b&&/h2&&figure&&img src=&https://pic2.zhimg.com/v2-69a40fddfacfecbf60054e9_b.jpg& data-size=&normal& data-rawwidth=&1438& data-rawheight=&763& data-thumbnail=&https://pic2.zhimg.com/v2-69a40fddfacfecbf60054e9_b.jpg& class=&origin_image zh-lightbox-thumb& width=&1438& data-original=&https://pic2.zhimg.com/v2-69a40fddfacfecbf60054e9_r.jpg&&&figcaption&加载网络代码&/figcaption&&/figure&&p&&br&&/p&&p&&br&&/p&&h2&&b&5. 加载本地Python文件&/b&&/h2&&h2&&b&① 使用场景&/b&&/h2&&p&想在Jupyter Notebook中加载本地的Python文件并执行文件代码。&/p&&h2&&b&② 方法&/b&&/h2&&p&执行以下命令:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&%load Python文件的绝对路径
&/code&&/pre&&/div&&h2&&b&③ 注意&/b&&/h2&&ol&&li&Python文件的后缀为“.py”。&/li&&li&“%load”后跟的是Python文件的&b&绝对路径&/b&。&/li&&li&输入命令后,可以按&code&CTRL 回车&/code&来执行命令。第一次执行,是将本地的Python文件内容加载到单元格内。此时,Jupyter Notebook会自动将“%load”命令注释掉(即在前边加井号“#”),以便在执行已加载的文件代码时不重复执行该命令;第二次执行,则是执行已加载文件的代码。&/li&&/ol&&h2&&b&④ 例&/b&&/h2&&figure&&img src=&https://pic3.zhimg.com/v2-99c16d96c8ffbf0e507ec909fadf416a_b.jpg& data-size=&normal& data-rawwidth=&1438& data-rawheight=&692& data-thumbnail=&https://pic3.zhimg.com/v2-99c16d96c8ffbf0e507ec909fadf416a_b.jpg& class=&origin_image zh-lightbox-thumb& width=&1438& data-original=&https://pic3.zhimg.com/v2-99c16d96c8ffbf0e507ec909fadf416a_r.jpg&&&figcaption&加载本地Python文件&/figcaption&&/figure&&p&&br&&/p&&p&&br&&/p&&h2&&b&6. 直接运行本地Python文件&/b&&/h2&&h2&&b&① 使用场景&/b&&/h2&&p&不想在Jupyter Notebook的单元格中加载本地Python文件,想要直接运行。&/p&&h2&&b&② 方法&/b&&/h2&&p&执行命令:&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&%run Python文件的绝对路径
&/code&&/pre&&/div&&p&或&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&!python3 Python文件的绝对路径
&/code&&/pre&&/div&&p&或&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&!python Python文件的绝对路径
&/code&&/pre&&/div&&h2&&b&③ 注意&/b&&/h2&&ol&&li&Python文件的后缀为“.py”。&/li&&li&“%run”后跟的是Python文件的&b&绝对路径&/b&。&/li&&li&“!python3”用于执行Python&br&
3.x版本的代码。&/li&&li&“!python”用于执行Python&br&
2.x版本的代码。&/li&&li&“!python3”和“!python”属于 &code&!shell命令&/code& 语法的使用,即在Jupyter Notebook中执行shell命令的语法。&/li&&li&输入命令后,可以按 &code&control return&/code& 来执行命令,执行过程中将不显示本地Python文件的内容,直接显示运行结果。&/li&&/ol&&h2&&b&④ 例&/b&&/h2&&figure&&img src=&https://pic1.zhimg.com/v2-353ba4eddf9bea398c11_b.jpg& data-size=&normal& data-rawwidth=&1438& data-rawheight=&506& data-thumbnail=&https://pic2.zhimg.com/v2-353ba4eddf9bea398c11_b.jpg& class=&origin_image zh-lightbox-thumb& width=&1438& data-original=&https://pic2.zhimg.com/v2-353ba4eddf9bea398c11_r.jpg&&&figcaption&运行本地Python文件&/figcaption&&/figure&&p&&br&&/p&&p&&br&&/p&&h2&&b&7. 在Jupyter Notebook中获取当前位置&/b&&/h2&&h2&&b&① 使用场景&/b&&/h2&&p&想要在Jupyter Notebook中获取当前所在位置的&b&绝对路径。&/b&&/p&&h2&&b&② 方法&/b&&/h2&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&%pwd
&/code&&/pre&&/div&&p&或&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&!pwd
&/code&&/pre&&/div&&h2&&b&③ 注意&/b&&/h2&&ol&&li&获取的位置是当前Jupyter Notebook中创建的笔记本所在位置,且该位置为&b&绝对路径&/b&。&/li&&li&“!pwd”属于 &code&!shell命令&/code& 语法的使用,即在Jupyter&br&
Notebook中执行shell命令的语法。&/li&&/ol&&h2&&b&④ 例&/b&&/h2&&figure&&img src=&https://pic1.zhimg.com/v2-ca13bbc098e544d0fd53c4d5835f6cfd_b.jpg& data-size=&normal& data-rawwidth=&1433& data-rawheight=&386& data-thumbnail=&https://pic2.zhimg.com/v2-ca13bbc098e544d0fd53c4d5835f6cfd_b.jpg& class=&origin_image zh-lightbox-thumb& width=&1433& data-original=&https://pic2.zhimg.com/v2-ca13bbc098e544d0fd53c4d5835f6cfd_r.jpg&&&figcaption&获取当前位置的绝对路径&/figcaption&&/figure&&p&&br&&/p&&p&&br&&/p&&h2&&b&8. 在Jupyter Notebook使用shell命令&/b&&/h2&&h2&&b&① 方法一——在笔记本的单元格中&/b&&/h2&&h2&&b&⑴ 语法&/b&&/h2&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&!shell命令
&/code&&/pre&&/div&&p&&br&&/p&&ul&&li&在Jupyter Notebook中的笔记本单元格中用英文感叹号“!”后接shell命令即可执行shell命令。&/li&&/ul&&h2&&b&⑵ 例&/b&&/h2&&figure&&img src=&https://pic1.zhimg.com/v2-8b8ec14f44dffa57f117674_b.jpg& data-size=&normal& data-rawwidth=&1438& data-rawheight=&524& data-thumbnail=&https://pic1.zhimg.com/v2-8b8ec14f44dffa57f117674_b.jpg& class=&origin_image zh-lightbox-thumb& width=&1438& data-original=&https://pic1.zhimg.com/v2-8b8ec14f44dffa57f117674_r.jpg&&&figcaption&Shell命令的使用&/figcaption&&/figure&&p&&br&&/p&&p&&br&&/p&&h2&&b&② 方法二——在Jupyter Notebook中新建终端&/b&&/h2&&h2&&b&⑴ 启动方法&/b&&/h2&&p&在Jupyter Notebook主界面,即“File”界面中点击“New”;在“New”下拉框中点击“Terminal”即新建了终端。此时终端位置是在你的家目录,可以通过&code&pwd&/code&命令查询当前所在位置的绝对路径。&/p&&h2&&b&⑵ 关闭方法&/b&&/h2&&p&在Jupyter Notebook的“Running”界面中的“Terminals”类目中可以看到正在运行的终端,点击后边的“Shutdown”即可关闭终端。&/p&&h2&&b&⑶ 例&/b&&/h2&&figure&&img src=&https://pic1.zhimg.com/v2-bd_b.jpg& data-size=&normal& data-rawwidth=&1437& data-rawheight=&699& data-thumbnail=&https://pic2.zhimg.com/v2-bd_b.jpg& class=&origin_image zh-lightbox-thumb& width=&1437& data-original=&https://pic2.zhimg.com/v2-bd_r.jpg&&&figcaption&笔记本中的终端使用&/figcaption&&/figure&&p&&br&&/p&&p&&br&&/p&&h2&&b&9. 隐藏笔记本输入单元格&/b&&/h2&&h2&&b&① 使用场景&/b&&/h2&&p&在Jupyter Notebook的笔记本中无论是编写文档还是编程,都有输入(In [])和输出(Out [])。当我们编写的代码或文档使用的单元格较多时,有时我们只想关注输出的内容而暂时不看输入的内容,这时就需要隐藏输入单元格而只显示输出单元格。&/p&&h2&&b&② 方法一&/b&&/h2&&h2&&b&⑴ 代码&/b&&/h2&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&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)
&/code&&/pre&&/div&&p&在笔记本第一个单元格中输入以上代码,然后执行,即可在该文档中使用“隐藏/显示”输入单元格功能。&/p&&ul&&li&缺陷:此方法不能很好的适用于Markdown单元格。&/li&&/ul&&h2&⑵ 例&/h2&&figure&&img src=&https://pic1.zhimg.com/v2-b8847adffd4ebc8ec24d_b.jpg& data-size=&normal& data-rawwidth=&1438& data-rawheight=&701& data-thumbnail=&https://pic2.zhimg.com/v2-b8847adffd4ebc8ec24d_b.jpg& class=&origin_image zh-lightbox-thumb& width=&1438& data-original=&https://pic2.zhimg.com/v2-b8847adffd4ebc8ec24d_r.jpg&&&figcaption&方法一:隐藏/显示输入单元格&/figcaption&&/figure&&p&&br&&/p&&p&&br&&/p&&h2&&b&③ 方法二&/b&&/h2&&h2&&b&⑴ 代码&/b&&/h2&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&from IPython.display import HTML
HTML('''&script&
code_show=
function code_toggle() {
if (code_show){
$('div.input').hide();
$('div.input').show();
code_show = !code_show
$( document ).ready(code_toggle);
&form action=&javascript:code_toggle()&&&input type=&submit& value=&Click here to toggle on/off the raw code.&&&/form&''')
&/code&&/pre&&/div&&p&在笔记本第一个单元格中输入以上代码,然后执行,即可在该文档中使用“隐藏/显示”输入单元格功能。&/p&&ul&&li&缺陷:此方法不能很好的适用于Markdown单元格。&/li&&/ul&&h2&&b&⑵ 例&/b&&/h2&&figure&&img src=&https://pic4.zhimg.com/v2-b4df17eaf80c2ceb4e3bf_b.jpg& data-size=&normal& data-rawwidth=&1438& data-rawheight=&659& data-thumbnail=&https://pic4.zhimg.com/v2-b4df17eaf80c2ceb4e3bf_b.jpg& class=&origin_image zh-lightbox-thumb& width=&1438& data-original=&https://pic4.zhimg.com/v2-b4df17eaf80c2ceb4e3bf_r.jpg&&&figcaption&方法二:隐藏/显示输入单元格&/figcaption&&/figure&&p&&br&&/p&&p&&br&&/p&&h2&&b&10. 魔术命令&/b&&/h2&&p&由于目前暂时用不到过多的魔术命令,因此暂时先参考&a href=&http://link.zhihu.com/?target=http%3A//ipython.readthedocs.io/en/stable/interactive/magics.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&官网的文档&/a&。&/p&&h2&&b&六、增加内核——“ipykernel” ☆&/b&&/h2&&h2&&b&1. 使用场景&/b&&/h2&&ul&&li&场景一:同时用不同版本的Python进行工作,在Jupyter Notebook中无法切换,即“New”的下拉菜单中无法使用需要的环境。&/li&&li&场景二:创建了不同的虚拟环境(或许具有相同的Python版本但安装的包不同),在Jupyter Notebook中无法切换,即“New”的下拉菜单中无法使用需要的环境。&/li&&/ul&&p&接下来将分别用“命令行模式”和“图形界面模式”来解决以上两个场景的问题。顾名思义,“命令行模式”即在终端中通过执行命令来一步步解决问题;“图形界面模式”则是通过在Jupyter Notebook的网页中通过鼠标点击的方式解决上述问题。&/p&&p&其中,“图形界面模式”的解决方法相对比较简单快捷,如果对于急于解决问题,不需要知道运行原理的朋友,可以直接进入“3. 解决方法之图形界面模式”来阅读。&/p&&p&“命令行模式”看似比较复杂,且又划分了使用场景,但通过这种方式来解决问题可以更好的了解其中的工作原理,比如,每进行一步操作对应的命令是什么,而命令的执行是为了达到什么样的目的,这些可能都被封装在图形界面上的一个点击动作来完成了。对于想更深入了解其运作过程的朋友,可以接着向下阅读。&/p&&h2&&b&2. 解决方法之命令行模式&/b&&/h2&&h2&&b&① 同时使用不同版本的Python&/b&&/h2&&h2&&b&⑴ 在Python 3中创建Python 2内核&/b&&/h2&&h2&&b&(a) pip安装&/b&&/h2&&ul&&li&首先安装Python 2的ipykernel包。&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&python2 -m pip install ipykernel
&/code&&/pre&&/div&&ul&&li&再为&b&当前用户&/b&安装Python 2的内核(ipykernel)。&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&python2 -m ipykernel install --user
&/code&&/pre&&/div&&ul&&li&注意:“--user”参数的意思是针对当前用户安装,而非系统范围内安装。&/li&&/ul&&h2&&b&(b) conda安装&/b&&/h2&&ul&&li&首先创建Python版本为2.x且具有ipykernel的新环境,其中“&env_name&”为自定义环境名,环境名两边不加尖括号“&&”。&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&conda create -n &env_name& python=2 ipykernel
&/code&&/pre&&/div&&ul&&li&然后切换至新创建的环境。&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&Windows: activate &env_name&
Linux/macOS: source activate &env_name&
&/code&&/pre&&/div&&ul&&li&为&b&当前用户&/b&安装Python 2的内核(ipykernel)。&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&python2 -m ipykernel install --user
&/code&&/pre&&/div&&ul&&li&注意:“--user”参数的意思是针对当前用户安装,而非系统范围内安装。&/li&&/ul&&h2&&b&⑵ 在Python 2中创建Python 3内核&/b&&/h2&&h2&&b&(a) pip安装&/b&&/h2&&ul&&li&首先安装Python 3的ipykernel包。&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&python3 -m pip install ipykernel
&/code&&/pre&&/div&&ul&&li&再为&b&当前用户&/b&安装Python 2的内核(ipykernel)。&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&python3 -m ipykernel install --user
&/code&&/pre&&/div&&ul&&li&注意:“--user”参数的意思是针对当前用户安装,而非系统范围内安装。&/li&&/ul&&h2&&b&(b) conda安装&/b&&/h2&&ul&&li&首先创建Python版本为3.x且具有ipykernel的新环境,其中“&env_name&”为自定义环境名,环境名两边不加尖括号“&&”。&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&conda create -n &env_name& python=3 ipykernel
&/code&&/pre&&/div&&ul&&li&然后切换至新创建的环境。&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&Windows: activate &env_name&
Linux/macOS: source activate &env_name&
&/code&&/pre&&/div&&ul&&li&为&b&当前用户&/b&安装Python 3的内核(ipykernel)。&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&python3 -m ipykernel install --user
&/code&&/pre&&/div&&ul&&li&注意:“--user”参数的意思是针对当前用户安装,而非系统范围内安装。&/li&&/ul&&h2&&b&② 为不同环境创建内核&/b&&/h2&&h2&&b&⑴ 切换至需安装内核的环境&/b&&/h2&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&Windows: activate &env_name&
Linux/macOS: source activate &env_name&
&/code&&/pre&&/div&&ul&&li&注意:“&env_name&”是需要安装内核的环境名称,环境名两边不加尖括号“&&”。&/li&&/ul&&h2&&b&⑵ 检查该环境是否安装了ipykernel包&/b&&/h2&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&conda list
&/code&&/pre&&/div&&p&执行上述命令查看当前环境下安装的包,若没有安装ipykernel包,则执行安装命令;否则进行下一步。&/p&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&conda install ipykernel
&/code&&/pre&&/div&&h2&&b&⑶ 为当前环境下的当前用户安装Python内核&/b&&/h2&&ul&&li&若该环境的Python版本为2.x,则执行命令:&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&python2 -m ipykernel install --user --name &env_name& --display-name &&notebook_name&&
&/code&&/pre&&/div&&ul&&li&若该环境的Python版本为3.x,则执行命令:&/li&&/ul&&div class=&highlight&&&pre&&code class=&language-text&&&span&&/span&python3 -m ipykernel install --user --name &env_name& --display-name &&notebook_name&&
&/code&&/pre&&/div&&ul&&li&注意: &/li&&/ul&&p&1. “&env_name&”为当前环境的环境名称。环境名两边不加尖括号“&&”。&/p&&p&2. “&notebook_name&”为自定义显示在Jupyter Notebook中的名称。名称两边不加尖括号“&&”,但&b&双引号必须加&/b&。&/p&&p&3. “--name”参数的值,即“&env_name&”是Jupyter内部使用的,其目录的存放路径为&code&~/Library/Jupyter/kernels/&/code&。如果定义的名称在该路径已经存在,那么将自动覆盖该名称目录的内容。&/p&&p&4. “--display-name”参数的值是显示在Jupyter Notebook的菜单中的名称。&/p&&h2&&b&⑷ 检验&/b&&/h2&&p&使用命令&code&jupyter notebook&/code&启动Jupyter Notebook;在“Files”下的“New”下拉框中即可找到你在第⑶步中的自定义名称,此时,你便可以尽情地在Jupyter Notebook中切换环境,在不同的环境中创建笔记本进行工作和学习啦!&/p&&h2&&b&3. 解决方法之图形界面模式&/b&&/h2&&p&① 你创建了一个新的环境,但却发现在Jupyter Notebook的“New”中找不到这个环境,无法在该环境中创建笔记本。 &/p&&figure&&img src=&https://pic2.zhimg.com/v2-5c1dc6c24b9d29c62868aa_b.jpg& data-size=&normal& data-rawwidth=&1438& data-rawheight=&665& data-thumbnail=&https://pic3.zhimg.com/v2-5c1dc6c24b9d29c62868aa_b.jpg& class=&origin_image zh-lightbox-thumb& width=&1438& data-original=&https://pic3.zhimg.com/v2-5c1dc6c24b9d29c62868aa_r.jpg&&&figcaption&问题发现&/figcaption&&/figure&&p&&br&&/p&&p&&br&&/p&&p&② 进入Jupyter Notebook → Conda → 在“Conda&br&environment”中点击你要添加ipykernel包的环境 → 左下方搜索框输入“ipykernel”&br&→ 勾选“ipykernel” → 点击搜索框旁的“→”箭头 → 安装完毕 → 右下方框内找到“ipykernel”说明已经安装成功&/p&&figure&&img src=&https://pic3.zhimg.com/v2-fa20f958e3_b.jpg& data-size=&normal& data-rawwidth=&1438& data-rawheight=&660& data-thumbnail=&https://pic4.zhimg.com/v2-fa20f958e3_b.jpg& class=&origin_image zh-lightbox-thumb& width=&1438& data-original=&https://pic4.zhimg.com/v2-fa20f958e3_r.jpg&&&figcaption&解决方法&/figcaption&&/figure&&p&&br&&/p&&p&&br&&/p&&p&③ 在终端&code&control c&/code&关闭Jupyter Notebook的服务器然后重启Jupyter Notebook,在“File”的“New”的下拉列表里就可以找到你的环境啦。&/p&&figure&&img src=&https://pic2.zhimg.com/v2-9d4fa171be1229fdfebbbe0_b.jpg& data-size=&normal& data-rawwidth=&1438& data-rawheight=&705& data-thumbnail=&https://pic1.zhimg.com/v2-9d4fa171be1229fdfebbbe0_b.jpg& class=&origin_image zh-lightbox-thumb& width=&1438& data-original=&https://pic1.zhimg.com/v2-9d4fa171be1229fdfebbbe0_r.jpg&&&figcaption&验证&/figcaption&&/figure&&p&&br&&/p&&p&&br&&/p&&h2&&b&七、Jupyter Notebook快捷键&/b&&/h2&&h2&&b&1. Mac与Windows特殊按键对照表&/b&&/h2&&figure&&img src=&https://pic4.zhimg.com/v2-8eaff7a48cda7cb89fb462f82d464963_b.jpg& data-size=&normal& data-rawwidth=&558& data-rawheight=&766& class=&origin_image zh-lightbox-thumb& width=&558& data-original=&https://pic4.zhimg.com/v2-8eaff7a48cda7cb89fb462f82d464963_r.jpg&&&figcaption&Mac和Windows特殊按键对照表&/figcaption&&/figure&&h2&&b&2. Jupyter Notebook笔记本的两种模式&/b&&/h2&&h2&&b&① 命令模式&/b&&/h2&&ul&&li&命令模式将键盘命令与Jupyter Notebook笔记本命令相结合,可以通过键盘不同键的组合运行笔记本的命令。&/li&&li&按&code&esc&/code&键进入命令模式。&/li&&li&命令模式下,单元格边框为灰色,且左侧边框线为蓝色粗线条。&/li&&/ul&&figure&&img src=&https://pic3.zhimg.com/v2-a93c521c691f10a59f9ee66_b.jpg& data-size=&normal& data-rawwidth=&1440& data-rawheight=&311& class=&origin_image zh-lightbox-thumb& width=&1440& data-original=&https://pic3.zhimg.com/v2-a93c521c691f10a59f9ee66_r.jpg&&&figcaption&命令模式&/figcaption&&/figure&&h2&&b&② 编辑模式&/b&&/h2&&ul&&li&编辑模式使用户可以在单元格内编辑代码或文档。&/li&&li&按&code&enter&/code&或&code&return&/code&键进入编辑模式。&/li&&li&编辑模式下,单元格边框和左侧边框线均为绿色。&/li&&/ul&&figure&&img src=&https://pic2.zhimg.com/v2-dc_b.jpg& data-size=&normal& data-rawwidth=&1440& data-rawheight=&294& class=&origin_image zh-lightbox-thumb& width=&1440& data-original=&https://pic2.zhimg.com/v2-dc_r.jpg&&&figcaption&编辑模式&/figcaption&&/figure&&h2&&b&3. 两种模式的快捷键&/b&&/h2&&h2&&b&① 命令模式&/b&&/h2&&figure&&img src=&https://pic4.zhimg.com/v2-61c3aae5bf_b.jpg& data-size=&normal& data-rawwidth=&952& data-rawheight=&3302& class=&origin_image zh-lightbox-thumb& width=&952& data-original=&https://pic4.zhimg.com/v2-61c3aae5bf_r.jpg&&&figcaption&命令模式快捷键&/figcaption&&/figure&&h2&&b&② 编辑模式&/b&&/h2&&figure&&img src=&https://pic4.zhimg.com/v2-8eaff7a48cda7cb89fb462f82d464963_b.jpg& data-size=&normal& data-rawwidth=&558& data-rawheight=&766& class=&origin_image zh-lightbox-thumb& width=&558& data-original=&https://pic4.zhimg.com/v2-8eaff7a48cda7cb89fb462f82d464963_r.jpg&&&figcaption&编辑模式快捷键&/figcaption&&/figure&&h2&&b&4. 查看和编辑快捷键&/b&&/h2&&h2&&b&① 查看快捷键&/b&&/h2&&p&① 进入Jupyter Notebook主界面“File”中。&/p&&p&② 在“New”的下拉列表中选择环境创建一个笔记本。&/p&&p&③ 点击“Help”。&/p&&p&④ 点击“Keyboard Shortcuts”。&/p&&h2&&b&② 编辑快捷键&/b&&/h2&&h2&&b&⑴ 方法一&/b&&/h2&&p&① 进入Jupyter Notebook主界面“File”中。&/p&&p&② 在“New”的下拉列表中选择环境创建一个笔记本。&/p&&p&③ 点击“Help”。&/p&&p&④ 点击“Keyboard Shortcuts”。&/p&&p&⑤ 弹出的对话框中“Command Mode (press Esc to enable)”旁点击“Edit&br&Shortcuts”按钮。&/p&&h2&&b&⑵ 方法二&/b&&/h2&&p&① 进入Jupyter Notebook主界面“File”中。&/p&&p&② 在“New”的下拉列表中选择环境创建一个笔记本。&/p&&p&③ 点击“Help”。&/p&&p&④ 点击“Edit Keyboard Shortcuts”。&/p&&h2&&b&③ 例&/b&&/h2&&figure&&img src=&https://pic4.zhimg.com/v2-706e6c1a9f309e0ed5d0f6_b.jpg& data-size=&normal& data-rawwidth=&1436& data-rawheight=&705& data-thumbnail=&https://pic3.zhimg.com/v2-706e6c1a9f309e0ed5d0f6_b.jpg& class=&origin_image zh-lightbox-thumb& width=&1436& data-original=&https://pic3.zhimg.com/v2-706e6c1a9f309e0ed5d0f6_r.jpg&&&figcaption&查看和编辑快捷键&/figcaption&&/figure&&p&&br&&/p&&p&&br&&/p&&h2&&b&八、关闭和退出&/b&&/h2&&h2&&b&1. 关闭笔记本和终端&/b&&/h2&&p&当我们在Jupyter Notebook中创建了终端或笔记本时,将会弹出新的窗口来运行终端或笔记本。当我们使用完毕想要退出终端或笔记本时,仅仅&b&关闭页面&/b&是无法结束程序运行的,因此我们需要通过以下步骤将其完全关闭。&/p&&h2&&b&① 方法一&/b&&/h2&&p&⑴ 进入“Files”页面。&/p&&p&⑵ 勾选想要关闭的“ipynb”笔记本。正在运行的笔记本其图标为绿色,且后边标有“Running”的字样;已经关闭的笔记本其图标为灰色。&/p&&p&⑶ 点击上方的黄色的“Shutdown”按钮。&/p&&p&⑷ 成功关闭笔记本。&/p&&ul&&li&注意:此方法只能关闭笔记本,无法关闭终端。&/li&&/ul&&h2&&b&② 方法二&/b&&/h2&&p&⑴ 进入“Running”页面。&/p&&p&⑵ 第一栏是“Terminals”,即所有正在运行的终端均会在此显示;第二栏是“Notebooks”,即所有正在运行的“ipynb”笔记本均会在此显示。&/p&&p&⑶ 点击想要关闭的终端或笔记本后黄色“Shutdown”按钮。&/p&&p&⑷ 成功关闭终端或笔记本。&/p&&ul&&li&注意:此方法可以关闭任何正在运行的终端和笔记本。&/li&&/ul&&h2&&b&③ 注意&/b&&/h2&&p&⑴ 只有“ipynb”笔记本和终端需要通过上述方法才能使其结束运行。&/p&&p&⑵&br&“txt”文档,即“New”下拉列表中的“Text&br&File”,以及“Folder”只要关闭程序运行的页面即结束运行,无需通过上述步骤关闭。&/p&&h2&&b&④ 演示&/b&&/h2&&figure&&img src=&https://pic2.zhimg.com/v2-fbfc5bbef23_b.jpg& data-size=&normal& data-rawwidth=&1438& data-rawheight=&451& data-thumbnail=&https://pic4.zhimg.com/v2-fbfc5bbef23_b.jpg& class=&origin_image zh-lightbox-thumb& width=&1438& data-original=&https://pic4.zhimg.com/v2-fbfc5bbef23_r.jpg&&&figcaption&关闭笔记本或终端程序&/figcaption&&/figure&&p&&br&&/p&&p&&br&&/p&&h2&&b&2. 退出Jupyter Notebook程序&/b&&/h2&&p&如果你想退出Jupyter Notebook程序,仅仅通过关闭网页是无法退出的,因为当你打开Jupyter Notebook时,其实是启动了它的服务器。&/p&&p&你可以尝试关闭页面,并打开新的浏览器页面,把之前的地址输进地址栏,然后跳转页面,你会发现再次进入了刚才“关闭”的Jupyter Notebook页面。&/p&&p&如果你忘记了刚才关闭的页面地址,可以在启动Jupyter Notebook的终端中找到地址,复制并粘贴至新的浏览器页面的地址栏,会发现同样能够进入刚才关闭的页面。&/p&&p&因此,想要彻底退出Jupyter Notebook,需要关闭它的服务器。只需要在它启动的终端上按:&/p&&ul&&li&Mac用户:&code&control c&/code&&/li&&li&Windows用户:&code&ctrl c&/code&&/li&&/ul&&p&然后在终端上会提示:“Shutdown this notebook server (y/[n])?”输入&code&y&/code&即可关闭服务器,这才是彻底退出了Jupyter Notebook程序。此时,如果你想要通过输入刚才关闭网页的网址进行访问Jupyter Notebook便会看到报错页面。&/p&&h2&&b&九、参考资料&/b&&/h2&&p&1.知乎:jupyter notebook 可以做哪些事情?&a href=&https://www.zhihu.com/question//answer/?utm_source=wechat_session&utm_medium=social& class=&internal&&猴子的回答&/a&&/p&&p&2. &a href=&http://link.zhihu.com/?target=https%3A//jupyter-notebook.readthedocs.io/en/stable/notebook.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Jupyter Notebook官方介绍&/a&&/p&&p&3. &a href=&http://link.zhihu.com/?target=https%3A//www.anaconda.com/download/%23macos& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Anaconda官方下载页面&/a&&/p&&p&4. &a href=&http://link.zhihu.com/?target=http%3A//blog.csdn.net/tina_ttl/article/details/1-%25E6%%25E5%25BC%258F%25E4%25B8%2580& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Python·Jupyter Notebook各种使用方法记录&/a&&/p&&p&5. &a href=&http://link.zhihu.com/?target=https%3A//stackoverflow.com/questions//how-to-hide-code-from-cells-in-ipython-notebook-visualized-with-nbviewer& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Stack Overflow中有关如何隐藏/显示输入单元格的问题&/a&&/p&&p&6. &a href=&http://link.zhihu.com/?target=http%3A//ipython.readthedocs.io/en/stable/interactive/magics.html& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&魔术命令官方文档&/a&&/p&&p&7. &a href=&http://link.zhihu.com/?target=http%3A//blog.csdn.net/lawme/article/details/& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Jupyter Notebook 的快捷键&/a&&/p&&p&8. &a href=&http://link.zhihu.com/?target=http%3A//jupyter.org/documentation& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Jupyter Notebook官方文档&/a&&/p&
一、什么是Jupyter Notebook?1. 简介Jupyter Notebook是基于网页的用于交互计算的应用程序。其可被应用于全过程计算:开发、文档编写、运行代码和展示结果。——简而言之,Jupyter Notebook是以网页的形式打开,可以在网页页面中…
&p&事实上Anaconda 和 Jupyter notebook已成为数据分析的标准环境。&/p&&p&简单来说,Anaconda是包管理器和环境管理器,Jupyter notebook 可以将数据分析的代码、图像和文档全部组合到一个web文档中。&/p&&p&&br&&/p&&p&接下来我详细介绍下Anaconda,并在最后给出Jupyter notebook:&/p&&p&&b&1.Anaconda是什么?&/b&&/p&&p&&b&2.如何安装?&/b&&/p&&p&&b&3. 如何管理包?&/b&&/p&&p&&b&4.如何管理环境?&/b&&/p&&p&&b&5.Jupyter notebook如何快速上手?&/b&&/p&&p&&br&&/p&&p&&b&不过在开始前我需要强调下,下面的步骤你要亲自跟着敲一遍并在自己的电脑上实践。虽然下面你会遇到很多命令,给了谁都记不住的。但是别怕,也别中途放弃,因为你没必要记住命令,因为当你在后面学习数据分析用的多了,自然就记住了。&/b&&/p&&p&&b&记不住也没关系,学会在哪查找就可以了。你只需要跟着上面步骤操作下,并理解了每一步是干什么的就可以了。后面遇到要做的事情,忘记了回头查这个文档就可以了。&/b&&/p&&p&&b&刚开始学习的过程就像下面这个图,只要中途不放弃,自己实际操作一遍,我保证你可以熟练上手。&/b&&/p&&figure&&img src=&https://pic1.zhimg.com/v2-17d0d8cd0e704bcb4ce9e8_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&284& data-rawheight=&344& class=&content_image& width=&284&&&/figure&&p&&br&&/p&&blockquote&&b&1.Anaconda是什么?&/b&&/blockquote&&p&Anaconda在英文中是“蟒蛇”,麻辣鸡(Nicki Minaj妮琪·米娜)有首歌就叫《Anaconda》,表示像蟒蛇一样性感妖娆的身体。&/p&&figure&&img src=&https://pic2.zhimg.com/v2-152adf3e7a247a612f4b08ce_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&757& data-rawheight=&592& class=&origin_image zh-lightbox-thumb& width=&757& data-original=&https://pic2.zhimg.com/v2-152adf3e7a247a612f4b08ce_r.jpg&&&/figure&&p&所有你看下面Anaconda的图标就像一个收尾互相咬住的“蟒蛇”。&/p&&figure&&img src=&https://pic4.zhimg.com/v2-3e35b424dbfe78b_b.jpg& data-caption=&& data-size=&normal& data-rawwidth=&615& data-rawheight=&290& class=&origin_image zh-lightbox-thumb& width=&615& data-original=&https://pic4.zhimg.com/v2-3e35b424dbfe78b_r.jpg&&&/figure&&p&&b&你可能已经安装了 Python,那么为什么还需要 Anaconda?有以下3个原因:&/b&&/p&&p&1)Anaconda 附带了

我要回帖

更多关于 文件批量重命名 的文章

 

随机推荐