Pelican插件,用于使用Jupyter/IPython笔记本写博客
pelican-jupyter的Python项目详细描述
pelican jupyter:用于jupyter笔记本的pelican插件
安装
pip install pelican-jupyter
鹈鹕和朱庇特版本
主要关注的是运行最新版本的包,但是插件很有可能在Pelican和Jupyter/的旧版本中正常工作。 建议的库版本为:
pelican>=4
notebook>=6
nbconvert>=5
使用
此插件提供了两种在Pelican中使用Jupyter笔记本的模式:
- 作为一种新的标记语言,因此
.ipynb
文件被识别为文章的有效文件类型 - 作为基于liquid tags plugin的液体标签,所以笔记本可以
包含在一篇使用降价(
.md
)文件的普通帖子中。在
模式A:标记模式
在您的pelicanconf.py
上:
在这种模式下,您需要使用以下两个选项之一将MD元数据传递给插件:
选项1:.nbdata
元数据文件
将.ipynb
文件放在content文件夹中,并使用
与扩展名为.nbdata
的ipython笔记本同名。
例如,如果您有my_post.ipynb
创建my_post.nbdata
。在
.nbdata
应该像普通的基于降价的文章一样包含元数据(注意末尾的空行,您需要它):
Title:
Slug:
Date:
Category:
Tags:
Author:
Summary:
可以指定仅包含笔记本单元格的子集
Subcells
元数据项。
它应该包含要包含的第一个和最后一个单元格的索引(从0开始)
(使用None
表示开放范围)。
例如,要跳过前两个单元格:
Subcells: [2, None]
选项2:笔记本中的元数据单元格
使用此选项,元数据将从 笔记本(应该是一个降价单元格),在呈现笔记本时,此单元格将被忽略。在
在您的pelicanconf.py
上:
MARKUP=("md","ipynb")frompelican_jupyterimportmarkupasnb_markupPLUGINS=[nb_markup]IPYNB_MARKUP_USE_FIRST_CELL=TrueIGNORE_FILES=[".ipynb_checkpoints"]
现在,可以在降价模式下将元数据放在第一个笔记本单元格中,如下所示:
- title: My notebook - author: John Doe - date: 2018-05-11 - category: pyhton - tags: pip
模式B:液体标签
在您的pelicanconf.py
上:
MARKUP=('md',)frompelican_jupyterimportliquidasnb_liquidPLUGINS=[nb_liquid]IGNORE_FILES=[".ipynb_checkpoints"]
在这之后,你可以使用一个液体标签在任何常规的降价文章中包括一个笔记本,
例如mypost.md
:
Title:
Slug:
Date:
Category:
Tags:
Author:
Summary:
{% notebook path/from/content/dir/to/notebook.ipynb %}
推荐模式?在
就我个人而言,我喜欢方法A-选项1,因为我先写笔记本,然后再添加 元数据文件并保持笔记本的干净。在
Liquid tag模式提供了更大的灵活性,可以将现有的笔记本代码或输出与降价时的额外文本相结合。
您也可以在此模式下组合两个或多个笔记本。
liquid tag模式的唯一问题是它不能为文章生成摘要
自动从笔记本中删除,因此您必须将其写入包含笔记本的源.md
文件中
您可以同时使用这两种模式,但您可能会看到一个异常 防止冲突,忽略它。在
CSS上的注释
如果笔记本在你的鹈鹕主题看起来不好,这可以帮助。在
关于Jupyter笔记本需要的CSS和pelican主题有一些问题/冲突。在
我尽我最大的努力使插件与每一个主题,但由于明显的原因,我不能保证它将在任何鹈鹕主题好看。在
Jupyter笔记本是基于引导的,所以如果你想让html和css很好地呈现,你可能需要你的主题基于它。在
我试图通过删除Jupyter的引导代码,只注入额外的CSS代码,只注入必要的CSS。 在某些情况下,但是需要修复,我建议您看看my theme如何修复它们。在
您可以通过设置IPYNB_SKIP_CSS=True
完全禁止包含任何笔记本CSS,这使得pelican主题具有更大的灵活性。在
IPYNB_EXPORT_TEMPLATE
选项是使用Jupyter nbconvert本机扩展输出的另一个很好的方法。在
设置
注意:如果您使用的是液体模式,您需要在pelicanconf.py
中设置这样的变量。在
LIQUID_CONFIGS = (("IPYNB_EXPORT_TEMPLATE", "notebook.tpl", ""), )
如果您使用的是标记模式,那么只需将这些变量添加到您的pelicanconf.py
。在
Setting | Description |
---|---|
^{ | [markup and liquid] Do not apply any of the plugins "fixes" to the Jupyter CSS use all the default Jupyter CSS. |
^{ | [markup and liquid] Do not include (at all) the notebook CSS in the generated output. This is usefull if you want to include it yourself in the theme. |
^{ | [markup and liquid] A list of nbconvert preprocessors to be used when generating the HTML output. |
^{ | [markup and liquid] Path to nbconvert export template (relative to project root). For example: Create a custom template that extends from the ^{ |
^{ | [markup only] List of tuples with the html tag and attribute (python HTMLParser format) that are used to stop the summary creation, this is useful to generate valid/shorter summaries. |
^{ | [markup only] Create a summary based on the notebook content. Every notebook can still use the s^{ |
^{ | [markup only] List of tuples to extend the default ^{ |
^{ | [markup only] If you want to make the original notebook available set this variable in a is similar way to the default pelican ^{ |
^{ | [markup only] Change the pygments colorscheme used for syntax highlighting |
^{ | [Pelican setting useful for markup] Prevents pelican from trying to parse notebook checkpoint files. |
IPYNB_EXPORT_TEMPLATE
的模板示例:
{%- extends 'basic.tpl' -%}
{% block header %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<style type="text/css">
div.code_cell {
border: 2px solid red;
}
</style>
{%- endblock header %}
- 项目
标签: