def test_notebook_writer_hello(): path = get_test_file_path('ast', 'hello.json') ast = ASTPlugin().load(path) nb = NotebookWriter().write(ast) # Compare the notebooks. nb_expected = open_notebook(get_test_file_path('notebook', 'hello.ipynb')) # Ignore some fields when comparing the notebooks. NotebookPlugin().assert_equal(nb, nb_expected)
def test_notebook_reader_notebook(): # Open a test notebook with a code cell. path = get_test_file_path('notebook', 'notebook.ipynb') notebook = open_notebook(path) # Convert it to an AST. reader = NotebookReader() ast = reader.read(notebook) ast.show() # Compare with the markdown version. path = get_test_file_path('markdown', 'notebook.md') markdown = load_text(path) assert_equal(MarkdownPlugin().write(ast), markdown) assert 'output_4_1.png' in reader.resources
def test_extract_output(): # Open a test notebook with a code cell containing an image. path = get_test_file_path('notebook', 'notebook.ipynb') notebook = open_notebook(path) cell = notebook.cells[4] mime_type, data = list(extract_output(cell.outputs[1])) filename = output_filename(mime_type, cell_index=4, output_index=1) assert filename == 'output_4_1.png' # Open the image file in the markdown directory. image_path = get_test_file_path('markdown', filename) with open(image_path, 'rb') as f: data_expected = f.read() # The two image contents should be identical. assert data == data_expected
def test_notebook_writer_notebook(): path = get_test_file_path('ast', 'notebook.json') ast = ASTPlugin().load(path) # Load the image. fn = get_test_file_path('markdown', 'output_4_1.png') with open(fn, 'rb') as f: img = f.read() resources = {op.basename(fn): img} # Convert the AST to a notebook. nb = NotebookWriter().write(ast, resources=resources) # Compare the notebooks. nb_expected = open_notebook(get_test_file_path('notebook', 'notebook.ipynb')) # Ignore some fields when comparing the notebooks. NotebookPlugin().assert_equal(nb, nb_expected)
def test_notebook_reader_hello(): # Open a test notebook with just 1 Markdown cell. path = get_test_file_path('notebook', 'hello.ipynb') notebook = open_notebook(path) # Convert it to an AST. ast = NotebookReader().read(notebook) ast.show() # Check that the AST is equal to the one of a simple Mardown line. ast_1 = MarkdownPlugin().read('hello *world*') assert ast == ast_1
def test_notebook_writer_notebook(): path = get_test_file_path('ast', 'simplenb.json') ast = ASTPlugin().load(path) # TODO: save resource files in JSON serializer # Load the image. fn = get_test_file_path('markdown', 'simplenb_files/simplenb_4_1.png') with open(fn, 'rb') as f: img = f.read() # Convert the AST to a notebook. nb = NotebookWriter().write(ast, context={ 'resources': { op.basename(fn): img }, 'path': path }) # Compare the notebooks. nb_expected = open_notebook( get_test_file_path('notebook', 'simplenb.ipynb')) # Ignore some fields when comparing the notebooks. assert nb == nb_expected
def test_output_text(podoc): img_path = get_test_file_path('markdown', 'simplenb_files/simplenb_4_1.png') markdown = dedent(''' ```python print("hello") ``` ![Some text](%s) ''' % img_path) nb = podoc.convert_text(markdown, source='markdown', target='notebook') nb.cells[0].outputs[0].data['text/plain'] = 'Replaced text' md = podoc.convert_text(nb, source='notebook', target='markdown') assert 'Replaced' not in md assert 'Some text' in md