Skip to content

Commit b853b32

Browse files
authored
Add command line nb2py to convert a notebook into a python example (#16)
* Add command nb2rst * add unit test for command line * rename a command * complete rename * action * fix ut * unittests * update CHANGELOGS
1 parent b061881 commit b853b32

File tree

14 files changed

+1422
-5
lines changed

14 files changed

+1422
-5
lines changed

.github/workflows/documentation.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ jobs:
2525
with:
2626
python-version: '3.11'
2727

28+
- name: Install pandoc
29+
run: sudo apt-get install pandoc
30+
2831
- name: Install requirements
2932
run: python -m pip install -r requirements.txt
3033

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ _doc/examples/plot_*.png
1919
_doc/_static/require.js
2020
_doc/_static/viz.js
2121
_unittests/ut__main/*.png
22+
_unittests/ut__main/data/*.rst
23+
_unittests/ut__main/data/*.py
2224
_doc/examples/*.html
2325
_unittests/ut__main/_cache/*
2426
_unittests/ut__main/*.html

CHANGELOGS.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Change Logs
44
0.2.0
55
+++++
66

7+
* :pr:`16`: add command line nb2py to convert notebook to python files
8+
* :pr:`11`: add function make_linkcode_resolve
79
* :pr:`6`: add sphinx extension gdot
810
* :pr:`5`: add sphinx extension docassert
911
* :pr:`4`: add sphinx extension mathdef, exref, blocref

_doc/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Extensions
1616
epkg
1717
gdot
1818
runpython
19+
tools
1920

2021
Helpers
2122
=======

_doc/api/tools.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=====
2+
tools
3+
=====
4+
5+
Convert notebooks into examples
6+
===============================
7+
8+
The command line converts every notebook in a folder
9+
into exmaples which can be used into a sphinx gallery.
10+
11+
::
12+
13+
python -m sphinx_runpython --help

_doc/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"pandoc": "https://johnmacfarlane.net/pandoc/",
8585
"Pandoc": "https://johnmacfarlane.net/pandoc/",
8686
"PNG": "https://en.wikipedia.org/wiki/PNG",
87+
"pypandoc": "https://github.com/JessicaTegner/pypandoc",
8788
"python": "https://www.python.org/",
8889
"RST": "https://fr.wikipedia.org/wiki/ReStructuredText",
8990
"sphinx": "https://www.sphinx-doc.org/en/master/",

_unittests/ut__main/data/float_and_double_rouding.ipynb

Lines changed: 1246 additions & 0 deletions
Large diffs are not rendered by default.

_unittests/ut__main/test_cmd.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import platform
2+
import unittest
3+
import os
4+
from sphinx_runpython.ext_test_case import ExtTestCase
5+
from sphinx_runpython._cmd_helper import get_parser, nb2py
6+
7+
8+
class TestCmd(ExtTestCase):
9+
def test_cmd(self):
10+
parser = get_parser()
11+
self.assertNotEmpty(parser)
12+
13+
@unittest.skipIf(platform.system() != "Linux", reason="pandoc not installed")
14+
def test_convert(self):
15+
data = os.path.join(os.path.dirname(__file__), "data")
16+
parser = get_parser()
17+
parser.command = "nb2py"
18+
parser.path = data
19+
nb2py(data, verbose=1)
20+
expected = os.path.join(data, "float_and_double_rouding.py")
21+
self.assertExists(expected)
22+
23+
24+
if __name__ == "__main__":
25+
unittest.main(verbosity=2)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import platform
2+
import unittest
3+
import os
4+
from sphinx_runpython.ext_test_case import ExtTestCase
5+
from sphinx_runpython.convert import convert_ipynb_to_gallery
6+
7+
8+
class TestHelpers(ExtTestCase):
9+
@unittest.skipIf(platform.system() != "Linux", reason="pandoc not installed")
10+
def test_convert_notebook(self):
11+
data = os.path.join(os.path.dirname(__file__), "data")
12+
nb = os.path.join(data, "float_and_double_rouding.ipynb")
13+
res = convert_ipynb_to_gallery(nb)
14+
self.assertIn("# :math:`\\mathbb{P}", res)
15+
16+
17+
if __name__ == "__main__":
18+
unittest.main(verbosity=2)

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ matplotlib
88
numpydoc
99
openpyxl
1010
pandas
11+
pypandoc
1112
pyquickhelper
1213
pytest
1314
pytest-cov

0 commit comments

Comments
 (0)