cancel
Showing results for 
Search instead for 
Did you mean: 
Machine Learning
Dive into the world of machine learning on the Databricks platform. Explore discussions on algorithms, model training, deployment, and more. Connect with ML enthusiasts and experts.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to call a python function from displayHTML javascript code?

Hariprasad94
New Contributor II

python - How to use IPython.notebook.kernel.execute in Azure databricks? - Stack Overflow

In standard jupyter notebook, we could use IPython.notebook.kernel.execute to call a python function, in Azure databricks IPython seems to be not exposed in browser DOM global scope.

3 REPLIES 3

Anonymous
Not applicable

@hariprasad T​ :

In Azure Databricks, which is a cloud-based service for Apache Spark and big data processing, the notebook environment does not expose IPython directly in the browser DOM global scope as it is done in standard Jupyter notebooks. However, you can achieve similar functionality by using the dbutils.notebook.run function, which allows you to call a Python function from JavaScript code in a Databricks notebook.

Here's an example of how you can use dbutils.notebook.run to call a Python function from JavaScript code in a Databricks notebook:

  1. Define a Python function in a Databricks notebook cell:
def my_python_function(arg1, arg2):
    # Your Python code here
    result = arg1 + arg2
    return result

2) In another cell, use dbutils.notebook.runto call the Python function from JavaScript code:

// JavaScript code
var pythonCode = `
def my_python_function(arg1, arg2):
    # Your Python code here
    result = arg1 + arg2
    return result
`
 
// Call the Python function with arguments and get the result
var result = dbutils.notebook.run(
  'my_notebook_path',
  0,
  {
    'arg1': 'Hello',
    'arg2': 'World',
    'pythonCode': pythonCode
  }
)
 
// Display the result in the notebook
displayHTML(result);

In this example, my_notebook_path is the path to the notebook where the Python function is defined,

arg1 and arg2 are the arguments passed to the Python function, and pythonCode is the Python code for the function itself. The dbutils.notebook.run function takes the notebook path, cell index, and a dictionary of parameters as arguments, and returns the result of executing the Python function. You can then display the result in the notebook using displayHTMLfunction or use it for further processing in your JavaScript code. Note that the pythonCode parameter in dbutils.notebook.run should be a string representation of the Python code. Make sure to define your Python function and call dbutils.notebook.run in separate notebook cells as they are executed in separate contexts. Also, be mindful of passing any sensitive data as arguments to the Python function and handle it securely in your code.

Thanks for responding @Suteja Kanuri​. in the example you provided the 2nd block has javascript code. I couldn't figure out how to use var in python cell.

Im getting invalid syntax error since var isnt expected keyword for python cell.

imageSo i assumed, the second block is python code and changed it accordingly.

arg1 = "Hello";
arg2 = "World";
 
 
pythonCode = """
def my_python_function(arg1, arg2):
    result = "{}" + "{}"
    return result.format(arg1, arg2)
""".format(arg1, arg2);
 
 
result = dbutils.notebook.run(
  "/Users/<user-id>/<notebook-name>",
  0,
  {pythonCode: pythonCode}
);
 
displayHTML(result);

image 

Could you please tell how to proceed further?

Anonymous
Not applicable

@HardikGupta​ :

The Py4JJavaError is a common error that can occur when running Python code in Databricks. This error is usually caused by a problem with the code or configuration of your Databricks environment. Here are a few things you can try to resolve the Py4JJavaError:

  1. Check your code for syntax errors or logical mistakes. Make sure that all of the variable names are spelled correctly and that you have not used any Python keywords or reserved words incorrectly.
  2. Check your Databricks configuration. Make sure that you have configured your Databricks environment correctly, and that you have the correct permissions to run the code.
  3. Check the logs in the Databricks workspace for more information about the error. The logs may provide additional details about the cause of the error.
  4. Try running your code in a new Databricks cluster. Sometimes the Py4JJavaError can occur if there is a problem with the current cluster, so starting a new cluster may help resolve the issue.

Meanwhile, paste the error instead of a screenshot. Thanks.

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!