{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Advanced Report Creation\n", "\n", "Pygsti can now create jupyter notebooks from python, using a custom `Notebook` object, which has convenience methods built around a `json` representation of a jupyter notebook." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hello World\n", "The simplest functions for manipulating the `Notebook` object are `add_markdown` and `add_code`, which can be used to put python strings into notebook cells:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Launching notebook, assuming port=8888\n", "Hello world notebook creation and launch took 0.005967 seconds\n", "\n" ] } ], "source": [ "from pygsti.report import Notebook\n", "from pygsti.tools import timed_block\n", "\n", "with timed_block('Hello world notebook creation and launch'):\n", " nb = Notebook()\n", " nb.add_markdown('''\n", "# Hello World Notebook\n", "This notebook prints \"Hello, World\":''')\n", " nb.add_code('''print('Hello, World')''')\n", " nb.launch('HelloWorld.ipynb')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## File loading\n", "However, hard-coding strings in python can be a pain, especially as the size of the report increases. \n", "Also, docstrings are easily messed up by indentation (see cell above). \n", "To get around this, code and markdown can also be loaded from file. For the purposes of this tutorial, two files have been created in this directory: `lorem.md` and `lorem.py`" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Launching notebook, assuming port=8888\n", "Simple notebook file loading took 0.008859 seconds\n", "\n" ] } ], "source": [ "with timed_block('Simple notebook file loading'):\n", " nb = Notebook()\n", " nb.add_markdown_file('templates/lorem.md')\n", " nb.add_code_file('templates/lorem.py')\n", " nb.launch('Lorem.ipynb')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Advanced File Loading\n", "`Notebook` objects can also load formatted text, or other notebooks.\n", "\"Formatted text\" is a file that contains both python code and markdown, separated by the special tags \"@@code\" and \"@@markdown\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Launching notebook, assuming port=8888\n", "Advanced file loading took 0.010221 seconds\n", "\n" ] } ], "source": [ "with timed_block('Advanced file loading'):\n", " nb = Notebook()\n", " nb.add_notebook_text_file('templates/nbtext.txt')\n", " nb.add_notebook_file('HelloWorld.ipynb')\n", " nb.launch('AdvancedFileLoading.ipynb')\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Full Advanced Report Creation\n", "Below, we create a full pygsti advanced report, mimicking the existing `create_general_report` webpage" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Launching notebook, assuming port=8888\n", "Full Advanced Report took 0.018344 seconds\n", "\n" ] } ], "source": [ "import time\n", "\n", "with timed_block('Full Advanced Report'):\n", " nb = Notebook()\n", " nb.add_markdown('# Pygsti report\\n(Created on {})'.format(time.strftime(\"%B %d, %Y\")))\n", " nb.add_code_file('templates/setup.py')\n", " nb.add_code_file('templates/workspace.py')\n", " nb.add_notebook_text_files([\n", " 'templates/summary.txt',\n", " 'templates/goodness.txt',\n", " 'templates/gauge_invariant.txt',\n", " 'templates/gauge_variant.txt',\n", " 'templates/data_comparison.txt',\n", " 'templates/input.txt',\n", " 'templates/meta.txt'])\n", " nb.launch('AdvancedReport.ipynb')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.5.2" } }, "nbformat": 4, "nbformat_minor": 2 }