---
layout: default
navsection: sdk
navmenu: Python
title: Code cookbook
...
{% comment %}
Copyright (C) The Arvados Authors. All rights reserved.

SPDX-License-Identifier: CC-BY-SA-3.0
{% endcomment %}

h2. Cancel a container request

{% codeblock as python %}
import arvados
arvados.api().container_requests().update(uuid=container_request_uuid, body={"priority": 0}).execute()
{% endcodeblock %}

h2. Cancel all container requests

{% codeblock as python %}
import arvados
api = arvados.api()
result = api.container_requests().list(filters=[["state", "=", "Committed"], ["priority", ">", "0"]]).execute()["items"]
for container_request in result:
    api.container_requests().update(uuid=container_request["uuid"], body={"priority": 0}).execute()
{% endcodeblock %}

h2. List completed container requests

{% codeblock as python %}
import arvados
api = arvados.api()
result = api.container_requests().list(filters=[["name", "like", name], ["state", "=", "Final"]]).execute()["items"]
container_uuids = [cr["container_uuid"] for cr in result]
containers = api.containers().list(filters=[["uuid", "in", container_uuids]]).execute()["items"]
container_dict = {c["uuid"]: c for c in containers}

for container_request in result:
    container = container_dict[container_request["container_uuid"]]
    print("%s, %s, %s" % (container_request["uuid"], container_request["name"], "Success" if container["exit_code"] == 0 else "Failed"))
{% endcodeblock %}

h2. Get input of a CWL workflow

{% codeblock as python %}
import arvados
api = arvados.api()
container_request_uuid="qr1hi-xvhdp-zzzzzzzzzzzzzzz"
container_request = arvados.api().container_requests().get(uuid=container_request_uuid).execute()
print(container_request["mounts"]["/var/lib/cwl/cwl.input.json"])
{% endcodeblock %}

h2. Get output of a CWL workflow

{% codeblock as python %}
import arvados
import arvados.collection
api = arvados.api()
container_request_uuid="qr1hi-xvhdp-zzzzzzzzzzzzzzz"
container_request = arvados.api().container_requests().get(uuid=container_request_uuid).execute()
collection = arvados.collection.CollectionReader(container_request["output_uuid"])
print(collection.open("cwl.output.json").read())
{% endcodeblock %}

h2. List input of child requests

{% codeblock as python %}
import arvados
api = arvados.api()
parent_request_uuid = "qr1hi-xvhdp-zzzzzzzzzzzzzzz"
namefilter = "bwa%"  # the "like" filter uses SQL pattern match syntax
container_request = arvados.api().container_requests().get(uuid=parent_request_uuid).execute()
parent_container_uuid = container_request["container_uuid"]
child_requests = arvados.api().container_requests().list(filters=[
    ["requesting_container_uuid", "=", parent_container_uuid],
    ["name", "like", namefilter]]).execute()
for c in child_requests["items"]:
    print("%s" % c["name"])
    for m in c["mounts"].values():
        if "portable_data_hash" in m:
            print("  %s" % m["portable_data_hash"])
{% endcodeblock %}

h2. List output of child requests

{% codeblock as python %}
import arvados
api = arvados.api()
parent_request_uuid = "qr1hi-xvhdp-zzzzzzzzzzzzzzz"
namefilter = "bwa%"  # the "like" filter uses SQL pattern match syntax
container_request = arvados.api().container_requests().get(uuid=parent_request_uuid).execute()
parent_container_uuid = container_request["container_uuid"]
child_requests = arvados.api().container_requests().list(filters=[
    ["requesting_container_uuid", "=", parent_container_uuid],
    ["name", "like", namefilter]]).execute()
output_uuids = [c["output_uuid"] for c in child_requests["items"]]
collections = arvados.api().collections().list(filters=[["uuid", "in", output_uuids]]).execute()
uuid_to_pdh = {c["uuid"]: c["portable_data_hash"] for c in collections["items"]}
for c in child_requests["items"]:
    print("%s -> %s" % (c["name"], uuid_to_pdh[c["output_uuid"]]))
{% endcodeblock %}

