{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "## Kale base example\n",
    "\n",
    "This notebook presents a minimal example to show how Kale can convert a Notebook to a pipeline applying Notebook annotations.\n",
    "\n",
    "Variables that are constant throughout the Notebook are transformed into pipeline parameters, so that the pipeline can be re-run with different initializations. Code cells are divided into pipeline steps by mean of notebook tags. You can look at the annotations by inspecting the Notebook source, or by installing the Kale jupyter extension (see [github.com/kubeflow-kale/kale](https://github.com/kubeflow-kale/kale))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "imports"
    ]
   },
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "pipeline-parameters"
    ]
   },
   "outputs": [],
   "source": [
    "CANDIES = 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "functions"
    ]
   },
   "outputs": [],
   "source": [
    "def get_handful(left):\n",
    "    if left == 0:\n",
    "        print(\"There are no candies left! I want to cry :(\")\n",
    "        return 0\n",
    "    c = random.randint(1, left)\n",
    "    print(\"I got %s candies!\" % c)\n",
    "    return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "step:sack"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Let's put in a bag 20 candies and have three kids get a handful of them each\n"
     ]
    }
   ],
   "source": [
    "print(\"Let's put in a bag %s candies and have three kids get a handful of them each\" % CANDIES)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "prev:sack",
     "step:kid1"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I got 4 candies!\n"
     ]
    }
   ],
   "source": [
    "# kid1 gets a handful, without looking in the bag!\n",
    "kid1 = get_handful(CANDIES)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "prev:kid1",
     "step:kid2"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I got 3 candies!\n"
     ]
    }
   ],
   "source": [
    "kid2 = get_handful(CANDIES - kid1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "prev:kid2",
     "step:kid3",
     "prev:kid1"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I got 2 candies!\n"
     ]
    }
   ],
   "source": [
    "kid3 = get_handful(CANDIES - kid1 - kid2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "tags": [
     "pipeline-metrics"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "3\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "print(kid1)\n",
    "print(kid2)\n",
    "print(kid3)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "kubeflow_notebook": {
   "base_image": "",
   "experiment": {
    "id": "",
    "name": ""
   },
   "experiment_name": "",
   "pipeline_description": "Share some candies between three lovely kids",
   "pipeline_name": "candies-sharing",
   "volumes": []
  },
  "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.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
