Python SDK Setup

Prerequisites

Before you begin developing your application logic with Catalyst Python SDK in your local environment, please ensure you have the following package manager and programming environment installed on your local machine:

You can install Python from their official website and the pip package manager will be auto-installed in your local system. Please make sure you install the pip package manually if you install Python from other sources. You can refer to this doc for installing pip.

Note: If you have other versions of Python installed in your local machine (any version other than Python 3.9), then the execution of the Python functions in your directory will be skipped when the application is being served or deployed. These Python functions are also excluded when you pull the functions from the console to the CLI.

If you are creating Python functions in an existing Catalyst project in your local directory, you can install the prerequisites mentioned above, and proceed to set up the function. The steps to set up a Python function in an existing project directory are given in this help page. To learn about initalizing a Python function during Catalyst project initialization, refer this help page.

Installing the SDK

When you initialize a Catalyst project in the CLI and create or set up a Python function in an existing project directory in your local environment, the Python SDK package (zcatalyst-sdk) will automatically be installed inside the functions directory of your current project.

A main function file and a configuration file will be auto-generated with the boilerplate code in your function’s directory by default when you create a Catalyst Serverless function of any programming stack. For Python functions, an additional file named requirements.txt will also be created. This file contains the list of installed dependencies that are needed to implement the Python function. By default, it contains the entry for Catalyst’s Python SDK package (zcatalyst-sdk), when you create the Python function from the CLI. When you need to install external dependencies, you will need to add the name of the dependency manually in the requirements.txt file.

Note: If it is your first time initializing a Python function, you will need to additionally set the path information of Python installed in your system. You can set this information in a specific configuration file that is present in your local system as a hidden file. The path will need to be set using the config:set <key=value> CLI command. You can find out more about this command from this help document.

You can use the following command to install the Catalyst Python SDK globally in your system:

copy
$
pip install zcatalyst-sdk

Initializing the SDK

After Python SDK is installed in your function’s directory, you can begin coding the Python function. You must first initialize the SDK within the function’s code, using the initialize() method to access the Catalyst components of the current project. The initialization methods for the Catalyst function types are given below:

Basic I/O Functions

    
copy
import zcatalyst_sdk def handler(context, basicio): app = zcatalyst_sdk.initialize() #This app variable is used to access the catalyst components. #You can refer the SDK docs for code samples. #Your business logic comes here

Advanced I/O Functions

    
copy
import zcatalyst_sdk def handler(request: Request): app = zcatalyst_sdk.initialize() #This app variable is used to access the catalyst components. #You can refer the SDK docs for code samples. #Your business logic comes her

Event Functions

    
copy
import zcatalyst_sdk def handler(event, context): app = zcatalyst_sdk.initialize() #This app variable is used to access the catalyst components. #You can refer the SDK docs for code samples. #Your business logic comes here

Cron Functions

    
copy
import zcatalyst_sdk def handler(cron_details, context): app = zcatalyst_sdk.initialize() #This app variable is used to access the catalyst components. #You can refer the SDK docs for code samples. #Your business logic comes here

When you initialize the SDK package inside the function, it returns a Python object as the response. This object can be used to call the component-specific methods defined in the Python classes and access the required Catalyst components.

Note : You can create Python functions both from the web console or the CLI, based on your preference. However, you can only upload the function bundle from the local and can't code it in the console directly for now. We will be providing support for online editors in the future.

Initializing with Scopes

Catalyst allows you to initialize the SDK in a project using the following scopes:

  • Admin: You have unrestricted access to all the components and their respective functionalities. For example, you have complete access to the Data Store to perform all operations like Read, Write, Delete, etc.

  • User: You can restrict access to components, and specific functionalities. For example, you can provide Read access alone to Data Store.

Note:
  • It is not mandatory for you to initialize the projects with scopes. By default, a project that is initialized will have Admin privileges.

  • Ensure you have initialized the Catalyst SDK with the appropriate scope while you engineer your business logic. The permissions you define for your scope control your end-user’s actions.

  • Scopes only apply to operations related Data Store, File Store, and ZCQL.

  • Depending on how you engineer your business logic, you can decide if your end-users can perform Admin or User actions. This is decided based on the role assigned to your end-user when they sign up to your application in Catalyst Authentication. The permissions for the roles can be configured in the Scopes & Permissions section of the Data Store and File store.

The SDK snippets below will allow you to initialize the SDK using either Admin or User scope, and perform a SELECT query in the Data Store:

Admin Scope

    
copy
import zcatalyst_sdk def handler(request: Request): app = zcatalyst_sdk.initialize(scope='admin') #This app variable is used to access the catalyst components. #You can refer the SDK docs for code samples. #Your business logic comes her

User Scope

    
copy
import zcatalyst_sdk def handler(request: Request): app = zcatalyst_sdk.initialize(scope='user') #This app variable is used to access the catalyst components. #You can refer the SDK docs for code samples. #Your business logic comes here

Updating the SDK

As discussed previously, the requirements.txt file will contain the list of installed Python dependencies for your Python function. This includes the Catalyst Python SDK and the other user installed dependencies that are needed to implement certain Python classes.

You can update the version of the SDK by simply replacing it with the newer version in the requirements.txt file and saving it. Once you update the version of the SDK, you need to serve or deploy your application locally. This helps to enable the updated changes in the newer version of the SDK in your Python function.

Last Updated 2024-11-21 17:58:19 +0530 +0530