{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# Jupyter notebook utilities\n",
    "\n",
    "The `evo-sdk-common` package contains helpful widgets that make it easier to interact with Evo services\n",
    "in Jupyter notebooks. These widgets are built on top of the [`ipywidgets`](https://ipywidgets.readthedocs.io/en/stable/) package,\n",
    "which must be installed in order to use the widgets. The widgets are not required to use the `evo-sdk-common` package.\n",
    "\n",
    "To install all dependencies for the notebook utilities, install `evo-sdk-common` with the `notebooks` optional dependencies selected:\n",
    "\n",
    "```\n",
    "pip install evo-sdk-common[notebooks]\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1",
   "metadata": {},
   "source": [
    "## ServiceManagerWidget\n",
    "\n",
    "The `ServiceManagerWidget` provides authorization and discovery in one convenient widget. Options that have been selected are stored in a `.env` file in a cache directory that can be configured with the `cache_location` parameter.\n",
    "\n",
    "`ServiceManagerWidget.with_auth_code(...)` allows you to authenticate using the authorization code flow. This implementation will open a new browser tab to log in. The authorization code redirect will only work if the notebook is running on the user's local machine.\n",
    "\n",
    "**Important:** With this authorization implementation, your user token is stored in plain text in the `.env` file. It is recommended to keep this file secure and not share it with others.\n",
    "\n",
    "### Authorization Code flow\n",
    "\n",
    "Using the `ServiceManagerWidget` with the authorization code flow is simple:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from evo.notebooks import ServiceManagerWidget\n",
    "\n",
    "manager = await ServiceManagerWidget.with_auth_code(\n",
    "    client_id=\"your-client-id\",\n",
    "    redirect_url=\"http://localhost:3000/signin-callback\",\n",
    "    cache_location=\"./notebook-data/with-auth-code\",\n",
    ").login()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "Once you have selected a valid organization, hub, and workspace, you can use the service manager widget to create `APIConnector` and `Environment` instances. These instances can be used to interact with Evo services. It is not necessary to open `APIConnector` instances, because the underlying transport has already been opened by the `ServiceManagerWidget`. The `Environment` cache location is set to the same cache location as the `ServiceManagerWidget` by default."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from evo.common import BaseAPIClient\n",
    "\n",
    "client = manager.create_client(BaseAPIClient)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "You can access the notebook cache via the service manager widget."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "cache = manager.cache.get_location(manager.get_environment(), \"<scope>\")\n",
    "print(f\"{cache}: {cache.exists() = }\")\n",
    "\n",
    "manager.cache.clear_cache(manager.get_environment(), \"<scope>\")\n",
    "print(f\"{cache.exists() = }\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7",
   "metadata": {},
   "source": [
    "## FeedbackWidget\n",
    "\n",
    "A `FeedbackWidget` is provided to display feedback a progress bar and messages in the notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "from evo.notebooks import FeedbackWidget\n",
    "\n",
    "fb = FeedbackWidget(\"Loading...\")\n",
    "\n",
    "for i in range(100):\n",
    "    n = round(i * 0.01, 2)\n",
    "    if round(n, 1) == n:\n",
    "        fb.progress(n, f\"{n * 100:.0f}%\")\n",
    "    else:\n",
    "        fb.progress(n)\n",
    "    time.sleep(0.04)\n",
    "\n",
    "fb.progress(1, \"Done\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
