Skip to content

Commit 083612e

Browse files
authored
Merge pull request #329 from Exabyte-io/chore/SOF-7908
chore/SOF 7908
2 parents 3712fa8 + 8ca2bdc commit 083612e

16 files changed

Lines changed: 1638 additions & 2486 deletions

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Notebooks demonstrating the Mat3ra REST API, in roughly the order a new user sho
88

99
| Folder | Notebook | Description |
1010
|--------|----------|-------------|
11-
| [system](examples/system/) | [Get Authentication Params](examples/system/get_authentication_params.ipynb) | Programmatically find your user ID and access token. |
11+
| [system](examples/system/) | [Get Authentication Params](examples/system/get_authentication_params.ipynb) | Authenticate via OIDC and inspect account credentials. |
1212
| [material](examples/material/) | [Get Materials by Formula](examples/material/get_materials_by_formula.ipynb) | Query materials stored on your account by chemical formula. |
1313
| [material](examples/material/) | [Create Material](examples/material/create_material.ipynb) | Generate a material in [JSON format](https://docs.mat3ra.com/materials/data/) and upload it to your account. |
1414
| [material](examples/material/) | [Import Materials from POSCAR](examples/material/upload_materials_from_file_poscar.ipynb) | Import materials directly from POSCAR files. |
@@ -155,13 +155,11 @@ In order to run or edit the examples:
155155
156156
1. Assert an existing Mat3ra.com account. Examples require an account to run. New users can register [here](https://platform.mat3ra.com/register) to obtain one.
157157
158-
2. Open [settings](src/py/mat3ra/notebooks_utils/core/api/settings.json) and adjust it to provide the API authentication parameters. See the [corresponding example](examples/system/get_authentication_params.ipynb) to learn how to obtain the authentication parameters. It is also possible to generate an API token by logging in to [Mat3ra platform](https://platform.mat3ra.com/), navigating to the Account Preferences, and clicking the 'Generate new token' button under API Tokens. More details can be found [here](https://docs.mat3ra.com/accounts/ui/preferences/api/).
158+
2. Open the desired example notebook and run all cells. Authentication uses OIDC device flow via `await authenticate()` — a browser popup opens for login. On JupyterLite (platform-embedded notebooks), credentials are injected automatically.
159159
160-
3. Open the desired example notebook, adjust it as necessary and run. One can speed up the notebooks execution after running the [Get Authentication Params](examples/system/get_authentication_params.ipynb) one by reusing the kernel from the first notebook.
160+
3. Optionally, for local Jupyter without OIDC, set legacy API token values in [settings.json](src/py/mat3ra/notebooks_utils/core/api/settings.json). See [Get Authentication Params](examples/system/get_authentication_params.ipynb) for details. API tokens can also be generated in [Account Preferences](https://docs.mat3ra.com/accounts/ui/preferences/api/) on the platform.
161161
162-
<img src="images/reusable-kernel.png" width="250px" />
163-
164-
NOTE: The Materials Project API key should be obtained from [https://legacy.materialsproject.org/open](https://legacy.materialsproject.org/open).
162+
NOTE: The Materials Project API key should be set in `settings.json` and obtained from [https://legacy.materialsproject.org/open](https://legacy.materialsproject.org/open).
165163
166164
167165
## Contribute

examples/job/create_and_submit_job.ipynb

Lines changed: 33 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,64 @@
11
{
22
"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-
},
153
{
164
"attachments": {},
175
"cell_type": "markdown",
186
"metadata": {
197
"id": "mHaR0BOJ3sR5"
208
},
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."
2610
},
2711
{
28-
"attachments": {},
2912
"cell_type": "markdown",
3013
"metadata": {
3114
"id": "i-kHKlAM3sR5"
3215
},
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."
4617
},
4718
{
48-
"metadata": {},
4919
"cell_type": "code",
50-
"outputs": [],
5120
"execution_count": null,
21+
"metadata": {},
22+
"outputs": [],
5223
"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",
8324
"from mat3ra.notebooks_utils.packages import install_packages\n",
8425
"\n",
8526
"await install_packages(\"api\")"
8627
]
8728
},
8829
{
89-
"metadata": {},
9030
"cell_type": "code",
91-
"outputs": [],
9231
"execution_count": null,
32+
"metadata": {},
33+
"outputs": [],
9334
"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",
9636
"\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()"
10038
]
10139
},
10240
{
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": [],
10845
"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"
11053
]
11154
},
11255
{
56+
"metadata": {},
11357
"cell_type": "code",
114-
"execution_count": null,
115-
"metadata": {
116-
"id": "IEPURdwU3sR7"
117-
},
11858
"outputs": [],
59+
"execution_count": null,
11960
"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"
12362
]
12463
},
12564
{
@@ -128,9 +67,7 @@
12867
"metadata": {
12968
"id": "21aAFcwp3sR8"
13069
},
131-
"source": [
132-
"Set job name."
133-
]
70+
"source": "Set job name."
13471
},
13572
{
13673
"cell_type": "code",
@@ -149,11 +86,7 @@
14986
"metadata": {
15087
"id": "efnIafH13sR9"
15188
},
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."
15790
},
15891
{
15992
"cell_type": "code",
@@ -163,9 +96,9 @@
16396
},
16497
"outputs": [],
16598
"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",
169102
"\n",
170103
"material_id = default_material[\"_id\"]\n",
171104
"workflow_id = default_workflow[\"_id\"]"
@@ -177,11 +110,7 @@
177110
"metadata": {
178111
"id": "IL8DTcqB3sR9"
179112
},
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. "
185114
},
186115
{
187116
"cell_type": "code",
@@ -205,9 +134,7 @@
205134
"metadata": {
206135
"id": "L_pFN5n33sR-"
207136
},
208-
"source": [
209-
"## Create and submit job"
210-
]
137+
"source": "## Create and submit job"
211138
},
212139
{
213140
"cell_type": "code",
@@ -217,8 +144,8 @@
217144
},
218145
"outputs": [],
219146
"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\"])"
222149
]
223150
},
224151
{
@@ -227,11 +154,7 @@
227154
"metadata": {
228155
"id": "1dkTzmQn3sR-"
229156
},
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."
235158
},
236159
{
237160
"cell_type": "code",
@@ -245,7 +168,7 @@
245168
},
246169
"outputs": [],
247170
"source": [
248-
"job = job_endpoints.get(job[\"_id\"])\n",
171+
"job = client.jobs.get(job[\"_id\"])\n",
249172
"display_JSON(job)"
250173
]
251174
}

0 commit comments

Comments
 (0)