Skip to content

Commit

Permalink
Robust conversion of metadata to YAML #99
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Oct 14, 2018
1 parent 32e7306 commit 9c9364d
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 7 deletions.
11 changes: 4 additions & 7 deletions jupytext/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

import re
import yaml
from yaml.representer import SafeRepresenter
import nbformat
from nbformat.v4.nbbase import new_raw_cell
from .version import __version__
from .cell_to_text import comment_lines
from .languages import _SCRIPT_EXTENSIONS

SafeRepresenter.add_representer(nbformat.NotebookNode, SafeRepresenter.represent_dict)

_HEADER_RE = re.compile(r"^---\s*$")
_BLANK_RE = re.compile(r"^\s*$")
_JUPYTER_RE = re.compile(r"^jupyter\s*:\s*$")
Expand All @@ -25,12 +28,6 @@ def insert_or_test_version_number():
return INSERT_AND_CHECK_VERSION_NUMBER


def _as_dict(metadata):
if isinstance(metadata, nbformat.NotebookNode):
return {k: _as_dict(metadata[k]) for k in metadata.keys()}
return metadata


def uncomment_line(line, prefix):
"""Remove prefix (and space) from line"""
if not prefix:
Expand Down Expand Up @@ -91,7 +88,7 @@ def metadata_and_cell_to_header(notebook, text_format, ext):
skipline = not cell.metadata.get('noskipline', False)
notebook.cells = notebook.cells[1:]

metadata = _as_dict(notebook.get('metadata', {}))
metadata = notebook.get('metadata', {})

if insert_or_test_version_number():
metadata.setdefault('jupytext', {})['text_representation'] = {
Expand Down
46 changes: 46 additions & 0 deletions tests/notebooks/ipynb_py/notebook_with_complex_metadata.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.1"
},
"widgets": {
"state": {
"a65a11f142ca44eebc913788d256adcb": {
"views": [
{
"cell_index": 92
}
]
}
},
"version": "1.2.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
jupyter:
kernelspec:
display_name: Python 3
language: python
name: python3
language_info:
codemirror_mode:
name: ipython
version: 3
file_extension: .py
mimetype: text/x-python
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.5.1
widgets:
state:
a65a11f142ca44eebc913788d256adcb:
views:
- cell_index: 92
version: 1.2.0
---

```{python}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
jupyter:
kernelspec:
display_name: Python 3
language: python
name: python3
language_info:
codemirror_mode:
name: ipython
version: 3
file_extension: .py
mimetype: text/x-python
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.5.1
widgets:
state:
a65a11f142ca44eebc913788d256adcb:
views:
- cell_index: 92
version: 1.2.0
---

```python

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# ---
# jupyter:
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# language_info:
# codemirror_mode:
# name: ipython
# version: 3
# file_extension: .py
# mimetype: text/x-python
# name: python
# nbconvert_exporter: python
# pygments_lexer: ipython3
# version: 3.5.1
# widgets:
# state:
# a65a11f142ca44eebc913788d256adcb:
# views:
# - cell_index: 92
# version: 1.2.0
# ---

# %%

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ---
# jupyter:
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# language_info:
# codemirror_mode:
# name: ipython
# version: 3
# file_extension: .py
# mimetype: text/x-python
# name: python
# nbconvert_exporter: python
# pygments_lexer: ipython3
# version: 3.5.1
# widgets:
# state:
# a65a11f142ca44eebc913788d256adcb:
# views:
# - cell_index: 92
# version: 1.2.0
# ---


Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ---
# jupyter:
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# language_info:
# codemirror_mode:
# name: ipython
# version: 3
# file_extension: .py
# mimetype: text/x-python
# name: python
# nbconvert_exporter: python
# pygments_lexer: ipython3
# version: 3.5.1
# widgets:
# state:
# a65a11f142ca44eebc913788d256adcb:
# views:
# - cell_index: 92
# version: 1.2.0
# ---


0 comments on commit 9c9364d

Please sign in to comment.