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
?
CodePudding user response:
There is no separate separate API for appending a code to the notebook. But you can use Workspace 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.