Accessing a Utility File inside a Notebook

Steps to access a Utility script inside a Notebook.

Accessing Utility Files Inside a Notebook

The BDB Data Science Lab allows users to access and execute reusable utility scripts directly within notebook environments. These utility scripts typically contain helper functions—such as data generators, preprocessors, or reusable analysis logic—that can be imported and used across multiple projects.

There are two ways to access a utility file inside a Notebook:

  1. Using the Code

  2. Using the Copy Path Option

Accessing the Utility Script through Code

1

Navigate to the Utils Folder

  • Open the project workspace within the Data Science Lab.

  • Locate the Utils folder under the selected project.

  • Identify the required utility script

    • Example, dataframe_generator.py. This file likely defines a reusable function named generate_dataframe() that creates a sample DataFrame.

2

Open a Notebook

  • Navigate to the Repo folder under the same project.

  • Open an existing notebook or create a new one.

  • The notebook interface will appear on the right side of the page.

3

Insert a Code Cell

  • Inside the notebook, add a new code cell where you will write the import and execution statements.

4

Execute the Import and Function Call

  • Run the following code in the code cell:

from dataframe_generator import *
generate_dataframe()

Explanation:

  • The line from dataframe_generator import * imports all available functions from the dataframe_generator.py script. This makes the functions defined in that utility script directly accessible inside the notebook environment.

  • The generate_dataframe() function is then called.

    • This function creates and returns a sample pandas DataFrame.

5

Execution Details

Once the code cell is executed:

  • The notebook cell executes successfully in 0.08 seconds (indicated by the green status icon).

  • The kernel status shows as Running, confirming an active Python environment.

  • CPU and RAM usage are displayed at the top-right, providing resource utilization information.

6

Output Preview

  • After successful execution, a DataFrame Preview appears below the code cell, showing the sample data generated by the utility function.

Access a Utility Script through Copy Path

The Copy Path feature in the BDB Data Science Lab allows you to execute a utility script directly inside a Notebook by referencing its full system path. This is helpful when you want to run or reuse logic from a script without importing it as a module.

The steps below explain how to use this feature to run the dataframe_generator.py utility file.

1
  • Locate the Utils folder under the selected project.

  • Identify the required utility script—for example, dataframe_generator.py. This file likely defines a reusable function named generate_dataframe() that creates a sample DataFrame.

  • Click the Ellipsis menu (⋮) next to the utility script.

2

Copy the File Path

  • From the dropdown menu, click Copy Path.

    • This copies the full absolute path of the script to your clipboard.

    • Example copied path (for illustration):

    @SYS.DATASANDBOX_PATH + '22599305400/utility/dataframe_generator.py'
3

Paste the File Path into the Notebook

  • Open or switch to your Notebook in the Repo folder & paste the copied path into a variable, for example:

filepath = @SYS.DATASANDBOX_PATH + '22599305400/utility/dataframe_generator.py'
  • Run the Cell.

4

Load and Execute the Script

  • Use Python’s file handling and exec() to read and run the utility script:

with open(filepath, "r") as file:
    code = file.read()

# Execute the code from the file
exec(code)
  • This executes everything inside dataframe_generator.py, including functions such as generate_dataframe().

5

View the Output

  • When the script runs successfully, the output (in this example, a generated DataFrame) is displayed below the cell.

Last updated