developer tip

jupyter 노트북의 셀 축소

copycodes 2020. 8. 13. 23:32
반응형

jupyter 노트북의 셀 축소


ipython Jupyter 노트북을 사용하고 있습니다. 화면에서 많은 공간을 차지하는 함수를 정의했다고 가정 해 보겠습니다. 세포를 접을 수있는 방법이 있습니까?

함수가 실행되고 호출 가능한 상태로 유지되기를 원하지만 노트북을 더 잘 시각화하기 위해 셀을 숨기거나 축소하고 싶습니다. 어떻게 할 수 있습니까?


jupyter contrib nbextensions파이썬 패키지는 노트북에서 사용할 수있는 코드 접는 확장이 포함되어 있습니다. 문서를 보려면 링크 (Github)를 따르십시오.

명령 줄을 사용하여 설치하려면 :

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user

관리를 더 쉽게하기 위해 jupyter nbextensions configurator패키지 도 추천합니다 . 설치된 모든 확장 기능을 쉽게 (비) 활성화 할 수있는 노트북 인터페이스에 추가 탭을 제공합니다.

설치:

pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user

셀을 만들고 다음 코드를 넣을 수 있습니다.

%%html
<style>
div.input {
    display:none;
}
</style>

이 셀을 실행하면 모든 입력 셀이 숨겨집니다. 다시 표시하려면 메뉴를 사용하여 모든 출력을 지울 수 있습니다.

그렇지 않으면 아래와 같은 노트북 확장을 시도 할 수 있습니다.

https://github.com/ipython-contrib/IPython-notebook-extensions/wiki/Home_3x


JupyterLab 은 셀 축소를 지원합니다. 왼쪽에있는 파란색 셀 막대를 클릭하면 셀이 접 힙니다.여기에 이미지 설명 입력


나는 비슷한 문제가 있었고 @Energya가 지적한 "nbextensions"가 매우 잘 작동하고 쉽게 작동했습니다. 노트북 확장구성자에 대한 설치 지침은 간단합니다 (Windows에서 anaconda로 시도했습니다) .

즉, 다음 확장에 관심이 있어야한다고 덧붙이고 싶습니다.

  • 입력 숨기기 | 이 확장을 사용하면 노트북에서 개별 코드 셀을 숨길 수 있습니다. 툴바 버튼을 클릭하면됩니다.입력 숨기기

  • 접을 수있는 제목 | 전자 필기장에 제목으로 구분 된 접을 수있는 섹션이있을 수 있습니다.접을 수있는 제목

  • 코드 폴딩 | 이것은 언급되었지만 완전성을 위해 추가했습니다.코드 폴딩


~ / .jupyter / custom / 안에 다음 내용으로 custom.js 파일을 만듭니다.

$("<style type='text/css'> .cell.code_cell.collapse { max-height:30px; overflow:hidden;} </style>").appendTo("head");
$('.prompt.input_prompt').on('click', function(event) {
    console.log("CLICKED", arguments)   
    var c = $(event.target.closest('.cell.code_cell'))
    if(c.hasClass('collapse')) {
        c.removeClass('collapse');
    } else {
        c.addClass('collapse');
    }
});

저장 후 서버를 다시 시작하고 노트북을 새로 고칩니다. 입력 레이블 (In [])을 클릭하여 모든 셀을 축소 할 수 있습니다.


hide_code 확장을 사용하면 개별 셀 및 / 또는 그 옆에있는 프롬프트를 숨길 수 있습니다. 다음으로 설치

pip3 install hide_code

이 확장에 대한 자세한 내용은 https://github.com/kirbs-/hide_code/방문 하십시오 .


먼저 Energya의 지시를 따르십시오.

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user

두 번째는 핵심입니다. Jupiter Notebook을 연 후 Nbextension 탭을 클릭합니다. 이제 웹 브라우저가 아닌 Nbextension에서 제공하는 검색 도구에서 "colla"를 검색 하면 "Collapsible Headings"라는 것을 찾을 수 있습니다.

이것이 당신이 원하는 것입니다!


Pan Yan 제안의 개선 된 버전도 있습니다. 코드 셀을 다시 표시하는 버튼을 추가합니다.

%%html
<style id=hide>div.input{display:none;}</style>
<button type="button" 
onclick="var myStyle = document.getElementById('hide').sheet;myStyle.insertRule('div.input{display:inherit !important;}', 0);">
Show inputs</button>

또는 파이썬 :

# Run me to hide code cells

from IPython.core.display import display, HTML
display(HTML(r"""<style id=hide>div.input{display:none;}</style><button type="button"onclick="var myStyle = document.getElementById('hide').sheet;myStyle.insertRule('div.input{display:inherit !important;}', 0);">Show inputs</button>"""))

확장 기능을 활성화하는 것 외에는 많이 할 필요가 없습니다.

http://localhost:8888/nbextensions?nbextension=collapsible_headings
http://localhost:8888/nbextensions?nbextension=codefolding/main

여기에 이미지 설명 입력

대부분의 경우 여기에서 모든 확장을 찾을 수 있습니다.

http://localhost:8888/nbextensions

여기에 이미지 설명 입력

참고 URL : https://stackoverflow.com/questions/33159518/collapse-cell-in-jupyter-notebook

반응형