diff --git a/jupytext/jupytext.py b/jupytext/jupytext.py index 54a501712..bb51ce565 100644 --- a/jupytext/jupytext.py +++ b/jupytext/jupytext.py @@ -13,7 +13,7 @@ import io from copy import deepcopy from nbformat.v4.rwbase import NotebookReader, NotebookWriter -from nbformat.v4.nbbase import new_notebook +from nbformat.v4.nbbase import new_notebook, new_code_cell import nbformat from .formats import get_format, guess_format, \ update_jupytext_formats_metadata, format_name_for_ext @@ -43,6 +43,10 @@ def reads(self, s, **_): lines = lines[pos:] + if self.format.format_name and \ + self.format.format_name.startswith('sphinx'): + cells.append(new_code_cell(source='%matplotlib inline')) + while lines: reader = self.format.cell_reader_class(self.format.extension) cell, pos = reader.read(lines) @@ -89,6 +93,11 @@ def writes(self, nb, **kwargs): text = cell.simplify_code_markers( texts[i], texts[i + 1] if i + 1 < len(texts) else None, lines) + if i == 0 and self.format.format_name and \ + self.format.format_name.startswith('sphinx') and \ + text == ['%matplotlib inline']: + continue + lines.extend(text) lines.extend([''] * cell.lines_to_next_cell) diff --git a/tests/notebooks/mirror/sphinx-rst2md_to_ipynb/plot_notebook.ipynb b/tests/notebooks/mirror/sphinx-rst2md_to_ipynb/plot_notebook.ipynb index d2fff2e8b..9cd3ae6b4 100644 --- a/tests/notebooks/mirror/sphinx-rst2md_to_ipynb/plot_notebook.ipynb +++ b/tests/notebooks/mirror/sphinx-rst2md_to_ipynb/plot_notebook.ipynb @@ -1,5 +1,14 @@ { "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%matplotlib inline" + ] + }, { "cell_type": "markdown", "metadata": { diff --git a/tests/notebooks/mirror/sphinx_to_ipynb/plot_notebook.ipynb b/tests/notebooks/mirror/sphinx_to_ipynb/plot_notebook.ipynb index 750672551..957774cfa 100644 --- a/tests/notebooks/mirror/sphinx_to_ipynb/plot_notebook.ipynb +++ b/tests/notebooks/mirror/sphinx_to_ipynb/plot_notebook.ipynb @@ -1,5 +1,14 @@ { "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%matplotlib inline" + ] + }, { "cell_type": "markdown", "metadata": { diff --git a/tests/test_read_simple_sphinx.py b/tests/test_read_simple_sphinx.py index 451ce302b..114860238 100644 --- a/tests/test_read_simple_sphinx.py +++ b/tests/test_read_simple_sphinx.py @@ -25,27 +25,30 @@ def f(x): return 4 """): nb = jupytext.reads(script, ext='.py', format_name='sphinx') - assert nb.cells[0].cell_type == 'markdown' - assert nb.cells[0].source == 'This is a markdown cell' + assert nb.cells[0].cell_type == 'code' + assert nb.cells[0].source == '%matplotlib inline' - assert nb.cells[1].cell_type == 'code' - compare(nb.cells[1].source, '''1 + 2 + 3 + 4 + assert nb.cells[1].cell_type == 'markdown' + assert nb.cells[1].source == 'This is a markdown cell' + + assert nb.cells[2].cell_type == 'code' + compare(nb.cells[2].source, '''1 + 2 + 3 + 4 5 6''') - assert nb.cells[2].cell_type == 'markdown' - assert nb.cells[2].source == '' + assert nb.cells[3].cell_type == 'markdown' + assert nb.cells[3].source == '' - assert nb.cells[3].cell_type == 'code' - assert nb.cells[3].source == '7' + assert nb.cells[4].cell_type == 'code' + assert nb.cells[4].source == '7' - assert nb.cells[4].cell_type == 'markdown' - assert nb.cells[4].source == 'Another markdown cell' + assert nb.cells[5].cell_type == 'markdown' + assert nb.cells[5].source == 'Another markdown cell' - assert nb.cells[5].cell_type == 'code' - assert nb.cells[5].source == """def f(x): + assert nb.cells[6].cell_type == 'code' + assert nb.cells[6].source == """def f(x): '''Sample docstring''' return 4""" - assert len(nb.cells) == 6 + assert len(nb.cells) == 7 script2 = jupytext.writes(nb, ext='.py', format_name='sphinx') compare(script, script2) @@ -71,26 +74,29 @@ def test_read_more_complex_file(script="""'''This is a markdown cell''' """): nb = jupytext.reads(script, ext='.py', format_name='sphinx') - assert nb.cells[0].cell_type == 'markdown' - assert nb.cells[0].source == 'This is a markdown cell' + assert nb.cells[0].cell_type == 'code' + assert nb.cells[0].source == '%matplotlib inline' - assert nb.cells[1].cell_type == 'code' - compare(nb.cells[1].source, '''1 + 2 + 3 + 4 + assert nb.cells[1].cell_type == 'markdown' + assert nb.cells[1].source == 'This is a markdown cell' + + assert nb.cells[2].cell_type == 'code' + compare(nb.cells[2].source, '''1 + 2 + 3 + 4 5 6''') - assert nb.cells[2].cell_type == 'markdown' - assert nb.cells[2].source == 'Another markdown cell' - assert nb.cells[3].cell_type == 'markdown' - assert nb.cells[3].source == 'A third one' + assert nb.cells[3].source == 'Another markdown cell' assert nb.cells[4].cell_type == 'markdown' - assert nb.cells[4].source == 'Another markdown cell' + assert nb.cells[4].source == 'A third one' - assert nb.cells[5].cell_type == 'code' - assert nb.cells[5].source == '1 + 2 + 3 + 4' + assert nb.cells[5].cell_type == 'markdown' + assert nb.cells[5].source == 'Another markdown cell' - assert nb.cells[6].cell_type == 'markdown' - assert nb.cells[6].source == 'And a last one' + assert nb.cells[6].cell_type == 'code' + assert nb.cells[6].source == '1 + 2 + 3 + 4' - assert len(nb.cells) == 7 + assert nb.cells[7].cell_type == 'markdown' + assert nb.cells[7].source == 'And a last one' + + assert len(nb.cells) == 8