Python functions are currently in a beta state and may not be available on all enrollments.
The following documentation will guide you through the initial steps to prepare Python functions for use in the Palantir platform. You will learn how to create a Python functions repository, commit and publish a function, test in live previews, and more.
Navigate to the Project of your choice and create a new code repository by selecting + New > Code repository. Select the Pythons Functions template to initialize your repository. We recommend grouping all functions for use in Workshop or ontology based applications in a single repository to minimize costs.
Once the repository is created, navigate to the python-functions/python/python-functions/my_function.py
file.
Your repository will have been initialized with a my_function.py
file containing some example functions, including the following:
Copied!1 2 3 4 5
from functions.api import function, String @function def my_function() -> String: return "Hello World!"
Notice that the function adheres to the following constraints:
@function
from the functions.api
package to be recognized as a Python function. You may have multiple Python files with multiple functions in each file, but only the functions with this decorator will be registered as Python functions.String
from the functions API, but it may also be declared as the corresponding Python type str
.Even if you declare the type of an argument with the API type (for example, String
), your function will be passed the corresponding Python type at runtime (in this example, str
).
For a full overview of types in Python functions see our type reference documentation.
After you add the new function, you can run it immediately in the Functions helper. Open the Functions helper from the bottom left of the screen and select Live Preview. Choose the add_seconds_to_datetime
function, enter input values, and select Run to run the code.
Select Commit in the upper right to commit your changes onto the master
branch of your repository.
Once you write a function (or uncomment one of the example functions provided), follow the steps below to commit and publish it.
Select Tag and release and wait for the release step to complete.
Once the check is successful, select the Code tab, then open the Functions tab on the bottom of the page. You will see my_function
in the results.
Now, we will add a more complex function to this repository to test and publish. Copy and paste the code below to the bottom of the my_function
file.
Copied!1 2 3 4 5 6 7
from functions.api import function, String from datetime import datetime, timedelta @function def add_seconds_to_datetime(start_time: datetime, elapsed_millis: int) -> str: dt = start_time + timedelta(milliseconds=elapsed_millis) return dt.isoformat()
For more examples of how to use your Python functions in the platform, review our documentation on using Python functions in Pipeline Builder and on using Python functions in Workshop.