简体   繁体   中英

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.

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 ?

There is no separate separate API for appending a code to the notebook. 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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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