# Utility

This feature enable users to upload their files (_**in .py format**_) to the _**Utility**_ section of a DS Lab project using this feature. Subsequently, it will allow users to import these files as modules in a DsLab notebook, enabling the use of the uploaded utility functions in scripts.



{% hint style="warning" %}
_<mark style="color:orange;">**Prerequisite:**</mark> To use this feature, the user needs to create a project under Ds Lab module._
{% endhint %}

{% hint style="success" %}
_Check out the below-given video on how to use utility scripts in the DS Lab module._
{% endhint %}

{% embed url="https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvmyGSlDvDj86v7eh3RHM%2Fuploads%2F6xmJIh7aI4D0tKvwhtjc%2F2024-02-28-15-36-16%20(online-video-cutter.com).mp4?alt=media&token=80153075-cc2b-4c45-95a4-39c18d331206" %}
_**Adding an Utility script to a DS Lab Project**_
{% endembed %}

* Activate and open the project under DS Lab module.
* Navigate to the _**Utility**_ tab in the project.
* Click on _**Add Scripts**_ options.
* After that, the user will find two options:
  * **Import Utility:** It enables users to upload their own file _**(.py format)**_ where the script has been written.
  * **Pull from Git:** It enables users to pull their scripts directly from their Git repository. In order to use this feature, the user needs to configure their Git branch while creating the project in DS Lab. Detailed information on configuring Git in a DS Lab project can be found here: [_**Git Sync**_](git-sync)

### Import Utility

* **Utility Name:** Enter the utility name.
* **Utility Description:** Enter the description for the utility.
* **Utility Script:** The users can upload their files in the _**.py**_ format.
* Click the _**Save**_ option after entering all the required information on the _**Add utility**_ _**script**_ page, and the uploaded file will list under the _**Utility**_ tab of the selected project.

<figure><img src="https://content.gitbook.com/content/wT6nkX9iZNRiZKBnpFmn/blobs/6E1e1ovdUUX18xf5Nh7T/image.png" alt=""><figcaption><p><em><strong>Utility Tab displaying the Utility scripts list</strong></em></p></figcaption></figure>

* Once the file is uploaded to the Utility tab, the user can edit the contents of the file by clicking on the _**Edit**_ icon corresponding to the file name.

<figure><img src="https://content.gitbook.com/content/wT6nkX9iZNRiZKBnpFmn/blobs/hmSNOJgMlyTPQ4aHD7gR/image.png" alt=""><figcaption><p><em>Editing the utility file</em></p></figcaption></figure>

* After making changes, the users can validate their script by clicking on the _**Validate**_ icon on the top right.
* Click the _**Update**_ option to save the latest changes to the utility file.

<figure><img src="https://content.gitbook.com/content/wT6nkX9iZNRiZKBnpFmn/blobs/ODfe8u4u4T48qkAHmxtz/image.png" alt=""><figcaption><p><em>Editing and validating an uploaded utility file</em></p></figcaption></figure>

* If the user wants to delete a particular Utility, they can do so by clicking on the _**Delete**_ icon.

<figure><img src="https://content.gitbook.com/content/wT6nkX9iZNRiZKBnpFmn/blobs/YQ6Gv8U8pABJnf4qHfOy/image.png" alt=""><figcaption><p><em><strong>Deleting a utility file</strong></em></p></figcaption></figure>

### Importing the Utility File to a DS Lab Notebook

The user can import the uploaded utility file as a module in the DS Lab notebook and use it accordingly.&#x20;

<figure><img src="https://content.gitbook.com/content/wT6nkX9iZNRiZKBnpFmn/blobs/le0B5gkwvVYg6N1hzDLj/image.png" alt=""><figcaption><p><em><strong>Utility file</strong></em></p></figcaption></figure>

* In the above given image, it can be seen that "employee.py" file is uploaded in the utility. Now, this file is going to be imported in the DS Lab notebook and will further used in the script.

### Sample Utility Script&#x20;

Use the below-given sample code for the reference and explore the Utility file related features yourself inside the DS Lab notebook.

```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.
```

* In the above written sample script, the utility file(_**employee.py**_) has been imported (_**import employee**_) and it has been used in the script for further processing.
* After completing this process, the user can export this script to the Data Pipeline module and register it as a job according to their specific requirements.

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

* _Refer to the below-given links to get instructions on exporting to the pipeline and registering it as a job:_
  * [_**Exporting a Script from DS Lab**_](exporting-a-script-from-data-science-lab)
  * [_**Register as Job**_](register-as-job)
* To apply the changes made in the utility file and get the latest results, the user must restart the notebook's kernel after any modifications to the utility file.
{% endhint %}
