简体   繁体   English

Jupyter 笔记本模板单元格

[英]Jupyter notebook template cells

Is there a way to easily include template cells that run shell commands in a notebook.有没有办法在笔记本中轻松包含运行 shell 命令的模板单元。 I have some setup scripts for notebooks on Google Collab that install dependencies etc. They are common to many notebooks any currently if I need to update the cell I have to do it in every notebook.我在 Google Collab 上有一些用于安装依赖项等的笔记本的设置脚本。如果我需要更新必须在每个笔记本中执行的单元格,它们对于当前的许多笔记本来说都很常见。 Is it possible to create some kind of reference?是否可以创建某种参考?

As an example the code below checks a list of dependencies and installs them if not available:例如,下面的代码检查依赖项列表并在不可用时安装它们:

try:
    import importlib
    for name, path in DEPENDENCIES:
        importlib.import_module(name)
except (ImportError, ModuleNotFoundError):
    if 'google.colab' in str(get_ipython()):
        print('Running on CoLab')
        from google.colab import drive
        drive.mount('/content/drive')

        !mkdir -p ~/.access
        ...

You can create a function in a regular python module and import that python module in all the jupyter notebooks.您可以在常规 python 模块中创建 function 并在所有 jupyter 笔记本中导入该 python 模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM