cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
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
Esteemed Contributor
Esteemed Contributor
1 ACCEPTED SOLUTION

Accepted Solutions

shan_chandra
Esteemed Contributor
Esteemed Contributor

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
Esteemed Contributor
Esteemed Contributor

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 🎯.