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.
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:
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')
Launching notebook, assuming port=8888 Hello world notebook creation and launch took 0.005967 seconds
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
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')
Launching notebook, assuming port=8888 Simple notebook file loading took 0.008859 seconds
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"
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')
Launching notebook, assuming port=8888 Advanced file loading took 0.010221 seconds
Below, we create a full pygsti advanced report, mimicking the existing create_general_report
webpage
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')
Launching notebook, assuming port=8888 Full Advanced Report took 0.018344 seconds