Search code examples
pythonazure-databricks

Azure Databricks parameter transfer between notebooks using Python


The documentation by Microsoft at https://learn.microsoft.com/en-us/azure/databricks/notebooks/notebook-workflows says that you can run another notebook and pass parameters by doing the following:

notebook1:

result = dbutils.notebook.run("notebook2", 60, {"argument": "data", "argument2": "data2"})
print(f"{result}")

But it doesnt say how I can fetch the paramters argument and argument2 in my notebook2.

notebook2:

argument = ??
argument2 = ??
print(f"argument={argument} and argument2={argument2}")
dbutils.notebook.exit("Success")

How can I get get parameters in notebook2?


Solution

  • The document provided an answer for this. In order to get the parameters passed from notebook1 you must create two text widgets using dbuitls.widgets.text() in notebook2. Now use the dbuitls.widgets.get() method to get the values of these parameters.

    You can try using the following code:

    • Notebook1
    result = dbutils.notebook.run("nb2", 60, {"argument": "data", "argument2": "data2"}) 
    print(f"{result}") 
    
    • Notebook2
    dbutils.widgets.text("argument","argument_default") 
    argument = dbutils.widgets.get("argument") 
      
    dbutils.widgets.text("argument2","argument2_default") 
    argument2 = dbutils.widgets.get("argument2") 
      
    ans = argument+'    '+argument2 
      
    #print(f"argument={argument} and argument2={argument2}") 
    dbutils.notebook.exit(ans) 
    

    When you execute the notebook1 to run notebook2, the notebook2 runs successfully with the exit value as shown below:

    data data2 
    

    Note: If pass only one value from, then the other argument in notebook2 takes the default value mentioned in dbutils.widgets.text() (2nd parameter).