简体   繁体   English

使用 `--to script` 忽略 `jupyter nbconvert` 中的 Markdown 单元格

[英]ignore markdown cells in `jupyter nbconvert` with `--to script`

Is it possible to have jupyter nbconvert ... --to script ignore markdown cells rather than convert them to comments?是否可以让jupyter nbconvert ... --to script忽略降价单元而不是将它们转换为注释?

I'm asking because I want to use flake8 to check the Python code that's generated but I don't want to include the markdown cells (for example, because they are often long lines, and I don't want flake8 to complain about them).我问是因为我想使用flake8检查生成的 Python 代码,但我不想包含降价单元格(例如,因为它们通常是长行,我不想 flake8 抱怨它们)。

There is the command line argument PythonExporter.exclude_markdown that does what you want.命令行参数PythonExporter.exclude_markdown您的需求。 To get a list of pep8 errors that just look at the code cells, I run要获取仅查看代码单元格的 pep8 错误列表,我运行

jupyter nbconvert my_notebook.ipynb --stdout --to python --PythonExporter.exclude_markdown=True | flake8 - --ignore=W391

This seems to work:这似乎有效:

Create a template strip_markdown.tpl创建模板strip_markdown.tpl

## remove markdown cells
{% extends 'python.tpl'%}
{% block markdowncell -%}
{% endblock markdowncell %}

jupyter nbconvert my_notebook.ipynb --to python --template=strip_markdown.tpl jupyter nbconvert my_notebook.ipynb --to python --template=strip_markdown.tpl

In case someone else wants to check their jupyter notebooks with CI and the nbconvert + pipe approach doesn't work well enough for you (as it did for me), I want to promote the tool/hack I wrote for this purpose. 如果其他人想用CI检查他们的jupyter笔记本,而nbconvert + pipe方法对您来说效果不佳(对我而言是如此),我想推广我为此目的编写的工具/ hack。 flake8-nb flake8-nb

I'm asking because I want to use flake8 to check the Python code that's generated我问是因为我想使用 flake8 来检查生成的 Python 代码

You can do this directly with nbqa :您可以直接使用nbqa执行此操作

$ nbqa flake8 my_notebook.ipynb --extend-ignore=E203,E302,E305,E703
my_notebook.ipynb:cell_3:1:1: F401 'import pandas as pd' imported but unused

It also works as a pre-commit hook, see https://nbqa.readthedocs.io/en/latest/pre-commit.html它也可以作为预提交钩子,参见https://nbqa.readthedocs.io/en/latest/pre-commit.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 nbconvert 执行包含内联降价的 Jupyter 笔记本 - Execute a Jupyter notebook including inline markdown with nbconvert 我需要什么降价模板,以便在使用nbconvert导出时,Jupyter笔记本中的输出单元格看起来与输入单元格不同 - What markdown template do I need such that output cells in Jupyter notebooks look different from input cells when exporting using nbconvert nbconvert to markdown:避免代码单元格中的缩进 - nbconvert to markdown: avoid indents in code cells Jupyter Notebook nbconvert 无魔法命令/无降价 - Jupyter Notebook nbconvert without magic commands/ w/o markdown jupyter nbconvert:转换为 markdown 和/或 html 不包括标记为“跳过”的幻灯片 - jupyter nbconvert: convert to markdown and/or html excluding slides marked as `skip` 防止 Jupyter Notebook 合并 markdown 个单元格 - Prevent Jupyter Notebook from merging markdown cells 如何为Jupyter nbconvert编写Jinja2模板以显示降价并抑制输出数量? - How to write Jinja2 template for Jupyter nbconvert to show markdown and suppress output number? 仅显示从Jupyter Notebook用nbconvert创建的pdf中的特定(带标签)输入单元格 - Show only specific (tagged) input cells in pdf created with nbconvert from jupyter notebook 将jupyter笔记本转换为pdf(彩色) - nbconvert jupyter notebook to pdf (with color) Jupyter 内核死于 nbconvert 但不是在 jupyter - Jupyter Kernel dies from nbconvert but not on jupyter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM