{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Simple notebook pipeline \n",
    "\n",
    "Welcome to your first steps with Kubeflow Pipelines (KFP). This notebook demos: \n",
    "\n",
    "* Defining a Kubeflow pipeline with the KFP SDK\n",
    "* Creating an experiment and submitting pipelines to the KFP run time environment using the KFP SDK \n",
    "\n",
    "Reference documentation: \n",
    "* https://www.kubeflow.org/docs/pipelines/sdk/sdk-overview/\n",
    "* https://www.kubeflow.org/docs/pipelines/sdk/build-component/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Prerequisites: Install or update the pipelines SDK\n",
    "\n",
    "You may need to **restart your notebook kernel** after updating the KFP sdk.\n",
    "\n",
    "This notebook is intended to be run from a Kubeflow notebook server. (From other environments, you would need to pass different arguments to the `kfp.Client` constructor.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# You may need to restart your notebook kernel after updating the kfp sdk\n",
    "!python3 -m pip install kfp --upgrade --user"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Setup\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "EXPERIMENT_NAME = 'Simple notebook pipeline'        # Name of the experiment in the UI\n",
    "BASE_IMAGE = 'tensorflow/tensorflow:2.0.0b0-py3'    # Base image used for components in the pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import kfp\n",
    "import kfp.dsl as dsl\n",
    "from kfp import compiler\n",
    "from kfp import components"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create pipeline component"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Create a python function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@dsl.python_component(\n",
    "    name='add_op',\n",
    "    description='adds two numbers',\n",
    "    base_image=BASE_IMAGE  # you can define the base image here, or when you build in the next step. \n",
    ")\n",
    "def add(a: float, b: float) -> float:\n",
    "    '''Calculates sum of two arguments'''\n",
    "    print(a, '+', b, '=', a + b)\n",
    "    return a + b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Build a pipeline component from the function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Convert the function to a pipeline operation.\n",
    "add_op = components.func_to_container_op(\n",
    "    add,\n",
    "    base_image=BASE_IMAGE, \n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Build a pipeline using the component"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@dsl.pipeline(\n",
    "   name='Calculation pipeline',\n",
    "   description='A toy pipeline that performs arithmetic calculations.'\n",
    ")\n",
    "def calc_pipeline(\n",
    "   a='0',\n",
    "   b='7',\n",
    "   c='17',\n",
    "):\n",
    "    #Passing pipeline parameter and a constant value as operation arguments\n",
    "    add_task = add_op(a, 4) #Returns a dsl.ContainerOp class instance. \n",
    "    \n",
    "    #You can create explicit dependency between the tasks using xyz_task.after(abc_task)\n",
    "    add_2_task = add_op(a, b)\n",
    "    \n",
    "    add_3_task = add_op(add_task.output, add_2_task.output)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Compile and run the pipeline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Kubeflow Pipelines lets you group pipeline runs by *Experiments*. You can create a new experiment, or call `kfp.Client().list_experiments()` to see existing ones.\n",
    "If you don't specify the experiment name, the `Default` experiment will be used.\n",
    "\n",
    "You can directly run a pipeline given its function definition:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Specify pipeline argument values\n",
    "arguments = {'a': '7', 'b': '8'}\n",
    "# Launch a pipeline run given the pipeline function definition\n",
    "kfp.Client().create_run_from_pipeline_func(calc_pipeline, arguments=arguments, \n",
    "                                           experiment_name=EXPERIMENT_NAME)\n",
    "# The generated links below lead to the Experiment page and the pipeline run details page, respectively"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Alternately, you can separately compile the pipeline and then upload and run it as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Compile the pipeline\n",
    "pipeline_func = calc_pipeline\n",
    "pipeline_filename = pipeline_func.__name__ + '.pipeline.zip'\n",
    "compiler.Compiler().compile(pipeline_func, pipeline_filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get or create an experiment\n",
    "client = kfp.Client()\n",
    "experiment = client.create_experiment(EXPERIMENT_NAME)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Submit the compiled pipeline for execution:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Specify pipeline argument values\n",
    "arguments = {'a': '7', 'b': '8'}\n",
    "\n",
    "# Submit a pipeline run\n",
    "run_name = pipeline_func.__name__ + ' run'\n",
    "run_result = client.run_pipeline(experiment.id, run_name, pipeline_filename, arguments)\n",
    "\n",
    "# The generated link below leads to the pipeline run information page."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### That's it!\n",
    "You just created and deployed your first pipeline in Kubeflow!  You can put more complex python code within the functions, and you can import any libraries that are included in the base image (you can use [VersionedDependencies](https://kubeflow-pipelines.readthedocs.io/en/latest/source/kfp.compiler.html#kfp.compiler.VersionedDependency) to import libraries not included in the base image). "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----\n",
    "Copyright 2019 Google Inc. All Rights Reserved.\n",
    "\n",
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
