我想以某种方式将我的 IPython.html.widgets
的值存储在我的 ipython 笔记本中。
有没有办法从单元格本身的代码修改当前单元格的元数据?
最佳答案
我不知道如何在笔记本中执行此操作,但我找到了一种使用自定义 preprocessor 来执行此操作的方法和 nbconvert。
您可以创建一个扩展 nbconvert.preprocessors.ExecutePreprocessor 的类。在 preprocess(或 preprocess_cell)方法中,添加用于将相关输出存储在单元元数据中的逻辑。
类似于:
class MyExecutePreprocessor(ExecutePreprocessor):
def preprocess_cell(self, cell, resources, index):
# Execute the cell normally
cell, resources = super().preprocess_cell(cell, resources, index)
# Add your magic here
cell.metadata['widgets'] = {'stuff':'that is cool'}
return cell, resources
然后您可以执行此预处理器 programatically ,或作为 nbconvert 的参数.
关于python - 修改单元格的元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23917036/