03-31-2023 11:13 AM
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.
04-04-2023 06:28 PM
@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:
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.
04-05-2023 01:26 AM
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.
So 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);
Could you please tell how to proceed further?
04-05-2023 09:43 PM
@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:
Meanwhile, paste the error instead of a screenshot. Thanks.
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