Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us at FabCon Vienna from September 15-18, 2025, for the ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM. Get registered

Reply
CrhIT
Frequent Visitor

How to get a variable from one Notebook into another

I'm running a notebook, which output needs to be stored and used in a new notebook. How can this be done?

 

Let's say in Notebook_1 the code is

import pandas as pd

# Create an example pandas DataFrame
data = {
    "ID": [1, 2, 3, 4, 5],
    "Name": ["Alice", "Bob", "Charlie", "David", "Eve"],
    "Age": [25, 30, 35, 40, 45],
    "Salary": [50000, 60000, 70000, 80000, 90000],
    "Department": ["HR", "IT", "Finance", "Marketing", "Sales"]
}

df = pd.DataFrame(data)

 

I want to reference df in a new notebook so I have no need to rewrite the whole code in the new notebook.

1 ACCEPTED SOLUTION
nilendraFabric
Community Champion
Community Champion

Hello @CrhIT 

 

The `%run` magic command allows you to execute another notebook within the same workspace and access its variables directly.

 

Notebook1

 

import pandas as pd

data = {
"ID": [1, 2, 3, 4, 5],
"Name": ["Alice", "Bob", "Charlie", "David", "Eve"],
"Age": [25, 30, 35, 40, 45],
"Salary": [50000, 60000, 70000, 80000, 90000],
"Department": ["HR", "IT", "Finance", "Marketing", "Sales"]
}
df = pd.DataFrame(data)

 

In Notebook_2, use `%run` to call Notebook_1:
%run Notebook_1
print(df.head())


This method works only if both notebooks are in the same workspace.
• Variables defined in Notebook_1 will be available in Notebook_2

You can use `mssparkutils.notebook.run()`


Hope this is helpful. 

please accept the answer and give kudos if this helps 

 

View solution in original post

2 REPLIES 2
nilendraFabric
Community Champion
Community Champion

@CrhIT if this is helpful , please accept the answer as it will help community to find the right information quickly.

nilendraFabric
Community Champion
Community Champion

Hello @CrhIT 

 

The `%run` magic command allows you to execute another notebook within the same workspace and access its variables directly.

 

Notebook1

 

import pandas as pd

data = {
"ID": [1, 2, 3, 4, 5],
"Name": ["Alice", "Bob", "Charlie", "David", "Eve"],
"Age": [25, 30, 35, 40, 45],
"Salary": [50000, 60000, 70000, 80000, 90000],
"Department": ["HR", "IT", "Finance", "Marketing", "Sales"]
}
df = pd.DataFrame(data)

 

In Notebook_2, use `%run` to call Notebook_1:
%run Notebook_1
print(df.head())


This method works only if both notebooks are in the same workspace.
• Variables defined in Notebook_1 will be available in Notebook_2

You can use `mssparkutils.notebook.run()`


Hope this is helpful. 

please accept the answer and give kudos if this helps 

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June FBC25 Carousel

Fabric Monthly Update - June 2025

Check out the June 2025 Fabric update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.