diff --git a/other/materials_designer/workflows/band_gap.ipynb b/other/materials_designer/workflows/band_gap.ipynb index 055199b9..104c2077 100644 --- a/other/materials_designer/workflows/band_gap.ipynb +++ b/other/materials_designer/workflows/band_gap.ipynb @@ -91,7 +91,10 @@ "metadata": {}, "source": [ "## 2. Authenticate and initialize API client\n", - "### 2.1. Upon running of this cell, the browser with Mat3ra CLI login page will open." + "### 2.1. Authenticate and Initialize API Client\n", + "Authenticate in the browser and have credentials stored in environment variable \"OIDC_ACCESS_TOKEN\".\n", + "The token can be passed to the `get_authenticated_client` function as `access_token` parameter to preserve the authentication and skip logging in in case of the notebook refresh.\n", + "Tokens are invalidated after 30 min." ] }, { @@ -101,10 +104,9 @@ "metadata": {}, "outputs": [], "source": [ - "from utils.auth import authenticate\n", + "from utils.auth import get_authenticated_client\n", "\n", - "# Authenticate and have credentials stored in environment variables\n", - "await authenticate()" + "client = await get_authenticated_client()" ] }, { @@ -112,8 +114,7 @@ "id": "6", "metadata": {}, "source": [ - "### 2.2. Initialize API client\n", - "Authorization is done via environment variables, where token is stored after authentication." + "### 2.2. List accounts" ] }, { @@ -123,9 +124,7 @@ "metadata": {}, "outputs": [], "source": [ - "from mat3ra.api_client import APIClient\n", - "\n", - "client = APIClient.authenticate()" + "client.list_accounts()" ] }, { @@ -134,7 +133,7 @@ "metadata": {}, "source": [ "### 2.3. Select account to work under\n", - "You can choose to use your personal one or any of the organizations you belong to." + "You can choose to use your personal account or any of the organizations you belong to." ] }, { @@ -143,16 +142,6 @@ "id": "9", "metadata": {}, "outputs": [], - "source": [ - "client.list_accounts()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "10", - "metadata": {}, - "outputs": [], "source": [ "selected_account = client.my_account\n", "\n", @@ -165,7 +154,7 @@ }, { "cell_type": "markdown", - "id": "11", + "id": "10", "metadata": {}, "source": [ "## 3. Create material\n", @@ -175,7 +164,7 @@ { "cell_type": "code", "execution_count": null, - "id": "12", + "id": "11", "metadata": {}, "outputs": [], "source": [ @@ -192,7 +181,7 @@ }, { "cell_type": "markdown", - "id": "13", + "id": "12", "metadata": {}, "source": [ "### 3.2. Save material to the platform" @@ -201,7 +190,7 @@ { "cell_type": "code", "execution_count": null, - "id": "14", + "id": "13", "metadata": {}, "outputs": [], "source": [ @@ -215,7 +204,7 @@ }, { "cell_type": "markdown", - "id": "15", + "id": "14", "metadata": { "tags": [] }, @@ -226,7 +215,7 @@ { "cell_type": "code", "execution_count": null, - "id": "16", + "id": "15", "metadata": {}, "outputs": [], "source": [ @@ -235,7 +224,7 @@ }, { "cell_type": "markdown", - "id": "17", + "id": "16", "metadata": {}, "source": [ "## 5. Create workflow and set its parameters\n", @@ -245,7 +234,7 @@ { "cell_type": "code", "execution_count": null, - "id": "18", + "id": "17", "metadata": {}, "outputs": [], "source": [ @@ -258,7 +247,7 @@ { "cell_type": "code", "execution_count": null, - "id": "19", + "id": "18", "metadata": {}, "outputs": [], "source": [ @@ -269,7 +258,7 @@ }, { "cell_type": "markdown", - "id": "20", + "id": "19", "metadata": {}, "source": [ "### 5.2. Create workflow from standard workflows and preview it" @@ -278,7 +267,7 @@ { "cell_type": "code", "execution_count": null, - "id": "21", + "id": "20", "metadata": {}, "outputs": [], "source": [ @@ -294,7 +283,7 @@ }, { "cell_type": "markdown", - "id": "22", + "id": "21", "metadata": {}, "source": [ "### 5.3. Add relaxation subworkflow" @@ -303,7 +292,7 @@ { "cell_type": "code", "execution_count": null, - "id": "23", + "id": "22", "metadata": {}, "outputs": [], "source": [ @@ -317,7 +306,7 @@ }, { "cell_type": "markdown", - "id": "24", + "id": "23", "metadata": {}, "source": [ "### 5.4. Change subworkflow details (Model subtype)\n", @@ -327,7 +316,7 @@ { "cell_type": "code", "execution_count": null, - "id": "25", + "id": "24", "metadata": {}, "outputs": [], "source": [ @@ -340,7 +329,7 @@ }, { "cell_type": "markdown", - "id": "26", + "id": "25", "metadata": {}, "source": [ "#### 5.4.2. Modify model in subworkflow units" @@ -349,7 +338,7 @@ { "cell_type": "code", "execution_count": null, - "id": "27", + "id": "26", "metadata": {}, "outputs": [], "source": [ @@ -379,7 +368,7 @@ }, { "cell_type": "markdown", - "id": "28", + "id": "27", "metadata": {}, "source": [ "### 5.5. Modify k-grid in subworkflow units\n", @@ -389,7 +378,7 @@ { "cell_type": "code", "execution_count": null, - "id": "29", + "id": "28", "metadata": {}, "outputs": [], "source": [ @@ -402,7 +391,7 @@ }, { "cell_type": "markdown", - "id": "30", + "id": "29", "metadata": {}, "source": [ "#### 5.5.3. Modify workflow units with new context" @@ -411,7 +400,7 @@ { "cell_type": "code", "execution_count": null, - "id": "31", + "id": "30", "metadata": {}, "outputs": [], "source": [ @@ -439,7 +428,7 @@ }, { "cell_type": "markdown", - "id": "32", + "id": "31", "metadata": {}, "source": [ "### 5.6. Save workflow to collection" @@ -448,7 +437,7 @@ { "cell_type": "code", "execution_count": null, - "id": "33", + "id": "32", "metadata": {}, "outputs": [], "source": [ @@ -462,7 +451,7 @@ }, { "cell_type": "markdown", - "id": "34", + "id": "33", "metadata": {}, "source": [ "## 6. Create the compute configuration\n", @@ -472,7 +461,7 @@ { "cell_type": "code", "execution_count": null, - "id": "35", + "id": "34", "metadata": {}, "outputs": [], "source": [ @@ -482,7 +471,7 @@ }, { "cell_type": "markdown", - "id": "36", + "id": "35", "metadata": {}, "source": [ "### 6.2. Create compute configuration for the job" @@ -491,7 +480,7 @@ { "cell_type": "code", "execution_count": null, - "id": "37", + "id": "36", "metadata": {}, "outputs": [], "source": [ @@ -507,7 +496,7 @@ }, { "cell_type": "markdown", - "id": "38", + "id": "37", "metadata": {}, "source": [ "## 7. Create the job with material and workflow configuration\n", @@ -517,7 +506,7 @@ { "cell_type": "code", "execution_count": null, - "id": "39", + "id": "38", "metadata": {}, "outputs": [], "source": [ @@ -527,7 +516,7 @@ }, { "cell_type": "markdown", - "id": "40", + "id": "39", "metadata": {}, "source": [ "### 7.2. Create job" @@ -536,7 +525,7 @@ { "cell_type": "code", "execution_count": null, - "id": "41", + "id": "40", "metadata": {}, "outputs": [], "source": [ @@ -566,7 +555,7 @@ }, { "cell_type": "markdown", - "id": "42", + "id": "41", "metadata": {}, "source": [ "## 8. Submit the job and monitor the status" @@ -575,7 +564,7 @@ { "cell_type": "code", "execution_count": null, - "id": "43", + "id": "42", "metadata": {}, "outputs": [], "source": [ @@ -585,7 +574,7 @@ { "cell_type": "code", "execution_count": null, - "id": "44", + "id": "43", "metadata": {}, "outputs": [], "source": [ @@ -596,7 +585,7 @@ }, { "cell_type": "markdown", - "id": "45", + "id": "44", "metadata": {}, "source": [ "## 9. Retrieve results" @@ -605,7 +594,7 @@ { "cell_type": "code", "execution_count": null, - "id": "46", + "id": "45", "metadata": {}, "outputs": [], "source": [ @@ -619,7 +608,7 @@ { "cell_type": "code", "execution_count": null, - "id": "47", + "id": "46", "metadata": {}, "outputs": [], "source": [] diff --git a/other/materials_designer/workflows/total_energy.ipynb b/other/materials_designer/workflows/total_energy.ipynb index f51ff0fa..7b63099f 100644 --- a/other/materials_designer/workflows/total_energy.ipynb +++ b/other/materials_designer/workflows/total_energy.ipynb @@ -109,8 +109,10 @@ "metadata": {}, "source": [ "## 2. Authenticate and initialize API client\n", - "### 2.1. Authenticate\n", - "Authenticate in the browser and have credentials stored in environment variable \"OIDC_ACCESS_TOKEN\".\n" + "### 2.1. Authenticate and Initialize API Client\n", + "Authenticate in the browser and have credentials stored in environment variable \"OIDC_ACCESS_TOKEN\".\n", + "The token can be passed to the `get_authenticated_client` function as `access_token` parameter to preserve the authentication and skip logging in in case of the notebook refresh.\n", + "Tokens are invalidated after 30 min.\n" ] }, { @@ -120,10 +122,9 @@ "metadata": {}, "outputs": [], "source": [ - "from utils.auth import authenticate\n", + "from utils.auth import get_authenticated_client\n", "\n", - "\n", - "await authenticate()" + "client = await get_authenticated_client()" ] }, { @@ -131,7 +132,7 @@ "id": "7", "metadata": {}, "source": [ - "### 2.2. Initialize API Client\n" + "### 2.2. List accounts" ] }, { @@ -141,10 +142,7 @@ "metadata": {}, "outputs": [], "source": [ - "from mat3ra.api_client import APIClient\n", - "\n", - "client = APIClient.authenticate()\n", - "client" + "client.list_accounts()" ] }, { @@ -161,16 +159,6 @@ "id": "10", "metadata": {}, "outputs": [], - "source": [ - "client.list_accounts()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "11", - "metadata": {}, - "outputs": [], "source": [ "selected_account = client.my_account\n", "\n", @@ -183,7 +171,7 @@ }, { "cell_type": "markdown", - "id": "12", + "id": "11", "metadata": {}, "source": [ "### 2.4. Select project" @@ -192,7 +180,7 @@ { "cell_type": "code", "execution_count": null, - "id": "13", + "id": "12", "metadata": {}, "outputs": [], "source": [ @@ -203,7 +191,7 @@ }, { "cell_type": "markdown", - "id": "14", + "id": "13", "metadata": {}, "source": [ "## 3. Create material\n", @@ -213,7 +201,7 @@ { "cell_type": "code", "execution_count": null, - "id": "15", + "id": "14", "metadata": {}, "outputs": [], "source": [ @@ -230,7 +218,7 @@ }, { "cell_type": "markdown", - "id": "16", + "id": "15", "metadata": {}, "source": [ "### 3.2. Save material to the platform" @@ -239,7 +227,7 @@ { "cell_type": "code", "execution_count": null, - "id": "17", + "id": "16", "metadata": {}, "outputs": [], "source": [ @@ -251,7 +239,7 @@ }, { "cell_type": "markdown", - "id": "18", + "id": "17", "metadata": {}, "source": [ "## 4. Create workflow and set its parameters\n", @@ -261,7 +249,7 @@ { "cell_type": "code", "execution_count": null, - "id": "19", + "id": "18", "metadata": {}, "outputs": [], "source": [ @@ -275,7 +263,7 @@ }, { "cell_type": "markdown", - "id": "20", + "id": "19", "metadata": {}, "source": [ "### 4.2. Create workflow from standard workflows and preview it" @@ -284,7 +272,7 @@ { "cell_type": "code", "execution_count": null, - "id": "21", + "id": "20", "metadata": {}, "outputs": [], "source": [ @@ -301,7 +289,7 @@ }, { "cell_type": "markdown", - "id": "22", + "id": "21", "metadata": {}, "source": [ "### 4.3. Modify workflow (Optional)\n", @@ -311,7 +299,7 @@ { "cell_type": "code", "execution_count": null, - "id": "23", + "id": "22", "metadata": {}, "outputs": [], "source": [ @@ -321,7 +309,7 @@ }, { "cell_type": "markdown", - "id": "24", + "id": "23", "metadata": {}, "source": [ "#### 4.3.2. Modify model and method parameters (Optional)\n", @@ -331,7 +319,7 @@ { "cell_type": "code", "execution_count": null, - "id": "25", + "id": "24", "metadata": {}, "outputs": [], "source": [ @@ -361,7 +349,7 @@ }, { "cell_type": "markdown", - "id": "26", + "id": "25", "metadata": {}, "source": [ "#### 4.3.3. Modify important settings\n", @@ -371,7 +359,7 @@ { "cell_type": "code", "execution_count": null, - "id": "27", + "id": "26", "metadata": {}, "outputs": [], "source": [ @@ -398,7 +386,7 @@ }, { "cell_type": "markdown", - "id": "28", + "id": "27", "metadata": {}, "source": [ "### 4.4. Save workflow to collection" @@ -407,7 +395,7 @@ { "cell_type": "code", "execution_count": null, - "id": "29", + "id": "28", "metadata": {}, "outputs": [], "source": [ @@ -423,7 +411,7 @@ }, { "cell_type": "markdown", - "id": "30", + "id": "29", "metadata": {}, "source": [ "## 5. Create the compute configuration\n", @@ -433,7 +421,7 @@ { "cell_type": "code", "execution_count": null, - "id": "31", + "id": "30", "metadata": {}, "outputs": [], "source": [ @@ -443,7 +431,7 @@ }, { "cell_type": "markdown", - "id": "32", + "id": "31", "metadata": {}, "source": [ "### 5.2. Create compute configuration for the job\n" @@ -452,7 +440,7 @@ { "cell_type": "code", "execution_count": null, - "id": "33", + "id": "32", "metadata": {}, "outputs": [], "source": [ @@ -474,7 +462,7 @@ }, { "cell_type": "markdown", - "id": "34", + "id": "33", "metadata": {}, "source": [ "## 6. Create the job with material and workflow configuration\n", @@ -484,7 +472,7 @@ { "cell_type": "code", "execution_count": null, - "id": "35", + "id": "34", "metadata": {}, "outputs": [], "source": [ @@ -515,7 +503,7 @@ }, { "cell_type": "markdown", - "id": "36", + "id": "35", "metadata": {}, "source": [ "## 7. Submit the job and monitor the status" @@ -524,7 +512,7 @@ { "cell_type": "code", "execution_count": null, - "id": "37", + "id": "36", "metadata": {}, "outputs": [], "source": [ @@ -535,7 +523,7 @@ { "cell_type": "code", "execution_count": null, - "id": "38", + "id": "37", "metadata": {}, "outputs": [], "source": [ @@ -546,7 +534,7 @@ }, { "cell_type": "markdown", - "id": "39", + "id": "38", "metadata": {}, "source": [ "## 8. Retrieve results" @@ -555,7 +543,7 @@ { "cell_type": "code", "execution_count": null, - "id": "40", + "id": "39", "metadata": {}, "outputs": [], "source": [ diff --git a/other/materials_designer/workflows/total_energy_post_processing.ipynb b/other/materials_designer/workflows/total_energy_post_processing.ipynb index 40beb29f..12bc8ab0 100644 --- a/other/materials_designer/workflows/total_energy_post_processing.ipynb +++ b/other/materials_designer/workflows/total_energy_post_processing.ipynb @@ -127,7 +127,10 @@ "metadata": {}, "source": [ "## 2. Authenticate and initialize API client\n", - "### 2.1. Authenticate" + "### 2.1. Authenticate and Initialize API Client\n", + "Authenticate in the browser and have credentials stored in environment variable \"OIDC_ACCESS_TOKEN\".\n", + "The token can be passed to the `get_authenticated_client` function as `access_token` parameter to preserve the authentication and skip logging in in case of the notebook refresh.\n", + "Tokens are invalidated after 30 min." ] }, { @@ -136,16 +139,16 @@ "metadata": {}, "outputs": [], "source": [ - "from utils.auth import authenticate\n", + "from utils.auth import get_authenticated_client\n", "\n", - "await authenticate()" + "client = await get_authenticated_client()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### 2.2. Initialize API client" + "### 2.2. List accounts" ] }, { @@ -154,26 +157,14 @@ "metadata": {}, "outputs": [], "source": [ - "from mat3ra.api_client import APIClient\n", - "\n", - "client = APIClient.authenticate()\n", - "client" + "client.list_accounts()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### 2.3. Select account" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "client.list_accounts()" + "### 2.3. Select account to work under" ] }, { diff --git a/utils/auth.py b/utils/auth.py index c36fa808..4eb12a5f 100644 --- a/utils/auth.py +++ b/utils/auth.py @@ -6,7 +6,7 @@ import requests from IPython.display import Javascript, display -from mat3ra.api_client import ACCESS_TOKEN_ENV_VAR, CLIENT_ID, SCOPE, APIEnv, build_oidc_base_url +from mat3ra.api_client import ACCESS_TOKEN_ENV_VAR, CLIENT_ID, SCOPE, APIClient, APIEnv, build_oidc_base_url REFRESH_TOKEN_ENV_VAR = "OIDC_REFRESH_TOKEN" @@ -141,3 +141,13 @@ async def authenticate(force=False): else: if ACCESS_TOKEN_ENV_VAR not in os.environ or force: await authenticate_oidc() + + +async def get_authenticated_client(access_token: Optional[str] = None) -> APIClient: + if access_token is not None: + os.environ[ACCESS_TOKEN_ENV_VAR] = access_token + else: + await authenticate() + if ACCESS_TOKEN_ENV_VAR in os.environ: + print(f"Access token (copy to reuse in case of notebook refresh):\n{os.environ[ACCESS_TOKEN_ENV_VAR]}") + return APIClient.authenticate()