简体   繁体   English

将命令动态插入到 Databricks 中的现有笔记本中

[英]Dynamically insert a command into an existing notebook in Databricks

Off the back this great answer in this post: Create a notebook inside another notebook in Databricks Dynamically using Python where it was shown how to create an entirely new notebook dynamically, my question is on inserting a command into an existing notebook.在这篇文章中给出了这个很好的答案: Create a notebook inside another notebook in Databricks Dynamically using Python其中展示了如何动态创建一个全新的笔记本,我的问题是将命令插入现有笔记本。

Given that鉴于

ctx = json.loads(dbutils.notebook.entry_point.getDbutils().notebook().getContext().toJson())
notebook_path = ctx['extraContext']['notebook_path']

content = "some code"

is it possible to append content into the notebook at notebook_path ?是否可以将 append content放入 notebook_path 的notebook_path中?

There is no separate separate API for appending a code to the notebook.没有单独的单独的 API 用于将代码附加到笔记本上。 But you can useWorkspace API to export given notebook, decode base64-encoded content, append the code, and import it again with overwrite parameter set to the true .但是您可以使用工作区 API导出给定的笔记本,解码 base64 编码的内容,append 代码,然后将overwrite参数设置为true再次导入。

It's better to add following line:最好添加以下行:

# COMMAND ----------

to the begin of your new code so it will be treated as a new cell in the notebook.到新代码的开头,这样它将被视为笔记本中的新单元格。

Also, the permissions of the notebook could be reset, and if you want to avoid that, then you can use Notebook Permissions API to retrieve current permissions, and then set it back after import.另外,notebook的权限可能会被重置,如果你想避免这种情况,那么你可以使用Notebook Permissions API来获取当前的权限,然后在导入后重新设置。

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

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