Can i return more than 1 value from databricks notebook in a single command? - azure

I have a set of values to return as output from my databricks notebook. Can anyone sugggest a way to do that in an efficient and easy way?

You can only export a string, but you can organize its content as you want. You could export a JSON as a string, containing your different values. The output is then parsed to.retrieve the different values.

You could run said notebook within your current notebook using: %run <path to your notebook>. This will, however, import all variables etc from that notebook.

Related

For each activity in azure synapse

A notebook needs to be run over a set of Array of arrays in for each activity . Is there a way to pass in that subarray as a parameter to the notebook that's inside for each activity.
The datatype for a parameter in notebook in synapse doesn't accept an array type
Currently, Notebook activity does not support passing arrays to Notebook.
So, pass it as String, then In Notebook convert it into array.
My Array parameter in pipeline:
Inside ForEach, pass it as String.
Output of Notebook in one iteration:
Use eval() to convert string in to list in Notebook.

Import a notebook into another book in Azure Synapse

I have one notebook1 in synapse ws with python code that contains some variable.
I have created another notebook2 and I'd like to use the variables from notebook1. I tried to import as a package, but it didn't work.
Has any one done that in the past.
You can get the variables from another Notebook by referencing using %run in present Notebook.
Example:
This is my Notebook2 in which I have defined two variables.
Then In Notebook1 reference the Notebook2 like below %run Notebook2.
When you execute it, you can access the variables from it.
Note: Make sure you publish the Calling Notebook(Notebook2) to access the recent changes in that Notebook.
and don't execute any code in %run cell otherwise it will error like below.

%run magic using get_ipython().run_line_magic() in Databricks

I am trying to import other modules inside an Azure Databricks notebook. For instance, I want to import the module called 'mynbk.py' that is at the same level as my current Databricks notebook called 'myfile'
To do so, inside 'myfile', in a cell, I use the magic command:
%run ./mynbk
And that works fine.
Now, I would like to achieve the same result, but with using get_ipython().run_line_magic()
I thought, this is what I needed to type:
get_ipython().run_line_magic('run', './mynbk')
Unfortunately, that does not work. The error I get is:
Exception: File `'./mynbk.py'` not found.
Any help is appreciated.
It won't work on Databricks because IPython commands doesn't know about Databricks-specific implementation, and IPython's %run is expecting the file to execute, but Databricks notebooks aren't files on the disk, but the data stored in the database, so %run from IPython can't find it, and you get error.

Getting rid of print "<IPython.core.display.Markdown object>" when using `display`

I'm trying to create nice slides using jupyter notebook and RISE. One of my objectives is to display a pandas-dataframe in a Markdown cell in order to have some styling flexibility.
I am using the following code to display my dataframe in a Markdown cell:
{{Markdown(display(df_x))}}
After running this line, I get the following result:
image of dataframe displayed
I would like to get rid of the text printed below my dataframe (<IPython.core.display.Markdown object>).
I still haven't found a way to achieve this. Could someone give me a hand?
This is the library I'm working with:
from IPython.display import display
Not familiar with Markdown class so not sure why you need that but this text printed in the output cell is coming from the fact that this Markdown class is returning and object and since you're not assigning it to any variable the default behavior for the notebook is to run something like str(your_object) which correctly returns <IPython.core.display.Markdown object>.
So the easiest workaround would be to just assign it to some variable like this:
dummy_var = Markdown(display(df_x))
# or better yet:
_ = Markdown(display(df_x))

Writing into a Jupyter Notebook from Python

Is it possible for a Python script to write into a iPython Notebook?
with open("my_notebook.ipynb", "w") as jup:
jup.write("print(\"Hello there!\")")
If there's some package for doing so, can I also control the way cells are split in the notebook?
I'm designing a software tool (that carries out some optimization) to prepare an iPython notebook that can be run on some server performing scientific computations.
I understand that a related solution is to output to a Python script and load it within a iPython Notebook using %load my_python_script.py. However, that involves a user to type stuff that I would ideally like to avoid.
Look at the nbformat repo on Github. The reference implementation is shown there.
From their docs
Jupyter (né IPython) notebook files are simple JSON documents, containing text, source code, rich media output, and metadata. Each segment of the document is stored in a cell.
It also sounds like you want to create the notebook programmatically, so you should use the NotebookNode object.
For the code, something like, should get you what you need. new_cell_code should be used if you have code cells versus just plain text cells. Text cells should use the existing markdown formatting.
import nbformat
notebook = nbformat.v4.new_notebook()
text = """Hello There """
notebook['cells'] = [nbformat.v4.new_markdown_cell(text)]
notebook= nbformat.v4.new_notebook()
nbformat.write(notebook,'filename.ipynb')

Resources