> For the complete documentation index, see [llms.txt](https://docs.bdb.ai/data-science-lab-4/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.bdb.ai/data-science-lab-4/project/tabs-for-a-data-science-lab-project/tabs-for-tensorflow-and-pytorch-environment/utility.md).

# Utility

This tab allows to create and list the python scripts (.py files) that can be imported to your notebook.

{% hint style="success" %}
*Check out this walk-through on how to import a Utility scrip file to the DS Lab Project.*
{% endhint %}

{% embed url="<https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBLGYLEkBUnc8nVEBAuEI%2Fuploads%2FK9y7gVipiqTCE0gRlKvn%2FImporting%20Utility%20V2.mp4?alt=media&token=e2bb6abe-8f19-41ed-b599-f09662defd1c>" %}
***Importing a Utility Script file***
{% endembed %}

## Importing a Utility Script <a href="#importing-a-utility-script" id="importing-a-utility-script"></a>

* Navigate to the ***Utility*** tab for a Project.
* Click the ***Add Script*** option.

<figure><img src="https://content.gitbook.com/content/1QeOywZjV1cHo55cMW8u/blobs/QeToZEAFQBS8XbvouBy4/image.png" alt=""><figcaption></figcaption></figure>

* The ***Add utility script*** page opens.
* Select the Import Utility option.
* Provide a **Name for the Utility script.**
* Provide ***description*** for the Utility script.
* Use the ***Choose File*** option to import a Utility file from the system.

<figure><img src="https://content.gitbook.com/content/1QeOywZjV1cHo55cMW8u/blobs/5TsUZ6bklS6ymP2bpGb1/image.png" alt=""><figcaption></figcaption></figure>

* After the file gets imported from the system, the file name appears next to the ***Choose File*** option.
* The ***Save*** option will be enabled, click the ***Save*** option.

<figure><img src="https://content.gitbook.com/content/1QeOywZjV1cHo55cMW8u/blobs/NCbEiYTHHMe6KwhZuG49/image.png" alt=""><figcaption></figcaption></figure>

* A notification message appears.
* The imported script gets added under the Utility tab.

<figure><img src="https://content.gitbook.com/content/1QeOywZjV1cHo55cMW8u/blobs/6VzorZGuhwqozb0rPPXH/image.png" alt=""><figcaption></figcaption></figure>

## Importing the Utility Script inside a Notebook <a href="#importing-the-utility-script-inside-a-notebook" id="importing-the-utility-script-inside-a-notebook"></a>

* Copy a utility script name using the Utility list.
* Navigate to the Notebook tab.

<figure><img src="https://content.gitbook.com/content/1QeOywZjV1cHo55cMW8u/blobs/WJjOUMyG6ia8r9XruAzD/image.png" alt=""><figcaption></figcaption></figure>

* Upload a Notebook or create a new Notebook and Navigate to the Notebook page.
* Add a ***Code*** ***cell***.
* Write code to import data with specific class.
* Provide the Utility script as the resource to get data.
* Specify the variables to get the class and get the data in the next code cell.
* Provide print function to see the data.
* Run all the cells.
* You can get the output below.​

<figure><img src="https://content.gitbook.com/content/1QeOywZjV1cHo55cMW8u/blobs/yZd5XmAki2Dl74mLMpNg/image.png" alt=""><figcaption></figcaption></figure>

## Updating the Utility Script

* Navigate to the ***Utility*** tab.
* Select an uploaded utility script from the list.
* Click the ***Edit*** icon.        &#x20;

<figure><img src="https://content.gitbook.com/content/1QeOywZjV1cHo55cMW8u/blobs/pMpLs3mpwNluWsd5Oppv/image.png" alt=""><figcaption></figcaption></figure>

* The ***Update Utility Script*** page opens displaying the utility script content.
* Click the ***Validate*** option.

<figure><img src="https://content.gitbook.com/content/1QeOywZjV1cHo55cMW8u/blobs/nJY9JoR1GwkFHWOtJGAz/image.png" alt=""><figcaption></figcaption></figure>

* The Logs will be displayed in the Logs space.
* Click the ***Update*** option.

<figure><img src="https://content.gitbook.com/content/1QeOywZjV1cHo55cMW8u/blobs/kuCq9RcAUC4Kpcl2j4a5/image.png" alt=""><figcaption></figcaption></figure>

* A success message appears.
* The selected Utility script gets updated.

<figure><img src="https://content.gitbook.com/content/1QeOywZjV1cHo55cMW8u/blobs/Kqdrt0gJQDEn4wUSht3I/image.png" alt=""><figcaption><p><em><strong>the Utility Script get updated.</strong></em></p></figcaption></figure>

{% hint style="info" %}
*<mark style="color:green;">Please Note:</mark>*&#x20;

* The name of the selected utility script cannot be changed by using the update option.
* *Refer the*[ ***Data Science Lab Quick Start Flow***](https://docs.bdb.ai/data-science-lab-4/data-science-lab-quick-start-flow) *page to get an overview of the **Data Science Lab** module in nutshell.*&#x20;
* *The user can also import a **Utility** file by using the **Utility** option provided inside a **Notebook**. Refer the* [***Utility Notebook Operation***](https://docs.bdb.ai/data-science-lab-4/project/tabs-for-a-data-science-lab-project/tabs-for-tensorflow-and-pytorch-environment/notebook/notebook-page/notebook-operations/utility-notebook-operation) *page to understand it in details.*
  {% endhint %}

## Sample Utility Script

Please use the below-given sample Utility script to explore and use the Utility option provided in the Data Science Lab.

```python
# importing employee.py as a module in DS Lab Notebook.

import employee
import time
import logging
import pandas as pd
from pymongo import MongoClient

def run_scripts(conn_str, database, collection):
    conn_str = conn_str
    database = database
    collection = collection
    client = MongoClient(conn_str)
    db = client[database]
    collection = db[collection]
    res = employee.emp_data()
    
    for i in res:
        salary = i.get('salary', 0)
        i['status'] = 'rich' if salary > 50000 else ('middle_class' if 25000 < salary < 50000 else 'poor')
        logging.info(i)
        print(i)
    collection.insert_many(res)
    print(f"Inserted {len(res)} rows into MongoDB")
    logging.info(f"Inserted {len(res)} rows into MongoDB")
    
#The variable 'res' in this script holds the results derived from the code written in the utility file.
```
