How to retrive the notebook contents or the url of a particular cell in Google colab

偶尔善良 提交于 2020-11-30 00:18:25

问题


Is there a way to get or compute the URL to a cell in Colab? I know you can click on a link button to get the url, or click on the TOC and get the #scrollTo=J1nFJPC9V1-X part of the URL, but I want a way to generate this.

The use case is I search colabs for headlines, and I want to generate a link that opens a colab to that heading. Suppose I know the id for a colab, and I know it has a heading called "Printing arrays". I want to generate a full link that opens it at that cell.

Is it possible to compute the scrollTo part, or retrieve it somehow?


回答1:


Here's an example snippet that will find the cell ID with particular contents:

from google.colab import _message

# Load the notebook JSON.
nb = _message.blocking_request('get_ipynb')

# Search for the markdown cell with the particular contents.
for cell in nb['ipynb']['cells']:
  if (cell['cell_type'] == 'markdown' and 
      '\n'.join(cell['source']).find('## Printing arrays') >= 0):
    print (f'Cell ID for header is #{cell["metadata"]["id"]}')

Here's a complete example notebook: https://colab.research.google.com/drive/11jOuhB7wzm1d2P85mrmhBWycogJ_9F0q



来源:https://stackoverflow.com/questions/63671205/how-to-retrive-the-notebook-contents-or-the-url-of-a-particular-cell-in-google-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!