cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Import notebook content into a python file

thibault
Contributor II

Hi, I have a workflow based on python scripts. How can I import the content of a notebook where a class and functions are defined?

I know how to import python files into notebooks, but the other way around doesn't seem as straight forward.

1 ACCEPTED SOLUTION

Accepted Solutions

thibault
Contributor II

Found a solution executing a notebook, using the databricks api to download the notebook content as bytes :

1. set environment variables DATABRICKS_HOST and DATABRICKS_TOKEN

2. 

w = WorkspaceClient()
with w.workspace.download(notebook_path) as n:
notebook_content = n.read()
exec(notebook_content, globals())

View solution in original post

1 REPLY 1

thibault
Contributor II

Found a solution executing a notebook, using the databricks api to download the notebook content as bytes :

1. set environment variables DATABRICKS_HOST and DATABRICKS_TOKEN

2. 

w = WorkspaceClient()
with w.workspace.download(notebook_path) as n:
notebook_content = n.read()
exec(notebook_content, globals())

Connect with Databricks Users in Your Area

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