如何使通过 Jupyter.kernel.execute 命令执行的 python 命令在 Jupyter Notebook 的输入单元格中可见
How to make a python command executed via Jupyter.kernel.execute command visible in an Input Cell in Jupyter Notebook
我可以通过 Jupyter.kernel.execute(命令)从 Javascript 端执行 python 命令。例如,命令可能类似于 "a = 3"。我可以看到一个名为 "a" 的新变量设置为 3,没问题。
我希望在 INPUT CELL 中回显此命令,就好像它是手动输入的一样。可以吗,怎么做到的?
一个非常简单的例子是(将下面的代码粘贴到笔记本单元格中):
%%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 和一些东西可能会改变,也许上面发布的代码不是做你需要的最好的方法。
我可以通过 Jupyter.kernel.execute(命令)从 Javascript 端执行 python 命令。例如,命令可能类似于 "a = 3"。我可以看到一个名为 "a" 的新变量设置为 3,没问题。
我希望在 INPUT CELL 中回显此命令,就好像它是手动输入的一样。可以吗,怎么做到的?
一个非常简单的例子是(将下面的代码粘贴到笔记本单元格中):
%%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 和一些东西可能会改变,也许上面发布的代码不是做你需要的最好的方法。