{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "markdown-1",
   "metadata": {},
   "source": [
    "# 5. Deploy, share and delete\n",
    "\n",
    "So far your model has run only here, for you. To let others call it from Blue\n",
    "View, you **deploy** it. The platform then checks it, keeps every version and\n",
    "runs it whenever someone calls it.\n",
    "\n",
    "This notebook deploys the model from notebook 2, `my_first_model`. Run that\n",
    "notebook first."
   ]
  },
  {
   "cell_type": "code",
   "id": "code-2",
   "metadata": {},
   "source": [
    "from bff import lab\n",
    "\n",
    "FOLDER = \"my_first_model\"\n",
    "lab.check(FOLDER)"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "markdown-3",
   "metadata": {},
   "source": [
    "## Check before you push\n",
    "\n",
    "`lab.check` does here what the platform will do on its own machines:\n",
    "\n",
    "- **imports**: the model uses only libraries the platform has;\n",
    "- **declaration**: every field is valid;\n",
    "- **examples**: each example runs twice and gives the same answer both times.\n",
    "\n",
    "The third check matters most. A model that answers the same question two ways\n",
    "cannot be trusted, so the platform refuses it. Randomness must come from\n",
    "`call.rng`, which is seeded the same way for the same call.\n",
    "\n",
    "## Push\n",
    "\n",
    "`lab.push` uploads the folder as a new **version**. Versions count from 1 and\n",
    "never change: a fix is a new version. `release=True` makes the version\n",
    "available to others as soon as the platform's checks pass."
   ]
  },
  {
   "cell_type": "code",
   "id": "code-4",
   "metadata": {},
   "source": [
    "lab.push(FOLDER, release=True)"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "markdown-5",
   "metadata": {},
   "source": [
    "It prints the model's address in the read API. There, with your token, you can:\n",
    "\n",
    "- see each version, its checks and its state (`GET …/versions`);\n",
    "- choose **who may call it**: people by name, every trader, or everyone\n",
    "  (`PUT …/access`);\n",
    "- see when it was called and how long it took (`GET …/runs`).\n",
    "\n",
    "A colleague you granted finds it among the functions they may call\n",
    "(`GET /v1/functions`), under your name. A screen calls it with its own period\n",
    "and its own moment as the cut-off. That is why notebook 3 insisted that a model answer any period.\n",
    "\n",
    "## Change it, and get it back\n",
    "\n",
    "Edit `main.py`, then push again: that makes version 2. To bring a version back\n",
    "into a folder, from any machine, use `lab.pull`:\n",
    "\n",
    "```python\n",
    "lab.pull(\"my_first_model\", kind=\"model\")\n",
    "```\n",
    "\n",
    "## Delete it\n",
    "\n",
    "Each person may keep 20 functions. When you no longer need one, delete it. The\n",
    "platform removes every version; charts that drew it say it is no longer\n",
    "available. The name becomes free again, but version numbers continue, so an old\n",
    "chart never draws new code by mistake. Deleting cannot be undone, which is why\n",
    "the line below is commented out:"
   ]
  },
  {
   "cell_type": "code",
   "id": "code-6",
   "metadata": {},
   "source": [
    "# lab.delete(\"my_first_model\", kind=\"model\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "markdown-7",
   "metadata": {},
   "source": [
    "The read API deletes one too: `DELETE /v1/functions/{you}/{name}`.\n",
    "\n",
    "## What next\n",
    "\n",
    "- Change `my_first_model` into the naive rule and push it as version 2.\n",
    "- Copy an example from `../examples/`, change it and deploy it under a name of\n",
    "  your own.\n",
    "- Read the guide in the platform's documentation, under Models and indicators. It covers\n",
    "  every rule, every error and every limit."
   ]
  }
 ],
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 }
}
