Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _tst_notebook(self, notebook_name):
notebook_filename = os.path.abspath(os.path.join(
self.this_file_directory, '..', '..', 'examples', notebook_name))
with open(notebook_filename) as f:
nb = nbformat.read(f, as_version=4)
python_nb, metadata = export(PythonExporter, nb)
# Remove magic lines manually
python_nb = '\n'.join([
line for line in python_nb.split('\n')
if 'get_ipython().run_line_magic(' not in line
])
exec(python_nb)
def nb_to_python(nb_path):
"""convert notebook to python script"""
exporter = python.PythonExporter()
output, resources = exporter.from_filename(nb_path)
return output
def compile(Compiler, ast):
"""Compile AST to bytecode using the an IPython compiler."""
return (Compiler.ip and Compiler.ip.compile or CachingCompiler())(ast, Compiler.filename, 'exec')
def ast_parse(Compiler, source, filename='', symbol='exec', lineno=0):
return ast.increment_lineno(super().ast_parse(source, Compiler.filename, 'exec'), lineno)
# In[4]:
if __name__ == '__main__':
from pathlib import Path
Path('../importnb/compiler_ipython.py').write_text(PythonExporter().from_filename('compiler_ipython.ipynb')[0])
__import__('doctest').testmod()
return Compiler.ip.input_transformer_manager.transform_cell
def compile(Compiler, ast):
"""Compile AST to bytecode using the an IPython compiler."""
return (Compiler.ip and Compiler.ip.compile or CachingCompiler())(
ast, Compiler.filename, "exec"
)
def ast_parse(Compiler, source, filename="", symbol="exec", lineno=0):
return ast.increment_lineno(super().ast_parse(source, Compiler.filename, "exec"), lineno)
Compiler = IpythonCompiler
class IpythonPythonExporter(_PythonExporter):
def from_file(self, file_stream, resources=None, **kw):
return self.from_notebook_node(from_dict(load(file_stream)), resources, **kw)
PythonExporter = IpythonPythonExporter
if __name__ == "__main__":
try:
from .compile import export
except:
from compile import export
export("compile_ipython.ipynb", "../importnb/compile_ipython.py")
__import__("doctest").testmod()
def convert_notebook(self, name):
"""Converts a notebook into a python file."""
exporter = nbconvert.exporters.python.PythonExporter()
relative_path = self.convert_path(name)
file_path = self.get_path("%s.ipynb" % relative_path)
code = exporter.from_filename(file_path)[0]
self.write_code(name, code)
self.clean_code(name, [])