Skip to content

Commit

Permalink
Mini header #105
Browse files Browse the repository at this point in the history
'language_info' section is not in the text metadata any more. Instead, the notebook language is infered from 'kernelspec.name'.
  • Loading branch information
mwouts committed Nov 25, 2018
1 parent a2fcc1b commit 0537767
Show file tree
Hide file tree
Showing 89 changed files with 70 additions and 677 deletions.
7 changes: 7 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
Release History
---------------

0.8.6 (2018-11-??)
++++++++++++++++++++++

**Improvements**

- The ``language_info`` section is not part of the default header any more. Language information is now taken from metadata ``kernelspec.language``. (#105).

0.8.5 (2018-11-13)
++++++++++++++++++++++

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ Also, you may want some cells to be active only in the Python, or R Markdown rep

The text representation of the notebook focuses on the part of the notebook that you have written. That is also the part of the notebook that should go under version control. Outputs and metadata that are (re)-constructed automatically when the notebook is executed do not need to enter the text representation.

To that aim, cell metadata `autoscroll`, `collapsed`, `scrolled`, `trusted` and `ExecuteTime` are not included in the text representation. And only the required notebook metadata: `kernelspec`, `language_info` and `jupytext` are saved when a notebook is exported as text.
To that aim, cell metadata `autoscroll`, `collapsed`, `scrolled`, `trusted` and `ExecuteTime` are not included in the text representation. And only the required notebook metadata: `kernelspec` and `jupytext` are saved when a notebook is exported as text.

When a paired notebook is loaded, Jupytext reconstructs the filtered metadata using the `.ipynb` file. Please keep in mind that the `.ipynb` file is typically not distributed accross contributors, and that the cell metadata may be lost when an input cell changes (cells are matched according to their contents). Thus, if some cell or notebook metadata are important to your notebook, you should preserve it in the text version. Change the default metadata filtering as follows:
- If you want no YAML header in your script, then set a notebook metadata
Expand Down
2 changes: 1 addition & 1 deletion jupytext/contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def kernelspec_from_language(language):
try:
for name in find_kernel_specs():
ks = get_kernel_spec(name)
if ks.language == language:
if ks.language == language or (language == 'c++' and ks.language.startswith('C++')):
return {'name': name, 'language': language, 'display_name': ks.display_name}
except (KeyError, ValueError):
pass
Expand Down
4 changes: 2 additions & 2 deletions jupytext/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
_DEFAULT_NOTEBOOK_METADATA = ','.join([
# Preserve Jupytext section
'jupytext',
# Preserve kernel specs and language_info
'kernelspec', 'language_info',
# Preserve kernel specs
'kernelspec',
# Kernel_info found in Nteract notebooks
'kernel_info'])

Expand Down
16 changes: 11 additions & 5 deletions jupytext/languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,21 @@ def default_language_from_metadata_and_ext(notebook, ext):
from the given file extension"""
default_from_ext = _SCRIPT_EXTENSIONS.get(ext, {}).get('language', 'python')

return (notebook.metadata.get('language_info', {}).get('name')
or notebook.metadata.get('jupytext', {}).get('main_language') or default_from_ext)
language = (notebook.metadata.get('kernelspec', {}).get('language')
or notebook.metadata.get('jupytext', {}).get('main_language')
or default_from_ext)

if language.startswith('C++'):
language = 'c++'

return language


def set_main_and_cell_language(metadata, cells, ext):
"""Set main language for the given collection of cells, and
use magics for cells that use other languages"""
default_from_ext = _SCRIPT_EXTENSIONS.get(ext, {}).get('language', 'python')
main_language = (metadata.get('language_info', {}).get('name') or
main_language = (metadata.get('kernelspec', {}).get('language') or
metadata.get('jupytext', {}).get('main_language'))
if main_language is None:
languages = {default_from_ext: 0.5}
Expand All @@ -38,8 +44,8 @@ def set_main_and_cell_language(metadata, cells, ext):

main_language = max(languages, key=languages.get)

# save main language when not kernel is set
if 'name' not in metadata.get('language_info', {}):
# save main language when no kernel is set
if 'language' not in metadata.get('kernelspec', {}):
metadata.setdefault('jupytext', {})['main_language'] = main_language

# Remove 'language' meta data and add a magic if not main language
Expand Down
2 changes: 1 addition & 1 deletion jupytext/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Jupytext's version number"""

__version__ = '0.8.5'
__version__ = '0.8.6'
6 changes: 4 additions & 2 deletions tests/notebooks/Rmd/chunk_options.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ title: "Test chunk options in Rmd/Jupyter conversion"
author: "Marc Wouts"
date: "June 16, 2018"
jupyter:
language_info:
name: python
kernelspec:
display_name: Python
language: python
name: python3
---

```{r knitr_setup, include=FALSE}
Expand Down
4 changes: 3 additions & 1 deletion tests/notebooks/mirror/Rmd_to_ipynb/R_sample.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
}
],
"metadata": {
"main_language": "R"
"jupytext": {
"main_language": "R"
}
},
"nbformat": 4,
"nbformat_minor": 2
Expand Down
6 changes: 4 additions & 2 deletions tests/notebooks/mirror/Rmd_to_ipynb/chunk_options.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@
}
],
"metadata": {
"language_info": {
"name": "python"
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
Expand Down
4 changes: 3 additions & 1 deletion tests/notebooks/mirror/Rmd_to_ipynb/ioslides.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@
}
],
"metadata": {
"main_language": "python"
"jupytext": {
"main_language": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
Expand Down
4 changes: 3 additions & 1 deletion tests/notebooks/mirror/Rmd_to_ipynb/knitr-spin.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@
}
],
"metadata": {
"main_language": "R"
"jupytext": {
"main_language": "R"
}
},
"nbformat": 4,
"nbformat_minor": 2
Expand Down
4 changes: 3 additions & 1 deletion tests/notebooks/mirror/Rmd_to_ipynb/markdown.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
}
],
"metadata": {
"main_language": "python"
"jupytext": {
"main_language": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ jupyter:
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.6.6
---

##################################################################
Expand Down
10 changes: 0 additions & 10 deletions tests/notebooks/mirror/ipynb_to_Rmd/Notebook_with_R_magic.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ jupyter:
display_name: Python 2
language: python
name: python2
language_info:
codemirror_mode:
name: ipython
version: 2
file_extension: .py
mimetype: text/x-python
name: python
nbconvert_exporter: python
pygments_lexer: ipython2
version: 2.7.11
---

# A notebook with R cells
Expand Down
10 changes: 0 additions & 10 deletions tests/notebooks/mirror/ipynb_to_Rmd/World population.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ jupyter:
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.6.6
---

# A quick insight at world population
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ jupyter:
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.3
---

```{python}
Expand Down
10 changes: 0 additions & 10 deletions tests/notebooks/mirror/ipynb_to_Rmd/frozen_cell.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ jupyter:
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.7.0
---

```{python}
Expand Down
7 changes: 0 additions & 7 deletions tests/notebooks/mirror/ipynb_to_Rmd/ir_notebook.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ jupyter:
display_name: R
language: R
name: ir
language_info:
codemirror_mode: r
file_extension: .r
mimetype: text/x-r-source
name: R
pygments_lexer: r
version: 3.5.0
---

This is a jupyter notebook that uses the IR kernel.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ jupyter:
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.6.6
---

```{python}
Expand Down
10 changes: 0 additions & 10 deletions tests/notebooks/mirror/ipynb_to_Rmd/jupyter.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ jupyter:
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.6.4
---

# Jupyter notebook
Expand Down
10 changes: 0 additions & 10 deletions tests/notebooks/mirror/ipynb_to_Rmd/jupyter_again.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ jupyter:
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.6.5
---

```{python}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ jupyter:
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.6.6
---

```{python}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ jupyter:
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.6.5
---

```{python}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ jupyter:
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
---

```{python}
Expand Down
10 changes: 0 additions & 10 deletions tests/notebooks/mirror/ipynb_to_Rmd/nteract_with_parameter.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ jupyter:
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.6.6
---

```{python outputHidden=FALSE, inputHidden=FALSE, tags=c("parameters")}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ jupyter:
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.6.6
---

##################################################################
Expand Down
Loading

0 comments on commit 0537767

Please sign in to comment.