0

I want combine my 2 different notebooks and add one condition on received parameter. If parameter is True then run all cell of that notebook and if parameter is false only run added code from another notebooks that will be few bottom cells.

Basically i want to reduce the loading time of two notebook so combining them to one

 dbutils.widgets.text("condition", "True", "Condition: (True or False)") 
condition_str = dbutils.widgets.get("condition") condition = condition_str.lower() == "true"
def run_cell(cell_id):   dbutils.notebook.run(f"/FullNotebookPath", timeout_seconds=0, arguments={"cell_id": cell_id})

If condition:

for cell_number in range(1, 11):   run_cell(cell_number)

else: 

For cell_number in range(6, 11): run_cell(cell_number)

But the execution is failing.. Its working if i run single cell without for loop.

toyota Supra
  • 3,181
  • 4
  • 15
  • 19

1 Answers1

0

I tried to reproduce your work in my environment.

Add required code in the main notebook as below.

cell 1

dbutils.widgets.text("cell_id", "1")
cell_no = dbutils.widgets.get("cell_id")

cell 2

dbutils.notebook.exit(f"Hi from cell {cell_no}")

cell 3

dbutils.notebook.exit(f"Hi from cell {cell_no}")

cell 4

dbutils.notebook.exit(f"Hi from cell {cell_no}")

enter image description here

Like this add your code to the corresponding cells, and call your notebook in for loop with cell_id as argument.

dbutils.widgets.text("condition", "True", "Condition: (True or False)")
condition_str = dbutils.widgets.get("condition")
condition = condition_str.lower() == "true"

def run_cell(cell_id):
    return_values.append(
        dbutils.notebook.run(path, timeout_seconds=0, arguments={"cell_id": cell_id})
        )

Output:

enter image description here

return_values = []
if condition:
    for cell_number in  range(2, 7):
        run_cell(cell_number)
else:
    for cell_number in  range(5, 7):
        run_cell(cell_number)
        
print(return_values)

enter image description here

What your given is working fine. So, try to debug it.

JayashankarGS
  • 1,501
  • 2
  • 2
  • 6