在Jupyter笔记本中动态添加图像

时间:2016-12-06 09:33:55

标签: python-2.7 jupyter-notebook

假设您正在使用笔记本电脑。有没有一种奇特的方法来生成降价单元格来显示图像(在同一个笔记本中),给出一个文件名和标题列表?

换句话说,给定一个字符串模板(带有多个占位符)应该在markdown单元格中,有没有办法循环占位符值列表以生成所需的所有单元格?

现在,我的懒惰方式如下(不是很懒,因为它涉及在单元格中复制/粘贴md代码)。

from string import Template

img_list = !ls figs/start_*.png

fig_template = Template('''<figure>
    <img src="$fn" alt='$capt'>
    <figcaption> Figure $fig_idx. $capt</figcaption>
</figure>'''
)

for idx, img in enumerate(img_list, 1):
    print fig_template.substitute(fn=img, capt=img, fig_idx=idx)

0 个答案:

没有答案
相关问题