Azure | Working with Widgets
TL;DR
Don’t want to read the post, then explore this Azure Notebook
Requirements
Define needed moduls and functions
from datetime import datetime import pyspark.sql.functions as F
Create DataFrame for this post:
df = spark.sql("select * from diamonds") df.show()
Working with Widgets
Default Widgets
dbutils.widgets.removeAll() dbutils.widgets.text("W1", "1", "Text") dbutils.widgets.combobox("W2", "3", [str(x) for x in range(1, 10)], "Combobox") dbutils.widgets.dropdown("W3", "4", [str(x) for x in range(1, 10)], "Dropdown")
Multiselect Widgets
list = [ f"Square of {x} is {x*x}" for x in range(1, 10)] dbutils.widgets.multiselect("W4", list[0], list, "Multi-Select")
Monitor the changes when selection values
print("Selection: ", dbutils.widgets.get("W4")) print("Current Time =", datetime.now().strftime(Filter Query by widgets
Prepare widgets
dbutils.widgets.removeAll() df = spark.sql("select * from diamonds") vals = [ str(x[0]) for x in df.select("cut").orderBy("cut").distinct().collect() ] dbutils.widgets.dropdown("Cuts", vals[0], vals) vals = [ str(x[0]) for x in df.select("carat").orderBy("carat").distinct().collect() ] dbutils.widgets.dropdown("Carat", vals[0], vals)Now, change some values
filter_cut = dbutils.widgets.get("Cuts") df=spark.sql(f"select * from diamonds where cut='{filter_cut}'").show()