保存时自动将Jupyter笔记本导出为各种文件格式(.py、.html等)。
nbautoexport的Python项目详细描述
nbautoexport公司
Making it easier to code review Jupyter notebooks, one script at a time.
nbautoexport
使用Jupyter时,保存时自动将Jupyter笔记本导出为各种文件格式(.py、.html等)。一个很好的用例是自动生成笔记本的脚本版本,以方便代码评审注释。在
安装
首先,您需要安装nbautoexport
。这应该安装在运行Jupyter Notebook或Jupyter Lab所在的环境中。nbautoexport
可从PyPI经由pip
或从{a7}经由conda
获得。在
pip install nbautoexport^{pr2}$
{juter>自动运行时,{juter>或pyjuter}自动运行:
nbautoexport install
如果已经有一个Jupyter服务器正在运行,则需要重新启动它才能生效。在
简单用法
假设您有一个项目,并将您的笔记本放在notebooks/
子目录中。在
要将该目录配置为自动导出,请运行以下命令:
nbautoexport configure notebooks
这将创建一个配置文件notebooks/.nbautoexport
。在
如果您已经将nbautoexport
设置为与Jupyter一起工作(使用install
命令,如前一节所述),那么每当您在Jupyter中保存笔记本时,都会运行一个钩子来检查在与笔记本相同的目录中是否存在.nbautoexport
配置文件。如果是这样,它将使用该文件中指定的设置来导出您的笔记本。默认情况下,它将生成以笔记本命名的笔记本的脚本版本(扩展名为.py
),并保存在notebooks/script
目录中。在
如果一切正常,您的笔记本目录应以如下示例所示的文件结尾:
notebooks ├──0.1-ejm-data-exploration.ipynb ├──0.2-ejm-feature-creation.ipynb └── script └── 0.1-ejm-data-exploration.py └── 0.2-ejm-feature-creation.py
配置导出选项
默认的.nbautoexport
配置文件如下所示:
{"export_formats":["script"],"organize_by":"extension"}
保存后,这将导致笔记本被导出到保存到notebooks/script
目录中的脚本。在
notebooks ├──0.1-ejm-data-exploration.ipynb ├──0.2-ejm-feature-creation.ipynb └── script └── 0.1-ejm-data-exploration.py └── 0.2-ejm-feature-creation.py
组织导出文件的另一种方法是为每个笔记本创建一个目录。这对于在tab完成时匹配笔记本和子目录非常方便,然后在完成部分之后使用*
进行全局匹配。在
nbautoexport configure notebooks --organize-by notebook
notebooks ├── 0.1-ejm-data-exploration │ └── 0.1-ejm-data-exploration.py ├── 0.2-ejm-feature-creation │ └── 0.2-ejm-feature-creation.py ├──0.1-ejm-data-exploration.ipynb └──0.2-ejm-feature-creation.ipynb
如果您不喜欢所选的设置,您可以通过以下方式进行更改:1)使用新参数和--overwrite
标志重新运行nbautoexport
命令,或者2)手动编辑.nbautoexport
文件。在
您还可以指定任意多个导出格式。我们支持^{html
、md
和{--export-format
。在
高级示例
nbautoexport configure sprint_one_notebooks -f script -f html --organize-by extension
保存后,这将在sprint_one_notebooks
文件夹中创建.py
和{
notebooks ├──0.1-ejm-data-exploration.ipynb ├──0.2-ejm-feature-creation.ipynb ├── script │ └── 0.1-ejm-data-exploration.py │ └── 0.1-ejm-features-creation.py └── html └── 0.1-ejm-data-exploration.html └── 0.1-ejm-features-creation.html
更多功能
nbautoexport
CLI有两个附加命令:
export
用于临时导出笔记本或笔记本目录clean
(实验性)将删除目录中不是由当前.nbautoexport
配置生成的文件
使用--help
标志查看文档。在
命令行帮助
nbautoexport --help
Usage: nbautoexport [OPTIONS] COMMAND [ARGS]... Automatically export Jupyter notebooks to various file formats (.py, .html, and more) upon save. One great use case is to automatically have script versions of your notebooks to facilitate code review commenting. To set up, first use the 'install' command to register nbautoexport with Jupyter. If you already have a Jupyter server running, you will need to restart it. Next, you will need to use the 'configure' command to create a .nbautoexport configuration file in the same directory as the notebooks you want to have export automatically. Once nbautoexport is installed with the first step, exporting will run automatically when saving a notebook in Jupyter for any notebook where there is a .nbautoexport configuration file in the same directory. Options: --version Show nbautoexport version. --install-completion Install completion for the current shell. --show-completion Show completion for the current shell, to copy it or customize the installation. --help Show this message and exit. Commands: clean (EXPERIMENTAL) Remove subfolders/files not matching... configure Create a .nbautoexport configuration file in a directory. export Manually export notebook or directory of notebooks. install Register nbautoexport post-save hook with Jupyter.
这个存储库最初是使用Cookiecutter和^{
历史
0.2.1(2020年9月18日)
nbconvert
发布了Verion6,它破坏了一些功能。固定到nbconvert<6
,直到我们可以寻址#57。在
0.2.0(2020-09-04)
- 添加选项以指定全局样式模式,以在使用
clean
时从删除中排除文件。详见documentation。(#46,#54)
0.1.1(2020-08-06)
0.1.0(2020-08-05)
- PyPI的第一个版本。在
- 项目
标签: