Why this notebook is returning an error only when called by another notebook?

Saf4Databricks
Contributor

When I uncomment the last two lines of Called_Notebook.py and run it manually by itself, it correctly returns the output as:

Status: SUCCESS
Circle area: 50.26544

But when I comment out the last two lines of Called_Notebook.py and run it from the Caller_Notebook.py, it returns the output as "None" from the Exception block of the area_of_circle(r) function defined in the Called_Notebook.py. Question: Why the function is failing when called from Caller_Notebook.py ?

Called_Notebook.py:

dbutils.widgets.text("radius", "4")
r = float(dbutils.widgets.get("radius"))

status=""

try:
    def area_of_circle(r):
        return 3.14159 * r * r
    circle_area = area_of_circle(r)
    status = "SUCCESS"
except Exception as e:
    circle_area = None
    status = "ERROR"
    error_message = str(e)

# print(f"Status: {status}")
# print(f"Circle area: {circle_area}")

Caller Notebook:

try:
    output = dbutils.notebook.run("./Called_Notebook.py",60,{"radius":"4"})
    print(output)
except Exception as e:
    print("Child notebook failed:", e)

 

pradeep_singh
Contributor III

dbutils.notebook.run() returns only what the called notebook passes to dbutils.notebook.exit(). 

If your called notebook in the end add this 

dbutils.notebook.exit(f"{Value to return}")

 

 

Thank You
Pradeep Singh - https://www.linkedin.com/in/dbxdev

View solution in original post

Saf4Databricks
Contributor

Hi @pradeep_singh, your suggestion worked. Thank you for sharing your knowledge. Worth noticing that not including dbutils.notebook.exit(f"{Value to return}") raised the error in the exception block of the function inside the Called_Notebook - and that had confused me. Now, I know.