|
| 1 | +# Examples |
| 2 | + |
| 3 | +## Get Node Info |
| 4 | + |
| 5 | +Connect to a node and retrieve its information: |
| 6 | + |
| 7 | +```python |
| 8 | +from pyodx import Node, exceptions |
| 9 | + |
| 10 | +node = Node.from_url("http://localhost:3000?token=abc") |
| 11 | + |
| 12 | +try: |
| 13 | + print(node.info()) |
| 14 | +except exceptions.NodeConnectionError as e: |
| 15 | + print("Cannot connect: " + str(e)) |
| 16 | +``` |
| 17 | + |
| 18 | +## Create a Task |
| 19 | + |
| 20 | +Upload images, process them, and download results: |
| 21 | + |
| 22 | +```python |
| 23 | +import os |
| 24 | +from pyodx import Node, exceptions |
| 25 | + |
| 26 | +node = Node("localhost", 3000) |
| 27 | + |
| 28 | +try: |
| 29 | + # Start a task |
| 30 | + print("Uploading images...") |
| 31 | + task = node.create_task( |
| 32 | + ["images/image_1.jpg", "images/image_2.jpg"], |
| 33 | + {"dsm": True, "orthophoto-resolution": 4}, |
| 34 | + ) |
| 35 | + print(task.info()) |
| 36 | + |
| 37 | + try: |
| 38 | + # Block until the task is finished |
| 39 | + task.wait_for_completion() |
| 40 | + |
| 41 | + print("Task completed, downloading results...") |
| 42 | + task.download_assets("./results") |
| 43 | + print("Assets saved in ./results (%s)" % os.listdir("./results")) |
| 44 | + |
| 45 | + # Restart task and this time compute dtm |
| 46 | + task.restart({"dtm": True}) |
| 47 | + task.wait_for_completion() |
| 48 | + |
| 49 | + print("Task completed, downloading results...") |
| 50 | + task.download_assets("./results_with_dtm") |
| 51 | + print("Assets saved in ./results_with_dtm (%s)" % os.listdir("./results_with_dtm")) |
| 52 | + |
| 53 | + except exceptions.TaskFailedError as e: |
| 54 | + print("\n".join(task.output())) |
| 55 | + |
| 56 | +except exceptions.NodeConnectionError as e: |
| 57 | + print("Cannot connect: %s" % e) |
| 58 | +except exceptions.NodeResponseError as e: |
| 59 | + print("Error: %s" % e) |
| 60 | +``` |
| 61 | + |
| 62 | +## Upload Progress Callback |
| 63 | + |
| 64 | +Track upload progress with a callback: |
| 65 | + |
| 66 | +```python |
| 67 | +from pyodx import Node |
| 68 | + |
| 69 | +node = Node("localhost", 3000) |
| 70 | + |
| 71 | +def progress(percent): |
| 72 | + print("Upload: %.2f%%" % percent) |
| 73 | + |
| 74 | +task = node.create_task( |
| 75 | + ["image_1.jpg", "image_2.jpg"], |
| 76 | + progress_callback=progress, |
| 77 | +) |
| 78 | +``` |
| 79 | + |
| 80 | +## Task Status Callback |
| 81 | + |
| 82 | +Monitor task status while waiting for completion: |
| 83 | + |
| 84 | +```python |
| 85 | +from pyodx import Node |
| 86 | + |
| 87 | +node = Node("localhost", 3000) |
| 88 | + |
| 89 | +task = node.create_task(["image_1.jpg", "image_2.jpg"]) |
| 90 | + |
| 91 | +def status_update(info): |
| 92 | + print("Status: %s, Progress: %.1f%%" % (info.status, info.progress)) |
| 93 | + |
| 94 | +task.wait_for_completion(status_callback=status_update) |
| 95 | +``` |
0 commit comments