2016-06-13 5 views
0

Jupyter.kernel.execute (command)를 통해 Javascript 측에서 python 명령을 실행할 수 있습니다. 예를 들어 명령은 "a = 3"과 같을 수 있습니다. "a"라는 새 변수가 3으로 설정되어 있음을 알 수 있습니다.Jupyter.kernel.execute 명령을 통해 실행되는 python 명령을 Jupyter의 입력 셀에 표시하는 방법

수동으로 입력 한 것처럼이 명령을 입력 셀에 반향시키고 싶습니다. 가능할까요, 어떻게 할 수 있습니까?

답변

1

아주 간단한 예는 (노트북 셀에 아래의 코드를 붙여 넣습니다) 것 :

%%javascript 

// Function that accepts a string of code 
var create_and_execute_cell_below = function (code){ 
    var nb = Jupyter.notebook 

    // create cell below this one 
    nb.insert_cell_below() 

    // select cell below (the one we have created) 
    var cell = nb.select_next().get_selected_cell() 

    // set text in the cell 
    cell.set_text(code) 

    // execute cell 
    cell.execute() 
} 

// run the function created above with code 'k = 1' 
// and it will create a new cell, filled with 'k = 1' 
// and it will execute that cell. 
// if you run this cell using [ctrl] + [enter] only one cell 
// will be created. 
// if you run this cell using [Shift] + [enter] two cells 
// will be created, the one of the [Shift] + [enter] command 
// and the one of the function created above with the code. 
create_and_execute_cell_below('k = 1') 

나는 그것이 도움이되기를 바랍니다.

OTOH, the front-end API could be not very stable and there is a lack of documentation 및 일부 내용이 변경 될 수 있으며 위에 게시 된 코드가 필요한 것을 수행하는 최선의 방법이 아닙니다.

+0

지금은 잘 작동합니다. 청초한. 최소한 명시 적으로 kernel.execute를 사용하지 마십시오. cell.execute는이를 암묵적으로 사용할 수 있습니다. 마술은 다음 세포를 만들고 선택합니다. 그렇지 않으면 작동하지만 나타나지 않습니다. 감사. – user2800464