h2. List failed child requests

{% codeblock as python %}
import arvados
api = arvados.api()
parent_request_uuid = "qr1hi-xvhdp-zzzzzzzzzzzzzzz"
container_request = arvados.api().container_requests().get(uuid=parent_request_uuid).execute()
parent_container_uuid = container_request["container_uuid"]
child_requests = arvados.api().container_requests().list(filters=[
    ["requesting_container_uuid", "=", parent_container_uuid]], limit=1000).execute()
child_containers = {c["container_uuid"]: c for c in child_requests["items"]}
cancelled_child_containers = arvados.api().containers().list(filters=[
    ["exit_code", "!=", "0"],
    ["uuid", "in", child_containers.keys()]], limit=1000).execute()
for c in cancelled_child_containers["items"]:
    print("%s (%s)" % (child_containers[c["uuid"]]["name"], child_containers[c["uuid"]]["uuid"]))
{% endcodeblock %}

h2. Get log of a child request

{% codeblock as python %}
import arvados
import arvados.collection
api = arvados.api()
container_request_uuid = "qr1hi-xvhdp-zzzzzzzzzzzzzzz"
container_request = arvados.api().container_requests().get(uuid=container_request_uuid).execute()
collection = arvados.collection.CollectionReader(container_request["log_uuid"])
for c in collection:
    print(collection.open(c).read())
{% endcodeblock %}

h2. Create a collection sharing link

{% codeblock as python %}
import arvados
api = arvados.api()
download="https://your.download.server"
collection_uuid="qr1hi-4zz18-zzzzzzzzzzzzzzz"
token = api.api_client_authorizations().create(body={"api_client_authorization":{"scopes": [
    "GET /arvados/v1/collections/%s" % collection_uuid,
    "GET /arvados/v1/collections/%s/" % collection_uuid,
    "GET /arvados/v1/keep_services/accessible"]}}).execute()
print("%s/c=%s/t=%s/_/" % (download, collection_uuid, token["api_token"]))
{% endcodeblock %}

h2. Combine two or more collections

Note, if two collections have files of the same name, the contents will be concatenated in the resulting manifest.

{% codeblock as python %}
import arvados
import arvados.collection
api = arvados.api()
project_uuid = "qr1hi-tpzed-zzzzzzzzzzzzzzz"
collection_uuids = ["qr1hi-4zz18-aaaaaaaaaaaaaaa", "qr1hi-4zz18-bbbbbbbbbbbbbbb"]
combined_manifest = ""
for u in collection_uuids:
    c = api.collections().get(uuid=u).execute()
    combined_manifest += c["manifest_text"]
newcol = arvados.collection.Collection(combined_manifest)
newcol.save_new(name="My combined collection", owner_uuid=project_uuid)
{% endcodeblock %}

h2. Upload a file into a new collection

{% codeblock as python %}
import arvados
import arvados.collection

project_uuid = "qr1hi-j7d0g-zzzzzzzzzzzzzzz"
collection_name = "My collection"
filename = "file1.txt"

api = arvados.api()
c = arvados.collection.Collection()
with open(filename, "rb") as reader:
    with c.open(filename, "wb") as writer:
        content = reader.read(128*1024)
        while content:
            writer.write(content)
            content = reader.read(128*1024)
c.save_new(name=collection_name, owner_uuid=project_uuid)
print("Saved %s to %s" % (collection_name, c.manifest_locator()))
{% endcodeblock %}

h2. Download a file from a collection

{% codeblock as python %}
import arvados
import arvados.collection

collection_uuid = "qr1hi-4zz18-zzzzzzzzzzzzzzz"
filename = "file1.txt"

api = arvados.api()
c = arvados.collection.CollectionReader(collection_uuid)
with c.open(filename, "rb") as reader:
    with open(filename, "wb") as writer:
        content = reader.read(128*1024)
        while content:
            writer.write(content)
            content = reader.read(128*1024)
print("Finished downloading %s" % filename)
{% endcodeblock %}