Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

drmikecrowe
drmikecrowe

Posted on • Originally published atpinnsg.com on

Making Databricks Widgets Smarter

Image description

Have you ever wished the Databricks widgets were a little more intelligent? When you change one, the others adjust?

That’s not too hard, but it’s a bit tricky. Here’s the secret: The widgets must be replaced as they change!

Dynamic Widget Replacement

For example:

Let’s say you have a date input, and whenever it changes, you want a second widget to change. That second widget needs to be suffixed with an increasing number as the contents change. Consider:

    if interest_date != last_interest_date:        try:            dbutils.widgets.remove(f"issue{index1}")        except:            pass        index1 += 1    dbutils.widgets.dropdown(f"issue{index1}", "", ...)
Enter fullscreen modeExit fullscreen mode

You need the try/catch the first time thru because the remove will fail. Et Voila, changing widgets.

A Working Example

Here’s a functioning example:

interest_dates = {    "20230730": [        {"issue_name": "Issue 1", "upcs": ["1234", "5678"]},        {"issue_name": "Issue 2", "upcs": ["9876", "5432"]},    ],    "20240114": [        {"issue_name": "Issue 3", "upcs": ["1111", "2222"]},        {"issue_name": "Issue 4", "upcs": ["3333", "4444"]},    ],}last_interest_date, last_issue = (None, None)index1, index2 = (1, 1)dbutils.widgets.removeAll()from random import samplefrom time import sleepkeys = sorted(list(interest_dates.keys()))dbutils.widgets.removeAll()dbutils.widgets.dropdown(    "date", "", [""] + [str(k) for k in sorted(interest_dates)], "1. Date")interest_date = dbutils.widgets.get("date")if interest_date:    if interest_date != last_interest_date:        try:            dbutils.widgets.remove(f"issue{index1}")            dbutils.widgets.remove(f"upc{index2}")        except:            pass        index1 += 1        index2 += 1        last_interest_date = interest_date    dbutils.widgets.dropdown(        f"issue{index1}",        "",        [""] + [k["issue_name"] for k in interest_dates[interest_date]],        "2. Issue",    )    issue = dbutils.widgets.get(f"issue{index1}")    if issue:        if issue != last_issue:            try:                dbutils.widgets.remove(f"upc{index2}")            except:                pass            index2 += 1            last_issue = issue        current_issue = [            k for k in interest_dates[interest_date] if k["issue_name"] == issue        ].pop(0)        dbutils.widgets.dropdown(            f"upc{index2}",            "",            [""] + current_issue["upcs"],            "3. UPC",        )        upc = dbutils.widgets.get(f"upc{index2}")        if upc:            displayHTML(f"You selected: <b>{upc}</b>")
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A polyglot programmer who loves solving problems and discovering new technologies
  • Location
    Wilmington, NC area
  • Education
    BSEE
  • Work
    Principal Consultant at Pinnacle Solutions Group
  • Joined

More fromdrmikecrowe

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp