Using the Valohai APIs is rather straightforward, you’ll need to create an API token to authenticate your requests and then write your code to send & receive requests.
- Go to your profile setting and create an authentication token
- In our sample we’ll paste this directly to the file but you should consider saving this token in a configuration file or database for secure storage.
- Consider creating a custom virtual environment for Python before continuing.
python3 -m virtualenv .venv
source .venv/bin/activate
- You’ll need the ID of a single execution to fetch its metadata.
- You can query for it with Valohai APIs or by going to the browser and navigating inside an execution. You’ll see the ID in the url.
- Create a new folder on your computer and inside it create a new file
fetchMetadata.py
import requests
import json
import os
# Get the Base URL of your Valohai environment
VH_BASE_URL = os.getenv('VH_BASE_URL', 'https://app.valohai.com')
# Get the Valohai API token you created earlier.
# Here it has been stored in a local environment variable.
# Remember to follow your organization's security standards when handling the token.
VH_API_TOKEN = os.getenv('VH_API_TOKEN')
headers = {'Authorization': 'Token %s' % VH_API_TOKEN}
# Place your execution ID here
execution_id = '01847a7f-71c5-3f9c-5805-0d9247aab6fe'
# Send a request (with the authentication headers) to fetch all executions in a project
# You can get the project ID for example
resp = requests.get(f"{VH_BASE_URL}/api/v0/executions/{execution_id}/metadata/", headers=headers)
resp.raise_for_status()
# Save the response to a file
with open("execution-metadata.json", "w") as outfile:
json.dump(resp.json(), outfile)
- Save and run python fetchMetadata.py and you’ll see the metadata stream of a single execution.
Comments
0 comments
Please sign in to leave a comment.