简体   繁体   English

使用单元格链接功能运行代码(在 Google Colab notebooks 中)

[英]Using cell link function to run code (in Google Colab notebooks)

I'm looking at the "Link to cell" functionality in Google Colab.我正在查看 Google Colab 中的“链接到单元格”功能。 Using the three dots to the right of the cell, you can get a link specifically to the contents of that cell in that notebook to link to later in that notebook or others.使用单元格右侧的三个点,您可以获得一个专门指向该笔记本中该单元格内容的链接,以便稍后链接到该笔记本或其他笔记本中。 Is there a way to use this functionality to run the code contained with in the linked cell?有没有办法使用此功能来运行链接单元格中包含的代码?

For example, if I have two notebooks: notebookA & notebookB.例如,如果我有两个笔记本:notebookA 和 notebookB。

  • notebookA contains the master code for a scoring function notebookA 包含评分函数的主代码
  • notebookB is the current notebook I'm working in notebookB 是我正在使用的当前笔记本

Is there a way to create a cell in notebookB and use the link to the cell or cells in notebookA to run them within notebookB?有没有办法在 notebookB 中创建一个单元格并使用 notebookA 中一个或多个单元格的链接在 notebookB 中运行它们? I'm looking for a way to have Colab notebooks that contain master versions of code (including functions) that I use across several other Colab notebooks, so that when I update that code in the master version each of the other notebooks has the most up-to-date version of that code without copying and pasting it over each time.我正在寻找一种方法来让 Colab 笔记本包含我在其他几个 Colab 笔记本中使用的主版本代码(包括函数),这样当我在主版本中更新该代码时,其他每个笔记本都有最多该代码的最新版本,无需每次都复制和粘贴。

One option you can use is this:您可以使用的一个选项是:

-Save the master code in a file mymodule.py - 将主代码保存在文件 mymodule.py 中

-Put "mymodule.py" in your google drive. - 将“mymodule.py”放入您的谷歌驱动器中。

-Mount your drive using this button: - 使用此按钮安装驱动器:

在此处输入图片说明

-Append path and import "mylib.py": -附加路径并导入“mylib.py”:

import sys
sys.path.append('/content/gdrive/My Drive')
import mylib

-Now you can use a function from "mylib.py" like in any module: - 现在您可以像在任何模块中一样使用“mylib.py”中的函数:

mylib.myfunc()

您可能想从fastai看这门课程,从28:00开始。

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

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