1

I have a Databricks Python notebook that reads in a parameter from ADF using:

Program_Name = dbutils.widgets.get("Program_Name")

Is there an IF statement or something similar I can do in the notebook code, such that when I run the notebook interactively, it will substitute the call to dbutils with a plain assignment? Logically I want something like:

if running in ADF:
   Program_Name = dbutils.widgets.get("Program_Name")
else:
   Program_Name = 'ABC123'

If such a thing is possible, it beats the alternative of having to comment out the dbutils call every time I modify the rest of the notebook :) I've done similar things so that a script can be run from Jupyter/PyCharm or from the command line, but am not aware of anything that tells the python interpreter it's been called from ADF.

Many thanks!

Simon Norton
  • 95
  • 11

2 Answers2

2

Instead of catching up exception, it's just easier to create widget explicitly, and set default value (see docs).

dbutils.widgets.text("Program_Name", "ABC123", "Program name")
Program_Name = dbutils.widgets.get("Program_Name")

This has following benefits:

  1. The code is simpler - you don't need to have do any try/catch
  2. If necessary you can pass another program name even if you run notebook interactively
Alex Ott
  • 80,552
  • 8
  • 87
  • 132
0

You could call the notebook with a prefix when running it from ADF. For example, you would call the notebook with Program_Name = "ADF_prog_name"

Program_Name = dbutils.widgets.get("Program_Name")
if not(Program_Name.contains('ADF')):
   Program_Name = 'ABC123'
Axel R.
  • 1,141
  • 7
  • 22
  • 1
    Thanks Axel: Problem is the call to dbutils throws an error so the "if" statement never gets run - but that made me realize it's an error, not a crash. If you want to edit your answer, the strategy works as a try-except instead of an "if" statement. Your answer still lead me to the solution! – Simon Norton Jun 04 '21 at 18:46