我开始严重依赖IPython笔记本应用程序来开发和记录算法。这太棒了;但有些事情看起来应该是可能的,但我不知道该怎么做:

I would like to insert a local image into my (local) IPython notebook markdown to aid in documenting an algorithm. I know enough to add something like <img src="image.png"> to the markdown, but that is about as far as my knowledge goes. I assume I could put the image in the directory represented by 127.0.0.1:8888 (or some subdirectory) to be able to access it, but I can't figure out where that directory is. (I'm working on a mac.) So, is it possible to do what I'm trying to do without too much trouble?


当前回答

到目前为止给出的大多数答案都走错了方向,建议加载额外的库并使用代码而不是标记。在Ipython/Jupyter notebook中,这非常简单。确保单元格确实在标记中,并使用以下方法显示图像:

![alt text](imagename.png "Title")

与提出的其他方法相比,该方法的另一个优点是可以显示所有常见的文件格式,包括jpg、png和gif(动画)。

其他回答

笔记本目录中的文件在“Files /”url下可用。所以如果它在基本路径中,它将是<img src="files/image.png">, subdirs等也可用:<img src="files/subdir/image.png">等。

更新:从IPython 2.0开始,不再需要files/前缀(参见发布说明)。所以现在解决方案<img src="image.png">就像预期的那样工作。

在运行此代码之前,将默认块从“Code”更改为“Markdown”:

![<caption>](image_filename.png)

如果镜像文件在其他文件夹,可以执行以下操作:

![<caption>](folder/image_filename.png)

你可以通过“pwd”命令在没有引号的jupyter笔记本中找到你当前的工作目录。

jupyter笔记本的最后一个版本接受复制/粘贴的图像本机

明克的答案是正确的。

然而,我发现图像在打印视图中出现了损坏(在我的Windows机器上,在Chrome浏览器中运行IPython版本0.13.2的Anaconda发行版)

解决方法是使用<img src="../files/image.png">代替。

这使得图像在打印视图和正常的iPython编辑视图中都正确显示。

更新:当我升级到iPython v1.1.0时,不再需要这个解决方案,因为打印视图不再存在。事实上,您必须避免这种解决方法,因为它会阻止nbconvert工具查找文件。