Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
class PyNotebookExporter(Exporter):
"""
Exports to a python notebook (.py)
"""
@default('file_extension')
def _file_extension_default(self):
return '.py'
def from_notebook_node(self, nb, resources=None, **kw):
resources = resources or {}
resources['output_extension'] = self.file_extension
return jupytext.writes(nb, ext='.py'), resources
class JlNotebookExporter(Exporter):
"""
Exports to a julia notebook (.jl)
"""
@default('file_extension')
def _file_extension_default(self):
return '.jl'
def from_notebook_node(self, nb, resources=None, **kw):
resources = resources or {}
resources['output_extension'] = self.file_extension
return jupytext.writes(nb, ext='.jl'), resources
class RNotebookExporter(Exporter):
"""
description='''Export Jupyter notebook with a SoS kernel to a
.sos file. The cells are presented in the .sos file as
cell structure lines, which will be ignored if executed
in batch mode ''')
parser.add_argument('-a', '--all', action='store_true', dest='__all__',
help='''By default sos only export workflows from an .ipynb file, which consists
of only cells that starts with section headers (ignoring comments and magics before
them). Option `-a` allows you to export cell separator, meta data, execution count,
and all cells in a sos-like format although the resulting .sos file might not be
able to be executed in batch mode.''')
return parser
# This class cannot be defined in .kernel because it would cause some
# weird problem with unittesting not able to resolve __main__
class SoS_Exporter(Exporter):
def __init__(self, config=None, export_all=False, **kwargs):
self.output_extension = '.sos'
self.output_mimetype = 'text/x-sos'
self.export_all = export_all
Exporter.__init__(self, config, **kwargs)
def from_notebook_cell(self, cell, fh, idx = 0):
if self.export_all:
meta = ' '.join('{}={}'.format(x,y) for x,y in cell.metadata.items())
if not hasattr(cell, 'execution_count') or cell.execution_count is None:
fh.write('%cell {} {}\n'.format(cell.cell_type, meta))
else:
idx += 1
fh.write('%cell {} {} {}\n'.format(cell.cell_type, cell.execution_count, meta))
if cell.cell_type == 'code':
fh.write(cell.source.strip() + '\n')
import os
import datetime
def write_files(self, resources):
self.log.info('Base dir: {}'.format(self.output_dir))
for filename, data in resources.get('outputs', {}).items():
dest = join(self.output_dir, filename)
ensure_dir(dest)
with io.open(dest, 'wb') as f:
f.write(data)
self.log.info("Wrote '{}' ".format(filename))
class MetatabExporter(Exporter):
template_path = List(['.']).tag(config=True, affects_environment=True)
output_dir = Unicode(help='Output directory').tag(config=True)
notebook_dir = Unicode(help='CWD in which notebook will be executed').tag(config=True)
package_dir = Unicode(help='Directory in which to store generated package').tag(config=True)
package_name = Unicode(help='Name of package to generate. Defaults to the Metatab Root.Name').tag(config=True)
def __init__(self, config=None, **kw):
# import pdb; pdb.set_trace();
super().__init__(config, **kw)
self.log = kw.get('log', logging.getLogger(self.__class__.__name__))
def from_file(self, file_stream, resources=None, **kw):
"""
R markdown exporter for nbconvert
"""
from traitlets import default
from nbconvert.exporters import Exporter
import jupytext
class RMarkdownExporter(Exporter):
"""
Exports to a R markdown document (.Rmd)
"""
@default('file_extension')
def _file_extension_default(self):
return '.Rmd'
def from_notebook_node(self, nb, resources=None, **kw):
resources = resources or {}
resources['output_extension'] = self.file_extension
return jupytext.writes(nb, ext='.Rmd'), resources
'Content-Disposition', f'attachment; filename="{os.path.basename(notebook_name)}.docx"',
)
handler.set_header(
'Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
)
# send content to handler
yield handler.write(
converters.notebookcontent_to_docxbytes(model['content'], notebook_filename, notebook_path)
)
# Return the buffer value as the response
handler.finish()
class DocxExporter(Exporter):
"""Convert a notebook to docx
This is the API which nbconvert calls.
"""
output_mimetype = 'application/docx'
def _file_extension_default(self):
return '.docx'
def from_notebook_node(self, nb, resources=None, **kw):
nb_copy, resources = super().from_notebook_node(nb, resources)
return (
converters.notebookcontent_to_docxbytes(
nb_copy, resources['metadata']['name'], resources['metadata']['path'],
),
def __init__(self, config=None, **kwargs):
self.output_extension = '.sos'
self.output_mimetype = 'text/x-sos'
Exporter.__init__(self, config, **kwargs)
class JlNotebookExporter(Exporter):
"""
Exports to a julia notebook (.jl)
"""
@default('file_extension')
def _file_extension_default(self):
return '.jl'
def from_notebook_node(self, nb, resources=None, **kw):
resources = resources or {}
resources['output_extension'] = self.file_extension
return jupytext.writes(nb, ext='.jl'), resources
class RNotebookExporter(Exporter):
"""
Exports to a R notebook (.R)
"""
@default('file_extension')
def _file_extension_default(self):
return '.R'
def from_notebook_node(self, nb, resources=None, **kw):
resources = resources or {}
resources['output_extension'] = self.file_extension
return jupytext.writes(nb, ext='.R'), resources