<s> import io <EOL> import os <EOL> import argparse <EOL> from functools import partial <EOL> try : <EOL> from concurrent import futures <EOL> par = True <EOL> except ImportError : <EOL> par = False <EOL> import nbformat <EOL> from nbconvert import HTMLExporter , RSTExporter <EOL> from nbconvert . preprocessors import ExecutePreprocessor <EOL> EXAMPLE_DIR = os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , "<STR_LIT:..>" , <EOL> "<STR_LIT>" ) ) <EOL> SOURCE_DIR = os . path . join ( EXAMPLE_DIR , "<STR_LIT>" ) <EOL> EXECUTED_DIR = os . path . join ( EXAMPLE_DIR , "<STR_LIT>" ) <EOL> DST_DIR = os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , "<STR_LIT:..>" , <EOL> "<STR_LIT>" , "<STR_LIT:source>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> for dir in [ EXECUTED_DIR , DST_DIR ] : <EOL> if not os . path . exists ( dir ) : <EOL> os . makedirs ( dir ) <EOL> def execute_nb ( src , dst , allow_errors = False , timeout = <NUM_LIT:1000> , kernel_name = None ) : <EOL> '''<STR_LIT>''' <EOL> with io . open ( src , encoding = '<STR_LIT:utf-8>' ) as f : <EOL> nb = nbformat . read ( f , as_version = <NUM_LIT:4> ) <EOL> ep = ExecutePreprocessor ( allow_errors = allow_errors , <EOL> timeout = timeout , <EOL> kernel_name = kernel_name ) <EOL> ep . preprocess ( nb , { '<STR_LIT>' : { '<STR_LIT:path>' : '<STR_LIT>' } } ) <EOL> with io . open ( dst , '<STR_LIT>' , encoding = '<STR_LIT:utf-8>' ) as f : <EOL> nbformat . write ( nb , f ) <EOL> return dst <EOL> def convert ( src , dst , to = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> dispatch = { '<STR_LIT>' : RSTExporter , '<STR_LIT:html>' : HTMLExporter } <EOL> exporter = dispatch [ to . lower ( ) ] ( ) <EOL> ( body , resources ) = exporter . from_filename ( src ) <EOL> with io . open ( dst , '<STR_LIT>' , encoding = '<STR_LIT:utf-8>' ) as f : <EOL> f . write ( body ) <EOL> return dst <EOL> def find_notebooks ( directory = None ) : <EOL> if directory is None : <EOL> directory = SOURCE_DIR <EOL> nbs = ( os . path . join ( SOURCE_DIR , x ) <EOL> for x in os . listdir ( SOURCE_DIR ) <EOL> if x . endswith ( '<STR_LIT>' ) ) <EOL> return nbs <EOL> def do_one ( nb , to = None , execute = None , allow_errors = None , timeout = None , kernel_name = None ) : <EOL> name = os . path . basename ( nb ) <EOL> if execute : <EOL> dst = os . path . join ( EXECUTED_DIR , name ) <EOL> print ( "<STR_LIT>" % ( nb , dst ) ) <EOL> nb = execute_nb ( nb , dst , allow_errors = allow_errors , timeout = timeout , <EOL> kernel_name = kernel_name ) <EOL> dst = os . path . splitext ( os . path . join ( DST_DIR , name ) ) [ <NUM_LIT:0> ] + '<STR_LIT:.>' + to <EOL> print ( "<STR_LIT>" % ( nb , dst ) ) <EOL> convert ( nb , dst , to = to ) <EOL> return dst <EOL> def do ( fp = None , directory = None , to = '<STR_LIT:html>' , execute = True , <EOL> allow_errors = True , timeout = <NUM_LIT:1000> , kernel_name = None ) : <EOL> if fp is None : <EOL> nbs = find_notebooks ( directory ) <EOL> else : <EOL> nbs = [ fp ] <EOL> func = partial ( do_one , to = to , execute = execute , allow_errors = allow_errors , <EOL> timeout = timeout , kernel_name = kernel_name ) <EOL> if par : <EOL> with futures . ProcessPoolExecutor ( ) as pool : <EOL> for dst in pool . map ( func , nbs ) : <EOL> print ( "<STR_LIT>" % dst ) <EOL> else : <EOL> for nb in nbs : <EOL> func ( nb ) <EOL> print ( "<STR_LIT>" % nb ) <EOL> parser = argparse . ArgumentParser ( description = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , type = str , default = None , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , type = str , default = None , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , type = str , default = "<STR_LIT:html>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , type = bool , default = True , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , type = bool , default = True , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , type = int , default = <NUM_LIT:1000> , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , type = str , default = None , <EOL> help = "<STR_LIT>" ) <EOL> def main ( ) : <EOL> args = parser . parse_args ( ) <EOL> do ( fp = args . fp , directory = args . directory , to = args . to , execute = args . execute , <EOL> allow_errors = args . allow_errors , timeout = args . timeout , <EOL> kernel_name = args . kernel_name ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
