Home > Mobile >  how to run code with %run inside a string in databricks
how to run code with %run inside a string in databricks

Time:01-11

Hope all are doing well :D I am trying to create a function with the path as a parameter. Inside that function I would like to append that path to %run. And then run the notebook itself.

What did I do was:

def another_func(path):
    nb = "%run"   str(path)
    return nb

This return the everything in between "". I tried eval(), exec() but nothing works, it does return the following error: "invalid syntax (, line 1)".
I also tried instead of "nb = "%run" str(path)", %run $path but it doesn´t work as well :( Thank you so much in advance :)

CodePudding user response:

I have tried same thing, but it worked

def another_func(path):
  nb = "%run"    " "   str(path) 
  return nb
print(another_func("/sample/demo"))

Below is the one which i tried

CodePudding user response:

The %run is a special directive and doesn't allow right now to parametrize the path. You can pass path to dbutils.notebook.run, but it will execute that notebook as a separate job, not including the functions & variables into the context of the current notebook.

  • Related