Python

Install

First things first, let’s install the Python library!

(If you already installed the VSCode extension, the extension setup may have already installed the library.)

Run this command to ensure the python library is installed:

$ pip install polyapi-python

Caution

Please be sure to install the correct polyapi pypi package polyapi-python, as there are other registered packages with similar names.

Setup and Generate

Next, we need to make sure the PolyAPI Python client is properly configured:

$ python -m polyapi setup

Enter your server and API Key when prompted.

Next, let’s go ahead and retrieve all the trained Poly functions and generate the Python SDK for them:

$ python -m polyapi generate

Warning

Your api key is stored inside your PolyAPI library wherever it is installed (usually site_packages) in a file name .config.env

Do not commit this file to your git repo.

Develop First Function

Next, let’s develop our first custom function!

Open a new file called hello.py and add the following code:

def hello():
   return "Hello Poly World!"

Next use the PolyAPI Python SDK to deploy this function:

$ python -m polyapi --context mycontext --server function add hello hello.py

This will deploy a new serverless function to Poly. You can also instead do –client to create functions which are packaged into the generated SDK.

Run First Function

Finally, let’s open a new file called “run.py” and add the following code:

from polyapi import poly
print(poly.mycontext.hello())

Now run the python file:

$ python run.py

You should see “Hello Poly World!” printed in the console!

Onward

That’s it! You have now:

  • Setup your Python SDK

  • Trained your first server function

  • Ran your first server function

This is the last step on the guided tour of Poly.

To further explore aspects of Poly and what it can do, head over to Next Steps.