Forum Discussion

PANDAmonium's avatar
PANDAmonium
Resolver IV
1 year ago
Solved

Passing Arguments to Notebooks with Magic

Hi All,

 

I'm trying to figure out how to use magic commands to run another notebook with an argument that has a variable as the value. Hopefully the example below explains it better. The idea is to pass a pipeline parameter to the notebook, for example a True/False variable on whether it's being run on dev, and have that also apply to another notebook being ran using magic.

 

Notebook1

isDev = True

%run Notebook2 {'isDev': isDev}

Notebook2

     print(str(isDev))

 

When I use a string instead of a variable as the argument value it works. Otherwise I get this error:

 

MagicUsageError: Cannot parse notebook parameters. More details please visit https://go.microsoft.com/fwlink/?linkid=2270136 --> JsonReaderException: Unexpected character encountered while parsing value: i. Path 'isDev', line 1, position 9.

 

Thanks!

  • That still gave the same error, but I figured it out.

     

    Unexpected character encountered while parsing value: i. Path 'isDev', line 1, position 10.

     

    The variables set in the parent notebook already get passed to this child notebook, so passing the parameter wasn't even needed and what worked was simply:

     

    Notebook1

    isDev = True

    %run Notebook2

    Notebook2

    print(str(isDev))

     

    Also the following worked if I wanted to run it like a script, but the using the magic command was better and 10x faster.

     

    Notebook1

    params = {"isDev":True}
    mssparkutils.notebook.run("Notebook2", 120, params)

    Notebook2

    print(str(isDev))

     

    Thanks!

2 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi PANDAmonium 

    Thank you for reaching out microsoft fabric community forum.
    Convert the dictionary into a JSON string using json.dumps() before passing it to %run.

    Notebook1

    isDev = True

    %run Notebook2 {json.dumps({'isDev': isDev})}

    Notebook2
    print(str(isDev)) # This should now work correctly

     

    • json.dumps({'isDev': isDev}) converts the dictionary into a valid JSON string.
    • %run Notebook2 {json.dumps({'isDev': isDev})} correctly passes the argument in a format that the %run magic command can parse.

    This might  help you to resolve the JsonReaderException error and allow isDev to be correctly passed to Notebook2.
    If you need further assistance please feel free to reach us.
    If this solution helps, please consider giving us Kudos and accepting it as the solution so that it may assist other members in the community
    Thank you.

    • PANDAmonium's avatar
      PANDAmonium
      Resolver IV

      That still gave the same error, but I figured it out.

       

      Unexpected character encountered while parsing value: i. Path 'isDev', line 1, position 10.

       

      The variables set in the parent notebook already get passed to this child notebook, so passing the parameter wasn't even needed and what worked was simply:

       

      Notebook1

      isDev = True

      %run Notebook2

      Notebook2

      print(str(isDev))

       

      Also the following worked if I wanted to run it like a script, but the using the magic command was better and 10x faster.

       

      Notebook1

      params = {"isDev":True}
      mssparkutils.notebook.run("Notebook2", 120, params)

      Notebook2

      print(str(isDev))

       

      Thanks!