cancel
Showing results for 
Search instead for 
Did you mean: 
Get Started Discussions
Start your journey with Databricks by joining discussions on getting started guides, tutorials, and introductory topics. Connect with beginners and experts alike to kickstart your Databricks experience.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to create classes that can be instantiated from other notebooks?

Alex79
New Contributor II

Hi,

I am familiar with object oriented programming and cannot really get my head around the philosophy of coding in Databricks. My approach that naturally consists in creating classes and instantiating objects does not seem to be the right one.

Can someone explain to me how Databricks is expected to be used, and if this is possible to do so, then how to create a simple 'foo' class that I can instantiate outside of its notebook definition?

 

Thanks -

1 ACCEPTED SOLUTION

Accepted Solutions

szymon_dybczak
Esteemed Contributor III

Hi @Alex79 ,

Typically you define all the logic in plain old python modules, where you can have classes and functions. So for example, you can define following python module (in this case I'm defining simple function, but it could be class - it doesn't matter for the sake of example).

szymon_dybczak_0-1754904584166.png

 

And now my notebook could be the client of that module and import it.

szymon_dybczak_1-1754904714304.png

 

I would say that is the most common approach. So you have all the logic defined in python module that you can reuse and then those libraries are imported in notebooks (so notebooks act like orchestrator of the code).
But you can also use python wheel files if you want or stick only to py files and orchestrate those. You don't have use notebooks at all.

View solution in original post

7 REPLIES 7

szymon_dybczak
Esteemed Contributor III

Hi @Alex79 ,

Typically you define all the logic in plain old python modules, where you can have classes and functions. So for example, you can define following python module (in this case I'm defining simple function, but it could be class - it doesn't matter for the sake of example).

szymon_dybczak_0-1754904584166.png

 

And now my notebook could be the client of that module and import it.

szymon_dybczak_1-1754904714304.png

 

I would say that is the most common approach. So you have all the logic defined in python module that you can reuse and then those libraries are imported in notebooks (so notebooks act like orchestrator of the code).
But you can also use python wheel files if you want or stick only to py files and orchestrate those. You don't have use notebooks at all.

OK, makes sense to me, thanks!
Perfect!

szymon_dybczak
Esteemed Contributor III

No problem @Alex79 . If the answer was helpful to you, please consider marking it as accepted soution.

BS_THE_ANALYST
Honored Contributor III

Cool @szymon_dybczak

Thanks for this. Will we have to define absolute/relative parts in the "from __ import ___" statement if the module doesn't live in the same directory as the notebook? What's the normal practice for this in production? For instance, if you want to share this module (which will likely be updated) with your colleagues, I need to look into what Python Wheel Files offer. 

All the best,
BS


Hi @BS_THE_ANALYST ,

If modules don't live in the same directory you need to add directory that contains your module to sys.path.  You can specify directories using a relative path, as in the following example:

import sys
import os
sys.path.append(os.path.abspath('..'))

Here's a documentation entry where all of this is well described:

Work with Python and R modules | Databricks Documentation

If there is a need to share module with colleagues than I guess wheel file is a perfect fit for that purpose 🙂

BS_THE_ANALYST
Honored Contributor III

Legendary, @szymon_dybczak  👍

CatGIF.gif

All the best,
BS

No problem, happy that I could clarify some things for you 🙂

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local community—sign up today to get started!

Sign Up Now