将jupyter转换为python文件

news/2024/7/16 9:38:00 标签: python, jupyter, ide

一直在思考是否可以将jupyter转化为python,一直都未尝试,但是最近在网上找了一些资料,尝试了一下。相应的代码如下:

python"># 简单的将jupyter转换为python文件
import argparse
import os
import subprocess
def convert(input_path, output_path):
    subprocess.call(['jupyter', 'nbconvert', '--to', 'script', input_path, '--output', output_path])
def cleanup(path):
    skip_lines_startwith = ('Image(filename=', '# In[', '# <hr>', 'from IPython.display import Image', 'get_ipython()', '# <br>')
    clean_content = []
    imports = []
    existing_imports = set()
    with open(path, 'r', encoding="utf8") as f:
        next(f)
        next(f)
        for line in f:
            line = line.rstrip(' ')
            if line.startswith(skip_lines_startwith):
                continue
            if line.startswith('import ') or (
                    'from ' in line and 'import ' in line):
                if 'from __future__ import print_function' in line:
                    if line != imports[0]:
                        imports.insert(0, line)
                else:
                    if line.strip() not in existing_imports:
                        imports.append(line)
                        existing_imports.add(line.strip())
            else:
                clean_content.append(line)
    clean_content = ['# coding: utf-8\n\n\n'] + imports + clean_content
    with open(path, 'w', encoding="utf8") as f:
        for line in clean_content:
            f.write(line)
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Convert Jupyter notebook to Python script.', formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('-i', '--input', required=True, help='Path to the Jupyter Notebook file')
    parser.add_argument('-o', '--output', required=True, help='Path to the Python script file')
    parser.add_argument('-v', '--version', action='version', version='v. 0.1')
    args = parser.parse_args()
    convert(input_path=args.input,output_path=os.path.splitext(args.output)[0])
    cleanup(args.output)

后续如果需要将python转换为jupyter的文件,在更新相应的代码


http://www.niftyadmin.cn/n/5299665.html

相关文章

基于C#的机械臂欧拉角与旋转矩阵转换

欧拉角概述 机器人末端执行器姿态描述方法主要有四种&#xff1a;旋转矩阵法、欧拉角法、等效轴角法和四元数法。所以&#xff0c;欧拉角是描述机械臂末端姿态的重要方法之一。 关于欧拉角的历史&#xff0c;由来已久&#xff0c;莱昂哈德欧拉用欧拉角来描述刚体在三维欧几里…

使用bpftrace+GDB尽早attach启动时间不定的进程

引言 曾经在stackoverflow看到过一篇求助帖子《Is there any way to tell GDB to wait for a process to start and attach to it?》,大体问题:工具链上调用关系很长,父进程调用子进程,子进程调用孙进程,如此下去,最后有一个进程崩溃了,提问者期望在崩溃之前用GDB att…

Solana 与 DePIN 的双向奔赴,会带来 DePIN 之夏吗?

作者&#xff1a;LBank Labs 研究员 F.F 编译&#xff1a;TinTinLand 原文&#xff1a;https://medium.com/lbanklabs/new-anchor-of-solana-depin-b674d04d6980 太长不看版 在过去的一年里&#xff0c;我们观察到 Solana 和 DePIN 两者都呈现出了显著的增长。这不仅是极客科…

透视屏幕有哪些优点

透视屏幕的优点有以下几个方面&#xff1a; 互动性强&#xff1a;透视屏幕可以实现人机互动&#xff0c;观众可以通过触摸屏幕或使用其他交互设备与屏幕进行互动&#xff0c;增强了观众的参与感和体验感。 多样化的展示方式&#xff1a;透视屏幕可以采用多种展示方式&#xff…

GoLang编译型语言

Go&#xff08;Golang&#xff09;是一门编译型语言。Go 的源代码会被编译成机器码&#xff0c;生成与目标平台相关的可执行文件。这意味着在运行 Go 程序之前&#xff0c;需要先将代码编译成可执行文件&#xff0c;然后才能在目标计算机上运行。 Go 语言的编译器将源代码转换成…

cjson/cJSON.h: No such file or directory

具体错误 In file included from mosquitto_ctrl.c:19: ../../config.h:86:12: fatal error: cjson/cJSON.h: No such file or directory86 | # include <cjson/cJSON.h>| ^~~~~~~~~~~~~~~解决办法1&#xff08;无效&#xff09; sudo apt install -y libj…

牛客小白月赛84

A打靶 题目描述 小蓝非常喜欢玩FPS类游戏&#xff0c;最近他迷上了一款打靶游戏&#xff0c;已知总共会出现 n\mathit nn 个靶子&#xff0c;每次开枪如果打中了靶子则会得到 1\text 11 分&#xff0c;另外不论这次开枪打中与否&#xff0c;靶子都将消失&#xff0c;现在有 m…

mac中excel条件格式找到每一列的最大值并标红

假设现在excel有A1:R24组数据&#xff0c;最终效果如下 先选择要处理数据的第一列&#xff0c;然后点击【条件格式】-【新建规则】 style选择【classic】以及【Use a formula to determine which cells to format】&#xff0c;输入规则【C3MAX(C$3:C$24)】 注意这里C$3前面没…