{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Converting code to function\n", "\n", "MLRun annotations are used to identify the code that needs to be converted into an MLRun function.\n", "They provide non-intrusive hints which indicate which parts of your notebook should be considered as the code of the function." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Annotations start a code block using `# mlrun: start-code` and end a code block(s), with `# mlrun: end-code`.\n", "Use the `#mlrun: ignore` to exclude items from the code qualified annotations.\n", "Make sure that the annotations include anything required for the function to run." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# mlrun: start-code\n", "\n", "def sub_handler():\n", " return \"hello world\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `# mlrun: ignore` annotation enables you to exclude the cell from the function code." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# mlrun: ignore\n", "\n", "# the handler in the code section below will not call this sub_handler\n", "def sub_handler():\n", " return \"I will be ignored!\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def handler(context, event):\n", " return sub_handler()\n", "\n", "# mlrun: end-code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Convert the function with `mlrun.code_to_function` and run the handler. Notice the returned value under `results`." ] }, { "cell_type": "markdown", "source": [ "```{admonition} Note\n", "Make sure to save the notebook before running `mlrun.code_to_function` so that the lateset changes will be reflected in the function.\n", "```" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "> 2021-11-01 07:42:44,930 [info] starting run some-function-name uid=742e7d6e930c48f3a2f1d6175e971455 DB=http://mlrun-api:8080\n" ] }, { "data": { "text/html": [ "\n", "
project | \n", "uid | \n", "iter | \n", "start | \n", "state | \n", "name | \n", "labels | \n", "inputs | \n", "parameters | \n", "results | \n", "artifacts | \n", "
---|---|---|---|---|---|---|---|---|---|---|
default | \n", "\n", " | 0 | \n", "Nov 01 07:42:45 | \n", "completed | \n", "some-function-name | \n", "v3io_user=admin kind= owner=admin host=jupyter-8459699595-z544v | \n",
" \n", " | \n", " | return=hello world | \n",
" \n", " |
project | \n", "uid | \n", "iter | \n", "start | \n", "state | \n", "name | \n", "labels | \n", "inputs | \n", "parameters | \n", "results | \n", "artifacts | \n", "
---|---|---|---|---|---|---|---|---|---|---|
default | \n", "\n", " | 0 | \n", "Nov 01 07:42:54 | \n", "completed | \n", "my-function-name | \n", "v3io_user=admin kind= owner=admin host=jupyter-8459699595-z544v | \n",
" \n", " | \n", " | return=hello from my-function | \n",
" \n", " |
project | \n", "uid | \n", "iter | \n", "start | \n", "state | \n", "name | \n", "labels | \n", "inputs | \n", "parameters | \n", "results | \n", "artifacts | \n", "
---|---|---|---|---|---|---|---|---|---|---|
default | \n", "\n", " | 0 | \n", "Nov 01 07:43:05 | \n", "completed | \n", "multi-section-function-name | \n", "v3io_user=admin kind= owner=admin host=jupyter-8459699595-z544v | \n",
" \n", " | \n", " | return=hello from multi-section-function-name | \n",
" \n", " |
project | \n", "uid | \n", "iter | \n", "start | \n", "state | \n", "name | \n", "labels | \n", "inputs | \n", "parameters | \n", "results | \n", "artifacts | \n", "
---|---|---|---|---|---|---|---|---|---|---|
default | \n", "\n", " | 0 | \n", "Nov 01 07:43:14 | \n", "completed | \n", "part-cell-function | \n", "v3io_user=admin kind= owner=admin host=jupyter-8459699595-z544v | \n",
" \n", " | \n", " | return=hello from part-cell-function | \n",
" \n", " |