From 361b997d6244220d05522d1f3c094ddc7687f16e Mon Sep 17 00:00:00 2001 From: Adam Gabbert Date: Thu, 25 Mar 2021 12:08:21 -0500 Subject: [PATCH] Add example. --- jupyter/Scale Output Example.ipynb | 182 +++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 jupyter/Scale Output Example.ipynb diff --git a/jupyter/Scale Output Example.ipynb b/jupyter/Scale Output Example.ipynb new file mode 100644 index 0000000..2d53f4e --- /dev/null +++ b/jupyter/Scale Output Example.ipynb @@ -0,0 +1,182 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Simple Scalar Output Example\n", + "\n", + "In this example, we will define an integer parameter called `Integer input` and will output a floating point called `My scalar output` that can be bound to a numeric dashboard tile." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import scrapbook as sb \n", + "from random import *" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Parameters\n", + "\n", + "The parameters block is where you define all of the inputs and outputs to the notebook. If you look at __Property Inspector >> Advanced Tools >> Cell Metadata__, while the `# Parameters` code cell block is selected, you will see the json that describes the inputs and outputs. Note that when you edit that json, you must click the small check mark under \"Cell Metadata\" to save the changes. In this example you can see we have configured a numeric input and a numeric output." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "papermill": { + "parameters": { + "numeric_param": 1 + } + }, + "systemlink": { + "namespaces": [ + "ni-testmanagement" + ], + "outputs": [ + { + "display_name": "Input plus fraction", + "id": "scalar_output", + "type": "scalar" + } + ], + "parameters": [ + { + "display_name": "Number", + "id": "numeric_param", + "type": "number" + } + ], + "version": 2 + }, + "tags": [ + "parameters" + ] + }, + "outputs": [], + "source": [ + "# Parameters\n", + "numeric_param = 1 # default value used when input is not provided by Dashboard\n", + "\n", + "result = [] # container for all of the notebooks results" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "output = int(numeric_param) + uniform(0,1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Results\n", + "\n", + "Scalar results are appended with a json map that corresponds to the json in the cell metadata described above. The \"type' and 'id' fields should match the cell metadata values, and the 'value' field should contain the scalar result. The config block doesn't correspond to anything in the cell metadata." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "# Scalar result needs to be added to the result list and glued to the scrapbook\n", + "# The json descriptor should match the descriptor in the outputs section in the parameters cell metadata.\n", + "result.append({\n", + " 'type': 'scalar',\n", + " 'id': 'scalar_output',\n", + " 'config': {\n", + " 'title': 'Scalar Output Title'\n", + " },\n", + " 'value': output\n", + "})" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "application/scrapbook.scrap.json+json": { + "data": [ + { + "config": { + "title": "Scalar Output Title" + }, + "id": "scalar_output", + "type": "scalar", + "value": 1.322663202537357 + } + ], + "encoder": "json", + "name": "result", + "version": 1 + } + }, + "metadata": { + "scrapbook": { + "data": true, + "display": false, + "name": "result" + } + }, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "[{'type': 'scalar',\n", + " 'id': 'scalar_output',\n", + " 'config': {'title': 'Scalar Output Title'},\n", + " 'value': 1.322663202537357}]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Gluing the result list to the scrapbook is what actually allows you to bind the result to the numeric Dashboard tile.\n", + "sb.glue('result', result)\n", + "result # this line is just here so you can see the result in Jupyter Hub and doesn't impact Dashboard." + ] + } + ], + "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.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} \ No newline at end of file