Skip to content

Commit

Permalink
First cell with '%matplotlib inline' for sphinx gallery scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Sep 22, 2018
1 parent cd83906 commit 40c1117
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 28 deletions.
11 changes: 10 additions & 1 deletion jupytext/jupytext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down
9 changes: 9 additions & 0 deletions tests/notebooks/mirror/sphinx_to_ipynb/plot_notebook.ipynb
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down
60 changes: 33 additions & 27 deletions tests/test_read_simple_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

0 comments on commit 40c1117

Please sign in to comment.