All Products
Search
Document Center

Dataphin:Python computation task development case using third-party libraries

Last Updated:Jan 21, 2025

Dataphin offers tenant-level management of third-party libraries. To utilize Python third-party libraries in Python computation tasks within Dataphin, you must first install the required Python modules via the third-party library feature. This topic guides you through the process of managing third-party libraries for Python computation task development in Dataphin.

Case description

This example demonstrates the use of the xlrd third-party library.

Procedure

Step 1:Install the Python module.

  1. Navigate to the top menu bar on the Dataphin home page and single click Management Center > System Settings.

  2. Access the Install Python Module dialog box by following these steps:

    Single click Python Third-Party Package > Python Module > Install Python Module.

    image

  3. In the Install Python Module dialog box, set the following parameters:

    Parameter

    Description

    Module Name

    Type in xlrd as the module name.

    Python Version

    Choose the Python 3.7 version.

    Installation Method

    Opt for the Online Installation method.

  4. Click OK and wait for the xlrd module to finish installing.

Step 2:Create a Python computation task and introduce the third-party library

  1. On the Dataphin home page, navigate to the top menu bar and single click Development > Data Development.

  2. To open the New PYTHON Task dialog box, follow these steps:

    Select the project (Dev-Prod mode requires selecting the environment) > Single click Script Task > Click the New Icon image > Choose PYTHON.

    image

  3. In the New PYTHON Task dialog box, configure the task parameters as follows:

    Parameter

    Description

    Task Name

    Specify the code task's name, such as xlrd package test.

    Schedule Type

    Choose One-Time Task.

    Select Directory

    Select the directory to store the task.

    Use Template

    By default, this is set to Off.

    Python Third-Party Package

    Select the xlrd package successfully installed in Step 1.

    Description

    Provide a brief description of the task, for instance, xlrd package test.

  4. Click OK.

  5. On the Python task code editor, select Python 3.7 and begin coding. The sample code for this case is as follows:

    Note

    dataphin.xls resources can reference any .xls file uploaded to Dataphin, by replacing the resource name with the one provided during the upload. For more information, see the referenced document and .

    @resource_reference{"dataphin.xls"} # Reference dataphin.xls resource
    
    # Import xlrd module.
    import xlrd
    wb = xlrd.open_workbook('dataphince.xls') # Open excel
    sh = wb.sheet_by_name('Sheet1') # Locate worksheet by workbook
    
    # Traverse excel, print all data
    for i in range(sh.nrows):
        print(sh.row_values(i))
  6. Save and submit the Python task on the code editor page.

    1. Click the run code icon image.

    2. Click the submit code icon image in the upper right corner of the page.

  7. Enter remarks on the Submit Remarks page.

  8. Click OK And Submit.