욤미의 개발일지

Jupyter Notebook Output 초기화하는 방법 본문

Python

Jupyter Notebook Output 초기화하는 방법

욤미 2023. 2. 25. 16:29
728x90
반응형

Jupyter notebook을 사용하다보면 출력 결과(print 문)이 너무 많아서 브라우저에서 파일이 열리지 않는 경우가 있다.

이 때 Ipynb파일에서 output만 초기화주어 빈칸으로 만들어주면 다시 실행할 수 있다.

 

import sys
import io
import os
from nbformat import read, write

def clear_notebook(fname, new_fname):
    # read original file
    with io.open(fname, 'r', encoding = "utf-8") as f:
        nb = read(f, 4)
    # remove ouput
    for cell in nb.cells:
        if cell.cell_type == 'code':
            cell.outputs = [] # cell output clear
    # save new file
    base, ext = os.path.splitext(fname)
    new_ipynb = "%s_removed%s" % (base, ext)
    with io.open(new_fname, 'w', encoding='utf-8') as f:
        write(nb, f, 4)
        
# run
clear_notebook("Untitled.ipynb", "newfile.ipynb")

 

 참고

 

주피터 노트북 실행 결과 초기화 방법

이번 포스팅에서는 ipynb 파일에서 실행 결과를 초기화하는 방법을 소개한다. 이 방법은 코드를 구동하다가 출력 결과가 너무 많아 (print 문을 너무 많이 사용하거나, 그림을 많이 사용했거나 등),

gils-lab.tistory.com

728x90
반응형
Comments