|
1 | 1 | { |
2 | 2 | "cells": [ |
3 | | - { |
4 | | - "attachments": {}, |
5 | | - "cell_type": "markdown", |
6 | | - "metadata": { |
7 | | - "id": "4iM5tpoN3sR3" |
8 | | - }, |
9 | | - "source": [ |
10 | | - "<a href=\"https://colab.research.google.com/github/Exabyte-io/api-examples/blob/dev/examples/job/create_and_submit_job.ipynb\" target=\"_parent\">\n", |
11 | | - "<img alt=\"Open in Google Colab\" src=\"https://user-images.githubusercontent.com/20477508/128780728-491fea90-9b23-495f-a091-11681150db37.jpeg\" width=\"150\" border=\"0\">\n", |
12 | | - "</a>" |
13 | | - ] |
14 | | - }, |
15 | 3 | { |
16 | 4 | "attachments": {}, |
17 | 5 | "cell_type": "markdown", |
18 | 6 | "metadata": { |
19 | 7 | "id": "mHaR0BOJ3sR5" |
20 | 8 | }, |
21 | | - "source": [ |
22 | | - "# Overview\n", |
23 | | - "\n", |
24 | | - "This example demonstrates how to create and submit a job via [Job](https://docs.mat3ra.com/api/Job/put_jobs_create) endpoints." |
25 | | - ] |
| 9 | + "source": "# Overview\n\nThis example demonstrates how to create and submit a job via [Job](https://docs.mat3ra.com/api/Job/put_jobs_create) endpoints." |
26 | 10 | }, |
27 | 11 | { |
28 | | - "attachments": {}, |
29 | 12 | "cell_type": "markdown", |
30 | 13 | "metadata": { |
31 | 14 | "id": "i-kHKlAM3sR5" |
32 | 15 | }, |
33 | | - "source": [ |
34 | | - "# Complete Authorization Form and Initialize Settings\n", |
35 | | - "\n", |
36 | | - "This will also determine environment and set all environment variables. We determine if we are using Jupyter Notebooks or Google Colab to run this tutorial.\n", |
37 | | - "\n", |
38 | | - "If you are running this notebook from Google Colab, Colab takes ~1 min to execute the following cell.\n", |
39 | | - "\n", |
40 | | - "ACCOUNT_ID and AUTH_TOKEN - Authentication parameters needed for when making requests to [Mat3ra.com's API Endpoints](https://docs.mat3ra.com/rest-api/endpoints/).\n", |
41 | | - "\n", |
42 | | - "ORGANIZATION_ID - Authentication parameter needed for when working with collaborative accounts https://docs.mat3ra.com/collaboration/organizations/overview/\n", |
43 | | - "\n", |
44 | | - "> <span style=\"color: orange\">**NOTE**</span>: If you are running this notebook from Jupyter, the variables ACCOUNT_ID, AUTH_TOKEN, and ORGANIZATION_ID should be set in the file [settings.json](../../utils/settings.json) if you need to use these variables. To obtain API token parameters, please see the following link to the documentation explaining how to get them: https://docs.mat3ra.com/accounts/ui/preferences/api/" |
45 | | - ] |
| 16 | + "source": "## Authenticate and initialize API client\n\n### Authenticate\nAuthenticate in the browser (OIDC device flow) or via JupyterLite host injection. Credentials are stored in environment variables.\n\n### Initialize API client\nCreate an authenticated API client and resolve the owner account ID." |
46 | 17 | }, |
47 | 18 | { |
48 | | - "metadata": {}, |
49 | 19 | "cell_type": "code", |
50 | | - "outputs": [], |
51 | 20 | "execution_count": null, |
| 21 | + "metadata": {}, |
| 22 | + "outputs": [], |
52 | 23 | "source": [ |
53 | | - "# @title Authorization Form\n", |
54 | | - "ACCOUNT_ID = \"ACCOUNT_ID\" # @param {type:\"string\"}\n", |
55 | | - "AUTH_TOKEN = \"AUTH_TOKEN\" # @param {type:\"string\"}\n", |
56 | | - "ORGANIZATION_ID = \"ORGANIZATION_ID\" # @param {type:\"string\"}\n", |
57 | | - "\n", |
58 | | - "import os\n", |
59 | | - "import sys\n", |
60 | | - "\n", |
61 | | - "if \"COLAB_JUPYTER_IP\" in os.environ:\n", |
62 | | - " os.environ.update(\n", |
63 | | - " dict(\n", |
64 | | - " ACCOUNT_ID=ACCOUNT_ID,\n", |
65 | | - " AUTH_TOKEN=AUTH_TOKEN,\n", |
66 | | - " ORGANIZATION_ID=ORGANIZATION_ID,\n", |
67 | | - " )\n", |
68 | | - " )\n", |
69 | | - "\n", |
70 | | - " !GIT_BRANCH=\"dev\"; export GIT_BRANCH; curl -s \"https://raw.githubusercontent.com/Exabyte-io/api-examples/${GIT_BRANCH}/scripts/env.sh\" | bash\n", |
71 | | - "\n", |
72 | | - "if sys.platform == \"emscripten\":\n", |
73 | | - " apiConfig = data_from_host.get(\"apiConfig\")\n", |
74 | | - " os.environ.update(data_from_host.get(\"environ\", {}))\n", |
75 | | - " os.environ.update(\n", |
76 | | - " dict(\n", |
77 | | - " ACCOUNT_ID=apiConfig.get(\"accountId\"),\n", |
78 | | - " AUTH_TOKEN=apiConfig.get(\"authToken\"),\n", |
79 | | - " ORGANIZATION_ID=apiConfig.get(\"organizationId\") or \"\",\n", |
80 | | - " )\n", |
81 | | - " )\n", |
82 | | - "\n", |
83 | 24 | "from mat3ra.notebooks_utils.packages import install_packages\n", |
84 | 25 | "\n", |
85 | 26 | "await install_packages(\"api\")" |
86 | 27 | ] |
87 | 28 | }, |
88 | 29 | { |
89 | | - "metadata": {}, |
90 | 30 | "cell_type": "code", |
91 | | - "outputs": [], |
92 | 31 | "execution_count": null, |
| 32 | + "metadata": {}, |
| 33 | + "outputs": [], |
93 | 34 | "source": [ |
94 | | - "from mat3ra.notebooks_utils.api.settings import ENDPOINT_ARGS, ACCOUNT_ID\n", |
95 | | - "from mat3ra.notebooks_utils.ui import display_JSON\n", |
| 35 | + "from mat3ra.notebooks_utils.auth import authenticate\n", |
96 | 36 | "\n", |
97 | | - "from mat3ra.api_client.endpoints.jobs import JobEndpoints\n", |
98 | | - "from mat3ra.api_client.endpoints.materials import MaterialEndpoints\n", |
99 | | - "from mat3ra.api_client.endpoints.workflows import WorkflowEndpoints" |
| 37 | + "await authenticate()" |
100 | 38 | ] |
101 | 39 | }, |
102 | 40 | { |
103 | | - "attachments": {}, |
104 | | - "cell_type": "markdown", |
105 | | - "metadata": { |
106 | | - "id": "zjK-3ew_3sR7" |
107 | | - }, |
| 41 | + "cell_type": "code", |
| 42 | + "execution_count": null, |
| 43 | + "metadata": {}, |
| 44 | + "outputs": [], |
108 | 45 | "source": [ |
109 | | - "## Initialize the endpoints" |
| 46 | + "import os\n", |
| 47 | + "\n", |
| 48 | + "from mat3ra.api_client import APIClient\n", |
| 49 | + "\n", |
| 50 | + "client = APIClient.authenticate()\n", |
| 51 | + "selected_account = client.my_account\n", |
| 52 | + "OWNER_ID = os.getenv(\"ORGANIZATION_ID\") or selected_account.id" |
110 | 53 | ] |
111 | 54 | }, |
112 | 55 | { |
| 56 | + "metadata": {}, |
113 | 57 | "cell_type": "code", |
114 | | - "execution_count": null, |
115 | | - "metadata": { |
116 | | - "id": "IEPURdwU3sR7" |
117 | | - }, |
118 | 58 | "outputs": [], |
| 59 | + "execution_count": null, |
119 | 60 | "source": [ |
120 | | - "job_endpoints = JobEndpoints(*ENDPOINT_ARGS)\n", |
121 | | - "material_endpoints = MaterialEndpoints(*ENDPOINT_ARGS)\n", |
122 | | - "workflow_endpoints = WorkflowEndpoints(*ENDPOINT_ARGS)" |
| 61 | + "from mat3ra.notebooks_utils.ui import display_JSON" |
123 | 62 | ] |
124 | 63 | }, |
125 | 64 | { |
|
128 | 67 | "metadata": { |
129 | 68 | "id": "21aAFcwp3sR8" |
130 | 69 | }, |
131 | | - "source": [ |
132 | | - "Set job name." |
133 | | - ] |
| 70 | + "source": "Set job name." |
134 | 71 | }, |
135 | 72 | { |
136 | 73 | "cell_type": "code", |
|
149 | 86 | "metadata": { |
150 | 87 | "id": "efnIafH13sR9" |
151 | 88 | }, |
152 | | - "source": [ |
153 | | - "## Retrieve IDs\n", |
154 | | - "\n", |
155 | | - "Default account's materail and workflow are used in this example to create the job. Adjust the queries to use different material and workflow." |
156 | | - ] |
| 89 | + "source": "## Retrieve IDs\n\nDefault account's materail and workflow are used in this example to create the job. Adjust the queries to use different material and workflow." |
157 | 90 | }, |
158 | 91 | { |
159 | 92 | "cell_type": "code", |
|
163 | 96 | }, |
164 | 97 | "outputs": [], |
165 | 98 | "source": [ |
166 | | - "OWNER_ID = os.getenv(\"ORGANIZATION_ID\") or ACCOUNT_ID\n", |
167 | | - "default_material = material_endpoints.list({\"isDefault\": True, \"owner._id\": OWNER_ID})[0]\n", |
168 | | - "default_workflow = workflow_endpoints.list({\"isDefault\": True, \"owner._id\": OWNER_ID})[0]\n", |
| 99 | + "OWNER_ID = os.getenv(\"ORGANIZATION_ID\") or selected_account.id\n", |
| 100 | + "default_material = client.materials.list({\"isDefault\": True, \"owner._id\": OWNER_ID})[0]\n", |
| 101 | + "default_workflow = client.workflows.list({\"isDefault\": True, \"owner._id\": OWNER_ID})[0]\n", |
169 | 102 | "\n", |
170 | 103 | "material_id = default_material[\"_id\"]\n", |
171 | 104 | "workflow_id = default_workflow[\"_id\"]" |
|
177 | 110 | "metadata": { |
178 | 111 | "id": "IL8DTcqB3sR9" |
179 | 112 | }, |
180 | | - "source": [ |
181 | | - "## Create job config\n", |
182 | | - "\n", |
183 | | - "The job belongs to user's default account and it is created inside the defauult account's project. " |
184 | | - ] |
| 113 | + "source": "## Create job config\n\nThe job belongs to user's default account and it is created inside the defauult account's project. " |
185 | 114 | }, |
186 | 115 | { |
187 | 116 | "cell_type": "code", |
|
205 | 134 | "metadata": { |
206 | 135 | "id": "L_pFN5n33sR-" |
207 | 136 | }, |
208 | | - "source": [ |
209 | | - "## Create and submit job" |
210 | | - ] |
| 137 | + "source": "## Create and submit job" |
211 | 138 | }, |
212 | 139 | { |
213 | 140 | "cell_type": "code", |
|
217 | 144 | }, |
218 | 145 | "outputs": [], |
219 | 146 | "source": [ |
220 | | - "job = job_endpoints.create(config)\n", |
221 | | - "job_endpoints.submit(job[\"_id\"])" |
| 147 | + "job = client.jobs.create(config)\n", |
| 148 | + "client.jobs.submit(job[\"_id\"])" |
222 | 149 | ] |
223 | 150 | }, |
224 | 151 | { |
|
227 | 154 | "metadata": { |
228 | 155 | "id": "1dkTzmQn3sR-" |
229 | 156 | }, |
230 | | - "source": [ |
231 | | - "## Print the job\n", |
232 | | - "\n", |
233 | | - "Print the job in pretty JSON below. Check `status` field to make sure job is submiited." |
234 | | - ] |
| 157 | + "source": "## Print the job\n\nPrint the job in pretty JSON below. Check `status` field to make sure job is submiited." |
235 | 158 | }, |
236 | 159 | { |
237 | 160 | "cell_type": "code", |
|
245 | 168 | }, |
246 | 169 | "outputs": [], |
247 | 170 | "source": [ |
248 | | - "job = job_endpoints.get(job[\"_id\"])\n", |
| 171 | + "job = client.jobs.get(job[\"_id\"])\n", |
249 | 172 | "display_JSON(job)" |
250 | 173 | ] |
251 | 174 | } |
|
0 commit comments