Showing results for 
Search instead for 
Did you mean: 
Data Governance
Join discussions on data governance practices, compliance, and security within the Databricks Community. Exchange strategies and insights to ensure data integrity and regulatory compliance.
Showing results for 
Search instead for 
Did you mean: 

Unable to run spark sql commands from ipywidget button click event

New Contributor III

I'm unable to run any command that queries data from the unity catalog within a function that executes in the event of an ipywidget button click. Code block below. I cannot do queries such as spark.sql(f"SHOW SCHEMAS;") or spark.sql(f"select * from database.table")

[Start code block]

import ipywidgets as widgets

button_01 = widgets.Button(





out = widgets.Output()

def get_data(x):

  with out:


    print(spark.sql(f"SHOW SCHEMAS;").collect())


display(button_01, out)

[End code block]

The error it gives is "Py4JJavaError: An error occurred while calling o332.sql.

: java.lang.IllegalStateException: No api token found in Credential Context"

Also to note, I am able to run these spark sql commands alone in a cell, just not in a function with ipwidgets


Esteemed Contributor
Esteemed Contributor

can you try to println out?

val databricksApiTokenKey = CredentialContext.INHERITED_PROPERTY_DATABRICKS_API_TOKEN

val databricksApiCredentialOpt = CredentialContext.getCredential(databricksApiTokenKey)

val rawUrlProp = spark.sparkContext.getLocalProperty(SparkContext.API_URL_PROPERTY)

New Contributor III

I'm getting an error running those lines:

error: not found: value CredentialContext

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!