cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

How to create a drop-down widget in Databricks notebook with choices retrieved using subquery over a table in SQL, python?

shan_chandra
Honored Contributor III
Honored Contributor III
1 ACCEPTED SOLUTION

Accepted Solutions

shan_chandra
Honored Contributor III
Honored Contributor III

Please refer to the below widget example using SQL

%sql 
DROP VIEW IF EXISTS tempTable;
CREATE temporary view tempTable AS SELECT 'APPLE' as a UNION ALL SELECT 'ORANGE' as a UNION ALL SELECT 'BANANA' as a;
CREATE WIDGET DROPDOWN fruits DEFAULT 'ORANGE' CHOICES SELECT a from tempTable

python example for dropdown with values passed from the table. pass the values from the temp table (in the above example) through data frame df.

df = spark.sql("""select a from tempTable""")
dbutils.widgets.dropdown("fruits3", "ORANGE", [row[0] for row in df.collect()])

View solution in original post

2 REPLIES 2

shan_chandra
Honored Contributor III
Honored Contributor III

Please refer to the below widget example using SQL

%sql 
DROP VIEW IF EXISTS tempTable;
CREATE temporary view tempTable AS SELECT 'APPLE' as a UNION ALL SELECT 'ORANGE' as a UNION ALL SELECT 'BANANA' as a;
CREATE WIDGET DROPDOWN fruits DEFAULT 'ORANGE' CHOICES SELECT a from tempTable

python example for dropdown with values passed from the table. pass the values from the temp table (in the above example) through data frame df.

df = spark.sql("""select a from tempTable""")
dbutils.widgets.dropdown("fruits3", "ORANGE", [row[0] for row in df.collect()])

Hi @Shanmugavel Chandrakasu​ , Thank you for sharing the informative posts on our Community. You're a gem to our community 🎯.

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.