This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

Python

Creating and invoking Python actions

The process of creating Python actions is similar to that of other actions. The following sections guide you through creating and invoking a single Python action, and demonstrate how to bundle multiple Python files and third party dependencies.

  1. Create a Python_app folder and then create a package directory.Now create a Python file with the following content inside our packages/python. For this example, the file name is hello.py.
#--web true
#--kind python:default

def main(args):
  name = args.get("name", "stranger")
  result = f"Hello {name}!"
  print(result)
  return {"greeting": result}

The Python file might contain additional functions. However, by convention, a function called main must exist to provide the entry point for the action.

Your directory structure should looks like this:

Python_app
└── packages
    └── Python
        └── hello.py
  1. Run the following command to deploy the action
ops ide deploy
  1. Ops will create the action automatically. For this example, the action is called python/hello.
/home/openserverless/.ops/tmp/deploy.pid
PID 278075
> Scan:
>> Action: packages/python/hello.py
> Deploying:
>> Package: python
$ $OPS package update python 
ok: updated package python
>>> Action: packages/python/hello.py
$ $OPS action update python/hello packages/python/hello.py --web true --kind python:default
ok: updated action python/hello
build process exited with code 0
UPLOAD ASSETS FROM web
==================| UPLOAD RESULTS |==================
| FILES      : 0
| COMPLETED  : 0
| ERRORS     : 0
| SKIPPED    : 0
| EXEC. TIME : 2.37 ms
======================================================
URL: http://opstutorial.localhost:80

Note: To use a specific version of Python runtime, change the kind property --kind python:3.11, or --kind python:3.12 in the hello.py file.

  1. To invoke the action run the following command:
ops action invoke python/hello --param name Marina --result

Packaging Python actions in zip files

Sometimes you action would be more complex and probably you prefer to create multiple file in order to organize better your code.

In this example we’re creating a more complex python action.

  1. Create a folder complex_action inside packages/python folder. Then create two python files

main.py

#--web true
#--kind python:default
import utils

def main(args):
  name = args.get("name", "stranger")
  result = utils.concat_string("Nice to meet you,",name)
  return {"greeting": result}

utils.py

def concat_string(first_string: str, second_string: str):
  return f"{first_string} {second_string}"

Note: The filename of the source file containing the entry point (e.g., main) must be __main__.py.

  1. Deploy the action using ide deploy
ops ide deploy
  1. Now you can invoke your action. In this case the action name is python/complex_action
ops action invoke python/complex_action --param name Marina --result

1 - python:v3.10

python:v3.10 (default: false)

LibraryVersionLink
beautifulsoup44.10.0PyPI
httplib20.19.1PyPI
kafka_python2.0.2PyPI
python-dateutil2.8.2PyPI
requests2.31.0PyPI
scrapy2.5.0PyPI
simplejson3.17.5PyPI
twisted21.7.0PyPI
netifaces0.11.0PyPI
pyyaml6.0PyPI
redis4.4.2PyPI
boto31.28.25PyPI
psycopg3.1.10PyPI
pymongo4.4.1PyPI
minio7.1.16PyPI
openai1.7.1PyPI
auth0-python4.6.0PyPI
langchain0.1.0PyPI
farm-haystack1.23.0PyPI
nltk3.8.1PyPI
langdetect1.0.9PyPI
ollama0.1.9PyPI
joblib1.4.2PyPI
lightgbm4.5.0PyPI
feedparser6.0.11PyPI
numpy1.26.4PyPI
scikit-learn1.5.2PyPI
bcrypt4.2.1PyPI

2 - python:v3.11

python:v3.11 (default: false)

LibraryVersionLink
beautifulsoup44.10.0PyPI
httplib20.19.1PyPI
kafka_python2.0.2PyPI
python-dateutil2.8.2PyPI
requests2.31.0PyPI
scrapy2.5.0PyPI
simplejson3.17.5PyPI
twisted21.7.0PyPI
netifaces0.11.0PyPI
pyyaml6.0PyPI
redis4.4.2PyPI
boto31.28.25PyPI
psycopg3.1.10PyPI
pymongo4.4.1PyPI
minio7.1.16PyPI
openai1.7.1PyPI
auth0-python4.6.0PyPI
langchain0.1.0PyPI
farm-haystack1.23.0PyPI
nltk3.8.1PyPI
langdetect1.0.9PyPI
plotly5.19.0PyPI
ollama0.1.9PyPI
joblib1.4.2PyPI
lightgbm4.5.0PyPI
feedparser6.0.11PyPI
numpy1.26.4PyPI
scikit-learn1.5.2PyPI
bcrypt4.2.1PyPI

3 - python:v3.12

python:v3.12 (default: false)

LibraryVersionLink
beautifulsoup44.10.0PyPI
ollama0.4.5PyPI
openai1.59.3PyPI
pymilvus2.5.3PyPI
redis5.2.1PyPI
pillow11.1.0PyPI
nltk3.8.1PyPI
httplib20.19.1PyPI
kafka_python2.0.2PyPI
python-dateutil2.8.2PyPI
requests2.32.2PyPI
scrapy2.5.0PyPI
simplejson3.17.5PyPI
twisted21.7.0PyPI
netifaces0.11.0PyPI
pyyaml6.0.2PyPI
boto31.35.98PyPI
psycopg3.1.10PyPI
pymongo4.4.1PyPI
minio7.1.16PyPI
auth0-python4.6.0PyPI
langdetect1.0.9PyPI
plotly5.19.0PyPI
joblib1.4.2PyPI
lightgbm4.5.0PyPI
feedparser6.0.11PyPI
numpy1.26.4PyPI
scikit-learn1.5.2PyPI
langchain0.3.14PyPI
langchain-ollama0.2.2PyPI
langchain-openai0.2.14PyPI
langchain-anthropic0.3.1PyPI
langchain-together0.2.0PyPI
langchain-postgres0.0.12PyPI
langchain-milvus0.1.7PyPI
bcrypt4.2.1PyPI
chevron0.14.0PyPI
chess1.11.1PyPI