Skip to content

Commit

Permalink
Introducing the bare format
Browse files Browse the repository at this point in the history
Fixes #152
  • Loading branch information
mwouts committed Jan 19, 2019
1 parent 1694bba commit 4605540
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Release History
- The pairing information, and more generally the notebook metadata can be edited with the CLL, see the ``--set-formats`` and the ``--update-metadata`` arguments (#141).
- Format specification allow prefix and suffix for path and file name (#138, #142). Use ``ipynb,prefix/suffix.py:percent`` to pair the current notebook named ``notebook.ipynb`` to a script named ``prefixnotebooksuffix.py``. Suffix and prefix can also be configured on the ``ipynb`` file, with the same syntax.
- Introducing a new ``hydrogen`` format for scripts, which derives from ``percent``. In that format Jupyter magic commands are not commented (#59, #126, #132).
- Introducing a new ``bare`` format for scripts, which derives from ``light``. That format has no cell marker. Use a notebook metadata filter ``{"jupytext": {"notebook_metadata_filter":"-all"}}`` if you want no YAML header (#152).
- Python scripts or Markdown documents that have no Jupyter metadata receive a metadata filter that ensures that metadata is not exported back to the text representation (#124).
- Metadata filters are represented as strings rather than dictionaries to make YAML headers shorter. Previous syntax from #105 is still supported. They were also renamed to ``notebook_metadata_filter`` and ``cell_metadata_filter``.
- Markdown and RMarkdown formats have a new option ``split_at_heading`` to split Markdown cells at heading (#130)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ The `light` format has:

The `light` format is currently available for Python, Julia, R, Bash, Scheme and C++. Open our sample notebook in the `light` format [here](https://github.com/mwouts/jupytext/blob/master/demo/World%20population.lgt.py).

A variation of the `light` format is the `bare` format, with no cell marker at all. Please note that this format will split your code cells on code paragraphs. By default, this format still includes a YAML header - if you prefer to also remove the header, set `"notebook_metadata_filter": "-all"` in the jupytext section of your notebook metadata.

### The `percent` format

The `percent` format is a representation of Jupyter notebooks as scripts, in which cells are delimited with a commented double percent sign `# %%`. The format was introduced by Spyder five years ago, and is now supported by many editors, including
Expand Down
10 changes: 9 additions & 1 deletion jupytext/cell_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ def endofcell_marker(source, comment):
class LightScriptCellExporter(BaseCellExporter):
"""A class that represent a notebook cell as a Python or Julia script"""
default_comment_magics = True
use_cell_markers = True

def __init__(self, *args, **kwargs):
BaseCellExporter.__init__(self, *args, **kwargs)
Expand Down Expand Up @@ -199,7 +200,7 @@ def code_to_text(self):
if self.explicit_start_marker(source):
self.metadata['endofcell'] = endofcell_marker(source, self.comment)

if not self.metadata:
if not self.metadata or not self.use_cell_markers:
return source

lines = []
Expand All @@ -215,6 +216,8 @@ def code_to_text(self):
def explicit_start_marker(self, source):
"""Does the python representation of this cell requires an explicit
start of cell marker?"""
if not self.use_cell_markers:
return False
if self.metadata:
return True
if all([line.startswith(self.comment) for line in self.source]):
Expand Down Expand Up @@ -242,6 +245,11 @@ def simplify_code_markers(self, text, next_text, lines):
return text


class BareScriptCellExporter(LightScriptCellExporter):
"""A class that writes notebook cells as scripts with no cell markers"""
use_cell_markers = False


class RScriptCellExporter(BaseCellExporter):
"""A class that can represent a notebook cell as a R script"""
default_comment_magics = True
Expand Down
11 changes: 10 additions & 1 deletion jupytext/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
LightScriptCellReader, RScriptCellReader, DoublePercentScriptCellReader, HydrogenCellReader, \
SphinxGalleryScriptCellReader
from .cell_to_text import MarkdownCellExporter, RMarkdownCellExporter, \
LightScriptCellExporter, RScriptCellExporter, DoublePercentCellExporter, \
LightScriptCellExporter, BareScriptCellExporter, RScriptCellExporter, DoublePercentCellExporter, \
HydrogenCellExporter, SphinxGalleryCellExporter
from .cell_metadata import _JUPYTEXT_CELL_METADATA
from .stringparser import StringParser
Expand Down Expand Up @@ -87,6 +87,15 @@ def __init__(self,
# Version 1.0 on 2018-08-22 - jupytext v0.5.2 : Initial version
current_version_number='1.3',
min_readable_version_number='1.1') for ext in _SCRIPT_EXTENSIONS] + \
[
NotebookFormatDescription(
format_name='bare',
extension=ext,
header_prefix=_SCRIPT_EXTENSIONS[ext]['comment'],
cell_reader_class=LightScriptCellReader,
cell_exporter_class=BareScriptCellExporter,
current_version_number='1.0',
min_readable_version_number='1.0') for ext in _SCRIPT_EXTENSIONS] + \
[
NotebookFormatDescription(
format_name='percent',
Expand Down
31 changes: 31 additions & 0 deletions tests/test_read_simple_bare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from nbformat.v4.nbbase import new_notebook, new_code_cell, new_markdown_cell
from testfixtures import compare
from jupytext import reads, writes
from jupytext.combine import combine_inputs_with_outputs


def test_write_reload_simple_notebook():
nb1 = new_notebook(cells=[
new_markdown_cell('A markdown cell', metadata={'md': 'value'}),
new_code_cell('1 + 1'),
new_markdown_cell('A markdown cell', metadata={'md': 'value'}),
new_code_cell("""def f(x):
return x""", metadata={'md': 'value'}),
new_markdown_cell('A markdown cell', metadata={'md': 'value'}),
new_code_cell("""def g(x):
return x
def h(x):
return x
""", metadata={'md': 'value'})])

text = writes(nb1, 'py:bare')
nb2 = reads(text, 'py:bare')
combine_inputs_with_outputs(nb2, nb1, 'py:bare')
nb2.metadata.pop('jupytext')

assert len(nb2.cells) == 7
nb1.cells = nb1.cells[:5]
nb2.cells = nb2.cells[:5]
compare(nb1, nb2)

0 comments on commit 4605540

Please sign in to comment.