Automatic execution of code blocks


Sage interactive
In [1]:

Compile this document with

In [2]:
doconce format ipynb execute.do.txt --execute
  File "", line 1
    doconce format ipynb execute.do.txt --execute
                 ^
SyntaxError: invalid syntax

This is a normal python block using the pycod environment
In [3]:
print('pycod')
pycod

Hidden execution cells (pyhid, pycod-e) can be used to perform operations (e.g. imports, variable initializations) without showing any cell. The pyhid environment executes and hides the cell in formats other than .ipynb:

The pycod-e environment executes but hides the cell also in .ipynb files:

pycod is a normal cell that should execute automatically when using --execute. Note that this cells relies on code executed in a previous hidden cell:
In [4]:
print(sys.version)
b = 2
c = a + b
print("The result is {}".format(c))
c
3.6.9 (default, Oct  8 2020, 12:12:24) 
[GCC 8.4.0]
The result is 3
3

The *-t environment (e.g. pycod-t) formats a cell to text, and can be used to print an example
In [5]:
# This is a for-loop example
for i in [0,10]:
  print(i)
0
10

The *out (e.g. pycod-out) environment can be used to write a cell output:
In [6]:
# This is a normal pycod cell
1/0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
 in 
      1 # This is a normal pycod cell
----> 2 1/0

ZeroDivisionError: division by zero

In [7]:
1/0: You cannot divide by zero

The -h postfix can be used in the html format to show a Show/Hide button that toggles the code visibility.
In [8]:

Plotting

This is a cell that should plot and output:

In [9]:
from pylab import *
x = linspace(0, 10, 100)
plot(x, x*x)
show()

PDF cannot be displayed in your browser. See .doconce_figure_cache/d0e63083-ad2f-4e76-a1f6-641d581260ff.pdf

To improve quality when exporting to LaTeX, the following code has automatically been run to enable PDF export in notebooks.

In [10]:
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('png', 'pdf')

Ignore output

Predefined output can be omitted by passing --ignore_output to DocOnce. This will remove all environments ending with out.

In [11]:
a = 2
print(a)
2

In [12]:
2

Code with errors

If code contains errors, it will still be run and the exception shown as part of the output:

In [13]:
for a in range(10)
    print(a)
  File "", line 1
    for a in range(10)
                      ^
SyntaxError: invalid syntax

Opening files

The working directory is the same as the .do.txt file. You may want to use os.chdir to change the directory.

In [14]:
with open("../LICENSE") as f:
    print(f.read())
Copyright (c) 2007-2015, Hans Petter Langtangen  and
Simula Resarch Laboratory.

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the
      distribution.

    * Neither the name of Simula Research Laboratory nor the names of
      its contributors may be used to endorse or promote products
      derived from this software without specific prior written
      permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Remarks:

The figure and movie files in doc/manual/* were made by the Doconce
author and is released under the same conditions as Doconce.