Forum Discussion

VickyDev18's avatar
VickyDev18
Advocate II
2 years ago
Solved

Call one notebook from another with parameters

I have this simple notebook to load data from an xlsx file in a lakehouse to a delta table in the Lakehouse. 

 

How can I make this reusable/parameterized so I call it from another notebook and pass parameters and how exactly would I call it from another notebook?

 

 

import pandas as pd

source_path = "/lakehouse/default/"
source_file_name = "Files/Test.xlsx"
sheet_name = "Test"
destination_table = "test"

# Read Excel data into a Pandas DataFrame
df = pd.read_excel(source_path + source_file_name, sheet_name=sheet_name)

# Convert Pandas DataFrame to Spark DataFrame
df_spark = spark.createDataFrame(df)

# Save Spark DataFrame as a Delta table
df_spark.write.mode("overwrite").format("delta").saveAsTable(destination_table)

 

 

I am aware of how to call a parameterized notebook from a pipeline How to Pass Parameters from Pipelines to Notebooks in Microsoft Fabric! - YouTube

 

However, I want to know if we can call the notebook from another notebook. 

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi VickyDev18 
    Thanks for using Fabric Community.
    You can call one notebook from another notebook using %run command. I have created a repro of the same but instead I used a CSV file. I have attached the screenshots of the code.

    Functions is my child notebook where I am defining a function to create a table inside lakehouse using the data inside a CSV file.

    I am calling this notebook from another notebook:

    The table got created in the lakehouse.


    Hope this helps. Please let me know if you have any further questions.

5 Replies

  • HimanshuS-msft's avatar
    HimanshuS-msft
    Microsoft Employee

    Hello VickyDev18 , 

    Its me again. Have you tried using this 

     

    from notebookutils import mssparkutils
    mssparkutils.notebook.runMultiple(["aaa"])
    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi VickyDev18 
      Thanks for using Fabric Community.
      You can call one notebook from another notebook using %run command. I have created a repro of the same but instead I used a CSV file. I have attached the screenshots of the code.

      Functions is my child notebook where I am defining a function to create a table inside lakehouse using the data inside a CSV file.

      I am calling this notebook from another notebook:

      The table got created in the lakehouse.


      Hope this helps. Please let me know if you have any further questions.

      • VickyDev18's avatar
        VickyDev18
        Advocate II

        I had tried this method but for some reason it wasn't working. However, now that I tried it again, it worked! 

         

        Only simplified approach I tried was to just call the function directly without the 2nd function. 

         

        So overall the approach was:-

        1. Create a Notebook called Functions with code below. 

         

        import pandas as pd
        
        def load_excel(source_path, source_file_name, sheet_name, destination_table):
        
            # Read Excel data into a Pandas DataFrame
            df = pd.read_excel(source_path + source_file_name, sheet_name=sheet_name)
        
            # Convert Pandas DataFrame to Spark DataFrame
            df_spark = spark.createDataFrame(df)
        
            # Save Spark DataFrame as a Delta table
            df_spark.write.mode("overwrite").format("delta").saveAsTable(destination_table)

         

         

        2. Create a new Notebook and call the function above using the code below

         

        %run Functions
        
        #load_excel(source_path, source_file_name, sheet_name, destination_table)
        load_excel("/lakehouse/default/Files/", "Test.xlsx", "Test", "test")