gpt4 book ai didi

python - Jupyter笔记本: how to find the cell number from which python script function is called?

转载 作者:太空宇宙 更新时间:2023-11-03 19:53:55 27 4
gpt4 key购买 nike

假设我有一个名为 my_script.py 的 Python 脚本,其中包含一个名为 my_func() 的函数。我将此脚本导入 Jupyter Notebook,以便可以在单元格中运行 my_func()

我的问题是,在my_func()的定义中,如何找到它是从哪个单元格号码调用的,以便我可以记录单元格号码?我可以使用任何 API 吗?

类似于

# my_script.py

def my_func():
# find which cell this is called from
cell_number_called_from = some_IPython_history_class_api.get_current_cell()

# log the cell number and do the rest of my things

最佳答案

Jupyter Notebook 单元格中的以下 javascript 代码段将为您提供该单元格的索引:

%%javascript
var output_area = this;
// find my cell element
var cell_element = output_area.element.parents('.cell');
// which cell is it?
var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);
console.log(cell_idx)

关于python - Jupyter笔记本: how to find the cell number from which python script function is called?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59652848/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com