cancel
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.
cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to run spark sql commands from ipywidget button click event

129876
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(

  description='Submit',

  disabled=False,

  tooltip='Submit'

)

out = widgets.Output()

def get_data(x):

  with out:

    out.clear_output()

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

button_01.on_click(get_data)

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

2 REPLIES 2

Atanu
Databricks Employee
Databricks Employee

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)

129876
New Contributor III

I'm getting an error running those lines:

error: not found: value CredentialContext

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group