Forum Discussion
Call one notebook from another with parameters
- Anonymous2 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.
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")
Hi VickyDev18
Thanks for sharing your approach here. Glad that your query got resolved. Please continue using Fabric Community for any help regarding your queries.