How to convert .py to .ipynb in Ubuntu 22.04

How to convert .py to .ipynb in Ubuntu 22.04

  • [jupyter nbconvert](#jupyter nbconvert)
  • p2j

最近看到大家在用jupyter notebook,我也试了一下,感觉还不错,不过,也遇到了一些问题,比方说,我有堆的.py文件,如果要一个一个将其复制-粘贴到jupyter notebook中工作量将是非常大的,所以,我就在想有没一种方法可以让我能够实现.py文件到.ipynb文件的转换。

jupyter nbconvert

关于这个问题,我也试着问了一下ChatGPT

根据ChatGPT的回复,我尝试着用命令

bash 复制代码
jupyter nbconvert --to notebook --execute utils.py --output utils.ipynb

转换,结果提示我的文件有问题,下面是我的原始文件内容:

bash 复制代码
(lanzhou) lwk@qwfys:~/Public/project/python/alink_tutorial_python/pyalink$ cat utils.py
from pyalink.alink import *
import os
from datetime import datetime
from math import floor

ROOT_DIR = "/home/lwk/data/pyalink/"

def generateSchemaString(colNames, colTypes):
    n = len(colNames)
    str = ''
    for i in range(n):
        if i>0 :
            str = str + ","
        str = str + colNames[i] + " " + colTypes[i]
    return str


def splitTrainTestIfNotExist(source, trainFilePath, testFilePath, ratio):
    if not(os.path.exists(trainFilePath) or os.path.exists(testFilePath)):
        spliter = SplitBatchOp().setFraction(ratio)
        
        source.link(spliter)
        
        spliter.link(
            AkSinkBatchOp().setFilePath(trainFilePath)
        )
        
        spliter.getSideOutput(0)\
            .link(
                AkSinkBatchOp().setFilePath(testFilePath)
            )
        
        BatchOperator.execute()
		
        
class Stopwatch :
    
    def __init__(self):
        self.timer_start = datetime.now()
        self.timer_end = datetime.now()
    
    def start(self):
        self.timer_start = datetime.now()
        
    def stop(self):
        self.timer_end = datetime.now()
        
    def reset(self):
        self.timer_start = datetime.now()
    
    def getElapsedTimeSpan(self):
        elapse = self.timer_end - self.timer_start
        
        r = "";
        if elapse.days > 0 :
            r += str(elapse.days) + " days  "

        if elapse.seconds >= 3600 :
            r += str(floor(elapse.seconds/3600)) + " hours  "

        if elapse.seconds >= 60 :
            r += str(floor(elapse.seconds%3600/60)) + " minutes  "

        if elapse.seconds > 0 :
            r += str(elapse.seconds%60) + " seconds  "

        if elapse.microseconds > 0 :
            r += str(elapse.microseconds/1000) + " milliseconds  "
            
        return r
(lanzhou) lwk@qwfys:~/Public/project/python/alink_tutorial_python/pyalink$ 

这个问题暂时先不管了,我们用另外一种方式来转换。

p2j

这里,我们先来安装p2j包,即:

bash 复制代码
(lanzhou) lwk@qwfys:~/Public/project/python/alink_tutorial_python/pyalink$ pip install p2j
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting p2j
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ee/5b/98fd10f7ba9fdfc99613c99040a87285dfc68392e5a059c22387ea814e39/p2j-1.3.2-py3-none-any.whl (19 kB)
Installing collected packages: p2j
Successfully installed p2j-1.3.2
(lanzhou) lwk@qwfys:~/Public/project/python/alink_tutorial_python/pyalink$

接下来,我们借助p2j来转换

bash 复制代码
(lanzhou) lwk@qwfys:~/Public/project/python/alink_tutorial_python/pyalink$ p2j utils.py 
Notebook utils.ipynb written.
(lanzhou) lwk@qwfys:~/Public/project/python/alink_tutorial_python/pyalink$ 

我们看到.pynb文件已经生成,接下来,我们在jupyter notebook中打开新生成的utils.ipynb文件,如下图所示。

至此,我们任务就算完成了。

参考文献

相关推荐
JUNIOR_MU3 个月前
【VMware VCF】使用 VCF Import Tool 将现有 vSphere 环境导入为 VI 域。
esxi·convert·nsx·import·vsphere·vcenter·vsan·vcenter server·vcf·vmware cloud foundation·nsx manager·vi workload domain·sddc manager·vcf import tool·bundle transfer utility·compatibility matrix
JUNIOR_MU3 个月前
【VMware VCF】使用 VCF Import Tool 将现有 vSphere 环境转换为管理域。
convert·import·vsphere·vsan·vsan hci·vcf·vmware cloud foundation·sddc manager·vcf import tool
微小冷1 年前
WPF实现文字纵向排布的TabItem
wpf·convert·tabcontrol·tabitem·纵向显示
qwfys2001 年前
How to install mongodb-7.0 as systemd service with podman
mongodb·podman·install·how·systemctl
qwfys2001 年前
How to install mongodb 7.0 to Ubuntu 22.04
mongodb·ubuntu·install·how
qwfys2001 年前
How to install mysql 8.0 based on podman
mysql·podman·install·how
qwfys2001 年前
How to use jupyterlab in Ubuntu 22.04
ubuntu·install·jupyterlab·how
talk_81 年前
第九十二回 在Flutter中解析JSON数据
flutter·json·convert