This worksheet exports other worksheets to .sage files in order to save time when executing every particular worksheet. It can also be used to reproduce all computational results with one click, just click on Cell -> Run All.
This converts all other worksheets to .sage files and them loads them into the current worksheet, which executes all the code in each worksheet. This can take tens of minutes!
In [1]:
def fun_export_load_ipynb(list_wsnames, folder, load1 = True):
'''
Exports worksheet 'name' to .sage file in specified folder and then imports it
into current worksheet, executing all code and displaying its output.
If you only want to export but not load the files, set option load1 = False.
'''
for name in list_wsnames:
fun_export_ipynb(name, folder)
print 'created file ' + folder + name
if load1:
load(folder + name + '.sage')
print 'imported file ' + folder + name
import json # for reading notebook metadata
def fun_export_ipynb(worksheet, folder):
'''
Exports worksheet.ipynb to a .py or .sage depending on kernel.
Example:
fun_export_ipynb('Worksheet_setup', 'temp/')
Need to import json before using this function. Unless option output=True is given,
each line of text has a semicolon added in the .py file, preventing any output.
'''
str1 = 'jupyter nbconvert --to=python \'' + worksheet+'.ipynb\''
print str1
print 'Exporting specified worksheet to .py file...'
try:
retcode = os.system(str1)
if retcode < 0:
print >>sys.stderr, "nbconvert was terminated by signal", -retcode
else:
print >>sys.stderr, "nbconvert returned", retcode
except OSError as e:
print >>sys.stderr, "Execution failed:", e
print >>sys.stderr, "Trying ipython nbconvert instead..."
str1 = 'ipython nbconvert --to script \'' + worksheet+'.ipynb\'' # for new version of python
try:
retcode = os.system(str1)
if retcode < 0:
print >>sys.stderr, "nbconvert was terminated by signal", -retcode
else:
print >>sys.stderr, "nbconvert returned", retcode
except OSError as e:
print >>sys.stderr, "Execution failed:", e
str1 = worksheet + '.py'
print 'Checking if specified ipynb file was run with sage kernel...'
with open(worksheet+'.ipynb') as data_file:
data = json.load(data_file)
if data['metadata']['kernelspec']['name'][0:4] == 'sage':
print 'Renaming .py file to .sage if notebook kernel was sage (to avoid exponent error)'
str2 = folder + worksheet + '.sage'
os.rename(str1, str2)
In [2]:
def fun_export_load_ipynb(list_wsnames, folder, load1 = True):
'''
Exports worksheet 'name' to .sage file in specified folder and then imports it
into current worksheet, executing all code and displaying its output.
If you only want to export but not load the files, set option load1 = False.
'''
for name in list_wsnames:
fun_export_ipynb(name, folder)
print 'created file ' + folder + name
if load1:
load(folder + name + '.sage')
print 'imported file ' + folder + name
In [3]:
fun_export_load_ipynb(['Worksheet_setup'], 'temp/', load1 = True)
figuresBelow, list_wsnames contains a list of worksheet names that need to be executed in the specified order to recreate all figures in the directory /figures. This will take a few minutes. If you only want to export the worksheets as .sage files to the temp folder, just change load1 = True to load1 = False.
In [4]:
list_wsnames = ['leaf_enbalance_eqs', 'E_PM_eqs', 'stomatal_cond_eqs', 'leaf_chamber_eqs', 'leaf_chamber_data', 'Tables_of_variables' ]
folder = 'temp/'
fun_export_load_ipynb(list_wsnames, folder, load)
In [ ]: