Linking Custom Python Code

Overview

When creating Operations or Custom Instruments, it may be desirable for your Operations to reference custom Python code without publishing that code as part of the Operation. For example, you may have an extensive Python codebase that follows your internal software development process with your own source control system and versioning strategy. You’d like to leverage your codebase without having to continuously publish new versions of Operations in LabOS.

This document describes a way to reference custom Python code from LabOS Operations. Note that a Premium account is required to take advantage of this functionality.

Custom Python Code

While the custom Python code can be of arbitrary size and complexity, the goal of this document is to illustrate the mechanics of referencing custom code, so it uses a simple function that writes passed in data to a text file.

Create a my_custom_code.py file with the following content:

				
					from pathlib import Path

def custom_function(data: str) -> None:
    tmp_dir = Path('C:\Temp')
    if not tmp_dir.exists():
        tmp_dir.mkdir()
    with open(tmp_dir / 'test.txt', mode='w') as test_file:
        test_file.write(data)
        
				
			

Custom Package

  1. In the C:\Program Files\Instrument Connector\packages directory, create a custom folder (the actual name of the folder is inconsequential) and copy my_custom_code.py into the folder.
  2. Create a genie-manifest.json file with the content below and restart the Instrument Connector.
				
					{
  "name": "custom",
  "type": "python-package",
  "description": "my custom Python code",
  "version": "1.0.0"
}
				
			
The "type": "python-package" entry in the genie-manifest.json file instructs the Instrument Connector that this folder contains Python code which needs to be added to the PYTHONPATH. If more than one Python package has the same name (e.g., multiple packages called custom), the Instrument Connector will reference the package with the latest version (e.g., 1.0.1 over 1.0.0).

Custom Operation

  1. Create a new Operation with a parameter of type String. Use the Insert Snippet button to generate Python code that reference the parameter value. The resulting Operation may look as follows:
  1. Modify the generated code to pass the parameter value to your custom Python code
				
					""" New Operation """
from my_custom_code import custom_function
from labsdk.state.parameters import load_parameters_env

def main() -> None:
    params = load_parameters_env()
    file_content = params['file content'].data
    custom_function(file_content)


if __name__ == "__main__":
    main()
    
				
			
  1. Publish the Operation and associate it with an instrument. See the Custom Instruments document for more information on how to publish and use custom Operations.
  2. Run the newly published Operation, as shown below
  1. On the Windows PC, verify that a C:\Temp\test.txt file exists and its content is the passed in string value (e.g., This string will be written to the file)
  2. Modify the custom code to, for example, write the passed in string to a different file and then re-run the Operation. Notice that the new code is used and no additional modification needs to be made in LabOS.

Book a Demo

Genie is now in limited Beta. Complete the form below and one of our sales representitives will reach out to you within 24 hours to setup an in-person demo of our platform.