def export_to_scala(absolute_notebook_path): '''convert the notebook source to scala''' with warnings.catch_warnings(): warnings.filterwarnings('ignore', category=DeprecationWarning) exporter = TemplateExporter(extra_loaders=[FileSystemLoader(INSTALLDIR)], preprocessors=[ScalaAppPreprocessor]) exporter.template_file = 'scala_sparkapp' return exporter.from_file(absolute_notebook_path)
def __init__(self, template_file, strip_outputs=True, write_outputs=False, output_dir='./figures'): """template_file - location of jinja template to use for export strip_outputs - whether to remove output cells from the output """ filters = [ ('string2json', self.string2json), ('create_input_codeblock', self.create_input_codeblock), ('create_output_codeblock', self.create_output_codeblock), ('create_output_block', self.create_output_block), ('create_attributes', self.create_attributes), ('dequote', self.dequote), ('data2uri', self.data2uri) ] import jinja2 # need to create a jinja loader that looks in whatever # arbitrary path we have passed in for the template_file direct_loader = jinja2.FileSystemLoader(os.path.dirname(template_file)) self.exporter = TemplateExporter(extra_loaders=[direct_loader]) self.exporter.output_mimetype = 'text/markdown' self.exporter.file_extension = '.md' # have to register filters before setting template file for # ipython 3 compatibility for name, filter in filters: self.exporter.register_filter(name, filter) self.exporter.template_file = os.path.basename(template_file) logging.debug("Creating MarkdownWriter") logging.debug(("MarkdownWriter: template_file = %s" % template_file)) logging.debug(("MarkdownWriter.exporter.template_file = %s" % self.exporter.template_file)) logging.debug(("MarkdownWriter.exporter.filters = %s" % self.exporter.environment.filters.keys())) self.strip_outputs = strip_outputs self.write_outputs = write_outputs self.output_dir = output_dir
def embedIpynb(ipynbPath, cells=None): # 引数のipynbをmarkdownに変換して返す path = os.getcwd() + "/" + ipynbPath # root以下からのPathで指定 if os.path.exists(path): try: with codecs.open(path, 'r', 'utf-8') as f: lines = f.readlines() f = nbformat.reads("".join(lines), as_version=4) if cells: # 表示したいCellが指定されてる場合 showCells = cells else: # 指定がなければ全部表示 showCells = [ int(x['execution_count']) for x in f.cells if 'execution_count' in x and x['execution_count'] is not None ] buff = [] for i in f.cells: if 'execution_count' in i and i['execution_count']: if int(i['execution_count']) in showCells: buff.append(i) f.cells = buff exporter = TemplateExporter( template_file="template/embed_markdown.tpl") (body, reources) = exporter.from_notebook_node(f) buff = [" " + x for x in body.split("\n") if x != ""] text = [ f"!!! example \"\"", *buff, f" :fa-bookmark: [{os.path.basename(ipynbPath)}]({getGithubRoot()}/tree/master/{ipynbPath})" ] return "\n".join(text) except Exception as f: import traceback return f"ConvertEerror!! ->{ipynbPath}\n{traceback.format_exc()}" else: return f"Not Found -> {ipynbPath}"