2017-02-15 3 views
2

%%time%%timeit 마술은 Jupyter 또는 iPython 노트북에서 단일 셀의 타이밍을 활성화합니다.Jupyter 노트북의 모든 셀에서 타이밍 매직을 활성화하는 방법은 무엇입니까?

Jupyter 노트북의 모든 셀에서 타이밍을 켜고 끄는 기능이 있습니까?

This question과 관련되어 있지만 모든 셀에서 특정 마법을 자동으로 사용하도록 설정하는 것과 관련된 일반적인 질문에 대한 답변은 없습니다.

답변

2

이 할 수있는 해키 방법은 (보통 ~/.jupyter/custom/custom.js에 위치)를 custom.js 파일을 통해입니다

도구 모음이 here 위치하고 있으며 그것이 내가 오프이 대답을 기반 무엇되어있는 버튼을 만드는 방법의 예. 활성화 버튼을 누르면 모든 셀에 원하는 마법의 문자열 형식이 추가되고 비활성화 버튼은 str.replace을 사용하여 "해제"합니다.

define([ 
    'base/js/namespace', 
    'base/js/events' 
], function(Jupyter, events) { 
    events.on('app_initialized.NotebookApp', function(){ 
     Jupyter.toolbar.add_buttons_group([ 
      { 
       'label' : 'enable timing for all cells', 
       'icon' : 'fa-clock-o', // select your icon from http://fortawesome.github.io/Font-Awesome/icons 
       'callback': function() { 
        var cells = Jupyter.notebook.get_cells(); 
        cells.forEach(function(cell) { 
         var prev_text = cell.get_text(); 
         if(prev_text.indexOf('%%time\n%%timeit\n') === -1) { 
          var text = '%%time\n%%timeit\n' + prev_text; 
          cell.set_text(text); 
         } 
        }); 
       } 
      }, 
      { 
       'label' : 'disable timing for all cells', 
       'icon' : 'fa-stop-circle-o', // select your icon from http://fortawesome.github.io/Font-Awesome/icons 
       'callback': function() { 
        var cells = Jupyter.notebook.get_cells(); 
        cells.forEach(function(cell) { 
         var prev_text = cell.get_text(); 
         var text = prev_text.replace('%%time\n%%timeit\n',''); 
         cell.set_text(text); 
        }); 
       } 
      } 
      // add more button here if needed. 
     ]); 
    }); 
}); 
+0

환상적인 답변이지만 회사의 Jupyter 서버에서 실행 중이며 테스트하기 위해 'custom.js'에 대한 액세스 권한이 있는지 확실하지 않습니다. – abeboparebop

+0

마지막으로 다른 시스템에서이를 테스트 할 수있는 기회를 얻었습니다. 서면으로, 대답은별로 의미가 없습니다 - 모든 세포에서'%% time'과'%% timeit'을 모두 사용하는 것은 유용하지 않습니다. 그래서 당신의 대답은'%% time \ n %% timeit \ n'이라고 말하면서 그것을 %% time \ n으로 대체했습니다. 그러나 일반적인 접근 방식은 잘 작동합니다. 감사! – abeboparebop

+0

한편, 'fa-clock-o'아이콘은 작동하지만'fa-stop-circle-o'는 작동하지 않습니다. Jupyter 코어 버전 4.2.0. – abeboparebop

관련 문제