2

I am trying to create a notebook inside another notebook , the created notebook should contain both python code and sql code (using % sql ,% python ) .I need to run the created notebook from the parent notebook once it is created .Can anyone suggest a better way to do this .

I found something like dbutils.notebook.run() -which will help me to run the already existing notebook , but seeking a method to create a notebook first and run it later .Any suggestion is appreciable!!

Ansi P.K
  • 31
  • 4

1 Answers1

2

You can use the import command of the Databricks Workspace REST API.

Something like this (put the notebook content into content value):

import requests
import os
import json
import base64

ctx = json.loads(dbutils.notebook.entry_point.getDbutils().notebook().getContext().toJson())
host_name = ctx['extraContext']['api_url']
host_token = "your_PAT_token"
notebook_path = ctx['extraContext']['notebook_path']
new_path = os.path.join(os.path.dirname(notebook_path), 'New name')

content = "some code"

data = {
  "content": base64.b64encode(content.encode("utf-8")).decode('ascii'),
  "path": new_path,
  "language": "PYTHON",
  "overwrite": True,
  "format": "SOURCE"
}

response = requests.post(
    '{host_name}/api/2.0/workspace/import',
    headers={'Authorization': f'Bearer {host_token}'},
    json = data
  ).json()
Alex Ott
  • 80,552
  • 8
  • 87
  • 132
  • 1) I assume that 'true' will be 'True' - reserved word for python? 2) Please give me understanding to what you mean by 'extraContext', is that a reserved word or a place holder? – Mikee Feb 08 '22 at 12:56
  • 1
    `extraContent` is a field in the context dictionary – Alex Ott Feb 08 '22 at 13:15
  • In that case, will the api_token be auto generated or it should be supplied (excuse my naivety)? – Mikee Feb 08 '22 at 13:19
  • right now it's autogenerated, but really it's better supply it explicitly because it's not guaranteed that will be kept – Alex Ott Feb 08 '22 at 13:21
  • Got an error ` 'error_code': 'MALFORMED_REQUEST', 'message': 'Invalid JSON given in the body of the request - failed to parse given JSON' ` – Mikee Feb 08 '22 at 14:22
  • 1
    sorry, instead of `data = data` it should be `json = data` – Alex Ott Feb 08 '22 at 14:25
  • When I change the notebook content from the default you used (e.g. "some new code for new notebook"), I get ```Could not parse request object: Illegal white space character (code 0x20) as character #4 of 4-char base64 unit: can only used between units\n at [Source: N/A; line: -1, column: -1] ```. Any reason for this error and how to solve it? – Mikee Feb 08 '22 at 15:32
  • 1
    found another bug - forgot to base64 encode payload – Alex Ott Feb 08 '22 at 16:01
  • Asked a trailing question to this at . Please assist if you can. – Mikee Feb 09 '22 at 15:19