#!/usr/bin/env python # coding: utf-8 # # Advanced Report Creation # # 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. # ## Hello World # 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: # In[1]: from pygsti.report import Notebook from pygsti.tools import timed_block with timed_block('Hello world notebook creation and launch'): nb = Notebook() nb.add_markdown(''' # Hello World Notebook This notebook prints "Hello, World":''') nb.add_code('''print('Hello, World')''') nb.launch('HelloWorld.ipynb') # ## File loading # However, hard-coding strings in python can be a pain, especially as the size of the report increases. # Also, docstrings are easily messed up by indentation (see cell above). # 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` # In[2]: with timed_block('Simple notebook file loading'): nb = Notebook() nb.add_markdown_file('templates/lorem.md') nb.add_code_file('templates/lorem.py') nb.launch('Lorem.ipynb') # ## Advanced File Loading # `Notebook` objects can also load formatted text, or other notebooks. # "Formatted text" is a file that contains both python code and markdown, separated by the special tags "@@code" and "@@markdown" # In[3]: with timed_block('Advanced file loading'): nb = Notebook() nb.add_notebook_text_file('templates/nbtext.txt') nb.add_notebook_file('HelloWorld.ipynb') nb.launch('AdvancedFileLoading.ipynb') # ## Full Advanced Report Creation # Below, we create a full pygsti advanced report, mimicking the existing `create_general_report` webpage # In[4]: import time with timed_block('Full Advanced Report'): nb = Notebook() nb.add_markdown('# Pygsti report\n(Created on {})'.format(time.strftime("%B %d, %Y"))) nb.add_code_file('templates/setup.py') nb.add_code_file('templates/workspace.py') nb.add_notebook_text_files([ 'templates/summary.txt', 'templates/goodness.txt', 'templates/gauge_invariant.txt', 'templates/gauge_variant.txt', 'templates/data_comparison.txt', 'templates/input.txt', 'templates/meta.txt']) nb.launch('AdvancedReport.ipynb') # In[ ]: