forked from mwouts/jupytext
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '1.1.0_pandoc_with_mirror_tests' into 1.1.0_rc1
- Loading branch information
Showing
28 changed files
with
1,196 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
"""Jupyter notebook to Markdown and back, using Pandoc""" | ||
|
||
import os | ||
import subprocess | ||
import tempfile | ||
import nbformat | ||
from pkg_resources import parse_version | ||
|
||
|
||
class PandocError(OSError): | ||
"""An error related to Pandoc""" | ||
pass | ||
|
||
|
||
def pandoc(args, filein=None, fileout=None): | ||
"""Execute pandoc with the given arguments""" | ||
cmd = [u'pandoc'] | ||
|
||
if filein: | ||
cmd.append(filein) | ||
|
||
if fileout: | ||
cmd.append('-o') | ||
cmd.append(fileout) | ||
|
||
cmd.extend(args.split()) | ||
|
||
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) | ||
out, err = proc.communicate() | ||
if proc.returncode: | ||
raise PandocError('pandoc exited with return code {}\n{}'.format(proc.returncode, str(err))) | ||
return out.decode('utf-8') | ||
|
||
|
||
def is_pandoc_available(): | ||
"""Is Pandoc>=2.7.1 available?""" | ||
try: | ||
pandoc_version() | ||
return True | ||
except (IOError, OSError, PandocError): | ||
return False | ||
|
||
|
||
def pandoc_version(): | ||
"""Pandoc's version number""" | ||
version = pandoc(u'--version').splitlines()[0].split()[1] | ||
if parse_version(version) < parse_version('2.7.1'): | ||
raise PandocError('Please install pandoc>=2.7.1 (found version {})'.format(version)) | ||
|
||
return version | ||
|
||
|
||
def md_to_notebook(text): | ||
"""Convert a Markdown text to a Jupyter notebook, using Pandoc""" | ||
tmp_file = tempfile.NamedTemporaryFile(delete=False) | ||
tmp_file.write(text.encode('utf-8')) | ||
tmp_file.close() | ||
|
||
pandoc(u'--from markdown --to ipynb -s --atx-headers --wrap=preserve', tmp_file.name, tmp_file.name) | ||
|
||
with open(tmp_file.name, encoding='utf-8') as opened_file: | ||
notebook = nbformat.read(opened_file, as_version=4) | ||
os.unlink(tmp_file.name) | ||
|
||
return notebook | ||
|
||
|
||
def notebook_to_md(notebook): | ||
"""Convert a notebook to its Markdown representation, using Pandoc""" | ||
tmp_file = tempfile.NamedTemporaryFile(delete=False) | ||
tmp_file.write(nbformat.writes(notebook).encode('utf-8')) | ||
tmp_file.close() | ||
|
||
pandoc(u'--from ipynb --to markdown -s --atx-headers --wrap=preserve', tmp_file.name, tmp_file.name) | ||
|
||
with open(tmp_file.name, encoding='utf-8') as opened_file: | ||
text = opened_file.read() | ||
|
||
os.unlink(tmp_file.name) | ||
return '\n'.join(text.splitlines()) |
43 changes: 43 additions & 0 deletions
43
...otebooks/mirror/ipynb_to_pandoc/Notebook with function and cell metadata 164.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
jupyter: | ||
kernelspec: | ||
display_name: Python 3 | ||
language: python | ||
name: python3 | ||
nbformat: 4 | ||
nbformat_minor: 2 | ||
--- | ||
|
||
::: {.cell .code} | ||
``` {.python} | ||
1 + 1 | ||
``` | ||
::: | ||
|
||
::: {.cell .markdown} | ||
A markdown cell | ||
And below, the cell for function f has non trivial cell metadata. And the next cell as well. | ||
::: | ||
|
||
::: {.cell .code attributes="{\"n\":\"10\",\"id\":\"\",\"classes\":[]}"} | ||
``` {.python} | ||
def f(x): | ||
return x | ||
``` | ||
::: | ||
|
||
::: {.cell .code attributes="{\"n\":\"10\",\"id\":\"\",\"classes\":[]}"} | ||
``` {.python} | ||
f(5) | ||
``` | ||
::: | ||
|
||
::: {.cell .markdown} | ||
More text | ||
::: | ||
|
||
::: {.cell .code} | ||
``` {.python} | ||
2 + 2 | ||
``` | ||
::: |
45 changes: 45 additions & 0 deletions
45
tests/notebooks/mirror/ipynb_to_pandoc/Notebook with html and latex cells.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
jupyter: | ||
kernelspec: | ||
display_name: Python 3 | ||
language: python | ||
name: python3 | ||
nbformat: 4 | ||
nbformat_minor: 2 | ||
--- | ||
|
||
::: {.cell .code} | ||
``` {.python} | ||
%%html | ||
<p><a href="https://github.com/mwouts/jupytext", style="color: rgb(0,0,255)">Jupytext</a> on GitHub</p> | ||
``` | ||
::: | ||
|
||
::: {.cell .code} | ||
``` {.python} | ||
%%latex | ||
$\frac{\pi}{2}$ | ||
``` | ||
::: | ||
|
||
::: {.cell .code} | ||
``` {.python} | ||
%load_ext rpy2.ipython | ||
``` | ||
::: | ||
|
||
::: {.cell .code} | ||
``` {.python} | ||
%%R | ||
library(ggplot2) | ||
ggplot(data=data.frame(x=c('A', 'B'), y=c(5, 2)), aes(x,weight=y)) + geom_bar() | ||
``` | ||
::: | ||
|
||
::: {.cell .code} | ||
``` {.python} | ||
%matplotlib inline | ||
import pandas as pd | ||
pd.Series({'A':5, 'B':2}).plot(figsize=(3,2), kind='bar') | ||
``` | ||
::: |
40 changes: 40 additions & 0 deletions
40
tests/notebooks/mirror/ipynb_to_pandoc/Notebook with many hash signs.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
jupyter: | ||
kernelspec: | ||
display_name: Python 3 | ||
language: python | ||
name: python3 | ||
nbformat: 4 | ||
nbformat_minor: 2 | ||
--- | ||
|
||
::: {.cell .markdown} | ||
################################################################## | ||
|
||
This is a notebook that contains many hash signs. | ||
Hopefully its python representation is not recognized as a Sphinx Gallery script\... | ||
|
||
################################################################## | ||
::: | ||
|
||
::: {.cell .code} | ||
``` {.python} | ||
some = 1 | ||
code = 2 | ||
some+code | ||
################################################################## | ||
# A comment | ||
################################################################## | ||
# Another comment | ||
``` | ||
::: | ||
|
||
::: {.cell .markdown} | ||
################################################################## {#section} | ||
|
||
This is a notebook that contains many hash signs. | ||
Hopefully its python representation is not recognized as a Sphinx Gallery script\... | ||
|
||
################################################################## {#section-1} | ||
::: |
Oops, something went wrong.