有没有一种方法可以在Visual Studio Code中删除Jupyter“运行单元”语法?

时间:2019-03-28 07:26:26

标签: python visual-studio visual-studio-code jupyter-notebook

我要删除Jupyter“运行单元格|运行所有单元格”注释,如果Visual Studio代码中存在语法#%%,则会显示该注释。

vsc

有可以控制的设置吗?

谢谢。

4 个答案:

答案 0 :(得分:1)

如果在“设置”>“ Python”>“数据科学”>“启用”下关闭数据科学功能(“ Python交互式”窗口),那么您将不再看到这些代码镜头。但是,这也会隐藏镜头中其余的数据科学功能。您是要关闭python扩展中的所有数据科学功能还是仅关闭镜头?

答案 1 :(得分:1)

针对其他遇到此问题的人进行更新:

使用最新更新,您可以选择Run cell | ...“显示的内容。 如果您要消除混乱,请删除所有内容并按如下所示保存:

enter image description here

我建议至少保留python.datascience.runcell,因为它似乎会禁用shift +输入快捷键

答案 2 :(得分:0)

您可以关闭Python>Data Science: Enable Cell Code Lens设置。

Screenshot from settings

答案 3 :(得分:0)

将下面的代码片段保存为:remove_inline_comment.py
假设您的文件名是:sample_file.py,
运行:python remove_inline_comment.py sample_file.py

[注意:确保这两个文件在同一个文件夹中]

import argparse
import os
import sys
import re


def process(filename):
    """Removes empty lines and lines that contain only whitespace, and
    lines with comments"""

    with open(filename) as in_file, open(filename, "r+") as out_file:
        for line in in_file:
            if re.match("# In\[[0-9\\d+\]]", line):
                out_file.write("\n")
            else:
                out_file.writelines(line)


if __name__ == "__main__":

    my_parser = argparse.ArgumentParser(
        description="Removing the In#\[[0-9\d+\]] in notebook to script conversion"
    )

    my_parser.add_argument(
        "script_path",
        metavar="path",
        type=str,
        help="path to script that requires a conversion",
    )

    args = my_parser.parse_args()
    script_path = args.script_path

    file_format = script_path.split(".")[1]
    if file_format != "py":
        print("File is not a py file")
        sys.exit()

    try:
        print(f"processing : {script_path}")
        process(script_path)
    except FileNotFoundError:
        print("Please provide path correctly")