Skip to content

Commit

Permalink
auto:percent becomes the default format for exporting as script
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Jan 9, 2024
1 parent 0606f03 commit e5460f5
Show file tree
Hide file tree
Showing 21 changed files with 117 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ function getSelectedFormats(notebookTracker: INotebookTracker): Array<string> {
if (languageInfo && languageInfo.file_extension) {
const scriptExt = languageInfo.file_extension.substring(1);
formats = formats.map((format) => {
// By default use light format
// By default use percent format
if (format === scriptExt) {
return 'auto:light';
return 'auto:percent';
}
// Replace language specific extension with auto
return format.replace(`${scriptExt}:`, 'auto:');
Expand Down Expand Up @@ -102,8 +102,8 @@ function getSelectedFormats(notebookTracker: INotebookTracker): Array<string> {
'jupytext'
) as IJupytextSection;
const formatName = jupytext
? jupytext?.text_representation?.formatName || 'light'
: 'light';
? jupytext?.text_representation?.formatName || 'percent'
: 'percent';
formats.push(`auto:${formatName}`);
}
return formats;
Expand Down
41 changes: 9 additions & 32 deletions jupyterlab/packages/jupyterlab-jupytext-extension/src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,6 @@ export const JUPYTEXT_PAIR_COMMANDS_FILETYPE_DATA = new Map<
},
],
],
[
'auto:light',
[
{
fileExt: 'auto:light',
paletteLabel: 'Pair with light script',
caption: 'Pair Notebook with Light Format',
iconName: 'ui-components:text-editor',
},
],
],
[
'auto:percent',
[
Expand All @@ -107,12 +96,12 @@ export const JUPYTEXT_PAIR_COMMANDS_FILETYPE_DATA = new Map<
],
],
[
'auto:hydrogen',
'auto:light',
[
{
fileExt: 'auto:hydrogen',
paletteLabel: 'Pair with hydrogen script',
caption: 'Pair Notebook with Hydrogen Format',
fileExt: 'auto:light',
paletteLabel: 'Pair with light script',
caption: 'Pair Notebook with Light Format',
iconName: 'ui-components:text-editor',
},
],
Expand Down Expand Up @@ -250,17 +239,6 @@ export const JUPYTEXT_CREATE_TEXT_NOTEBOOK_FILETYPE_DATA = new Map<
string,
IFileTypeData[]
>([
[
'auto:light',
[
{
fileExt: 'auto:light',
paletteLabel: 'Light Format',
caption: 'Light Format',
launcherLabel: 'Light Format',
},
],
],
[
'auto:percent',
[
Expand All @@ -273,13 +251,13 @@ export const JUPYTEXT_CREATE_TEXT_NOTEBOOK_FILETYPE_DATA = new Map<
],
],
[
'auto:hydrogen',
'auto:light',
[
{
fileExt: 'auto:hydrogen',
paletteLabel: 'Hydrogen Format',
caption: 'Hydrogen Format',
launcherLabel: 'Hydrogen Format',
fileExt: 'auto:light',
paletteLabel: 'Light Format',
caption: 'Light Format',
launcherLabel: 'Light Format',
},
],
],
Expand Down Expand Up @@ -364,7 +342,6 @@ export const TEXT_NOTEBOOKS_LAUNCHER_ICONS = JUPYTEXT_FORMATS.filter(
(format) => {
return ![
'ipynb',
'auto:hydrogen',
'auto:nomarker',
'qmd',
'custom',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export async function getAvailableCreateTextNotebookCommands(
fileTypes.map((fileType: IFileTypeData) => {
// If format is auto, we need to add all currently available kernels
// For instance if there are Python and R kernels available, format
// auto:light will be replaced by py:light and R:light
// auto:percent will be replaced by py:percent and R:percent
if (format.startsWith('auto')) {
const formatType = format.split(':')[1];
let mapIndex = 0;
Expand Down
9 changes: 4 additions & 5 deletions src/jupytext/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,10 @@ def parse_jupytext_args(args=None):
}
)
)
+ "The default format for scripts is the 'light' format, "
"which uses few cell markers (none when possible). "
"Alternatively, a format compatible with many editors is the "
"'percent' format, which uses '# %%%%' as cell markers. "
"The main formats (markdown, light, percent) preserve "
+ "The default format for scripts is the 'percent' format, "
"which uses '# %%%%' as cell markers and is compatible with VS Code and PyCharm. "
"Alternatively, you can also use the 'light' format, which uses fewer cell markers. "
"The main formats (MyST Markdown, Markdown, percent, light) preserve "
"notebooks and text documents in a roundtrip. Use the "
"--test and and --test-strict commands to test the roundtrip on your files. "
"Read more about the available formats at "
Expand Down
6 changes: 6 additions & 0 deletions src/jupytext/jupytext.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
metadata_and_cell_to_header,
)
from .languages import (
_SCRIPT_EXTENSIONS,
default_language_from_metadata_and_ext,
set_main_and_cell_language,
)
Expand Down Expand Up @@ -476,6 +477,11 @@ def writes(notebook, fmt, version=nbformat.NO_CONVERT, config=None, **kwargs):
if not format_name:
format_name = format_name_for_ext(metadata, ext, explicit_default=False)

# Since Jupytext==1.17, the default format for
# writing a notebook to a script is the percent format
if not format_name and "cell_markers" not in fmt and ext in _SCRIPT_EXTENSIONS:
format_name = "percent"

if format_name:
fmt["format_name"] = format_name
update_jupytext_formats_metadata(metadata, fmt)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# ---
# jupyter:
# kernelspec:
# display_name: R
# language: R
# name: ir
# ---
#' ---
#' jupyter:
#' kernelspec:
#' display_name: R
#' language: R
#' name: ir
#' ---

# This notebook was created with IRKernel 0.8.12, and is not completely valid, as the code cell below contains an unexpected 'source' entry. This did cause https://github.com/mwouts/jupytext/issues/234. Note that the problem is solved when one upgrades to IRKernel 1.0.0.
#' This notebook was created with IRKernel 0.8.12, and is not completely valid, as the code cell below contains an unexpected 'source' entry. This did cause https://github.com/mwouts/jupytext/issues/234. Note that the problem is solved when one upgrades to IRKernel 1.0.0.

library("ggplot2")
ggplot(mtcars, aes(mpg)) + stat_ecdf()
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# ---
# jupyter:
# kernelspec:
# display_name: R
# language: R
# name: ir
# ---
#' ---
#' jupyter:
#' kernelspec:
#' display_name: R
#' language: R
#' name: ir
#' ---

# This notebook was created with IRKernel 0.8.12, and is not completely valid, as the code cell below contains an unexpected 'source' entry. This did cause https://github.com/mwouts/jupytext/issues/234. Note that the problem is solved when one upgrades to IRKernel 1.0.0.
#' This notebook was created with IRKernel 0.8.12, and is not completely valid, as the code cell below contains an unexpected 'source' entry. This did cause https://github.com/mwouts/jupytext/issues/234. Note that the problem is solved when one upgrades to IRKernel 1.0.0.

library("ggplot2")
ggplot(mtcars, aes(mpg)) + stat_ecdf()
18 changes: 9 additions & 9 deletions tests/data/notebooks/outputs/ipynb_to_spin/ir_notebook.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# ---
# jupyter:
# kernelspec:
# display_name: R
# language: R
# name: ir
# ---

# This is a jupyter notebook that uses the IR kernel.
#' ---
#' jupyter:
#' kernelspec:
#' display_name: R
#' language: R
#' name: ir
#' ---

#' This is a jupyter notebook that uses the IR kernel.

sum(1:10)

Expand Down
18 changes: 9 additions & 9 deletions tests/data/notebooks/outputs/ipynb_to_spin/ir_notebook.low.r
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# ---
# jupyter:
# kernelspec:
# display_name: R
# language: R
# name: ir
# ---

# This is a jupyter notebook that uses the IR kernel.
#' ---
#' jupyter:
#' kernelspec:
#' display_name: R
#' language: R
#' name: ir
#' ---

#' This is a jupyter notebook that uses the IR kernel.

sum(1:10)

Expand Down
4 changes: 2 additions & 2 deletions tests/external/pre_commit/test_pre_commit_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_ignore_unmatched_ignores(tmpdir, cwd_tmpdir):

# Run jupytext
status = jupytext(
["--from", "ipynb", "--to", "py:light", "--pre-commit-mode", file]
["--from", "ipynb", "--to", "py:percent", "--pre-commit-mode", file]
)

assert status == 0
Expand Down Expand Up @@ -90,7 +90,7 @@ def test_alert_untracked_alerts_for_modified(tmpdir, cwd_tmpdir, tmp_repo, capsy

# Run jupytext
status = jupytext(
["--from", "ipynb", "--to", "py:light", "--pre-commit-mode", "test.ipynb"]
["--from", "ipynb", "--to", "py:percent", "--pre-commit-mode", "test.ipynb"]
)

assert status == 1
Expand Down
8 changes: 4 additions & 4 deletions tests/external/pre_commit/test_pre_commit_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_pre_commit_hook(tmpdir):
git = git_in_tmpdir(tmpdir)
hook = str(tmpdir.join(".git/hooks/pre-commit"))
with open(hook, "w") as fp:
fp.write("#!/bin/sh\n" "jupytext --to py:light --pre-commit\n")
fp.write("#!/bin/sh\n" "jupytext --to py:percent --pre-commit\n")

st = os.stat(hook)
os.chmod(hook, st.st_mode | stat.S_IEXEC)
Expand Down Expand Up @@ -148,7 +148,7 @@ def test_pre_commit_hook_in_subfolder(tmpdir):
hook = str(tmpdir.join(".git/hooks/pre-commit"))
with open(hook, "w") as fp:
fp.write(
"#!/bin/sh\n" "jupytext --from ipynb --to python//py:light --pre-commit\n"
"#!/bin/sh\n" "jupytext --from ipynb --to python//py:percent --pre-commit\n"
)

st = os.stat(hook)
Expand Down Expand Up @@ -178,8 +178,8 @@ def test_pre_commit_hook_py_to_ipynb_and_md(tmpdir):
with open(hook, "w") as fp:
fp.write(
"#!/bin/sh\n"
"jupytext --from py:light --to ipynb --pre-commit\n"
"jupytext --from py:light --to md --pre-commit\n"
"jupytext --from py:percent --to ipynb --pre-commit\n"
"jupytext --from py:percent --to md --pre-commit\n"
)

st = os.stat(hook)
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ def test_jupytext_to_ipynb_does_not_update_timestamp_if_not_paired(
assert "Updating the timestamp" not in capture.out


@pytest.mark.parametrize("formats", ["ipynb,py", "py:percent", "py", None])
@pytest.mark.parametrize("formats", ["ipynb,py", "py:percent", "py:light", None])
def test_use_source_timestamp(tmpdir, cwd_tmpdir, python_notebook, capsys, formats):
# Write a text notebook
nb = python_notebook
Expand Down Expand Up @@ -1302,7 +1302,7 @@ def test_use_source_timestamp(tmpdir, cwd_tmpdir, python_notebook, capsys, forma
os.utime(test_py, (src_timestamp, src_timestamp))

# Then we can't open paired notebooks
if formats == "ipynb,py":
if formats == "ipynb,py:percent":
from tornado.web import HTTPError

with pytest.raises(HTTPError, match="is more recent than test.py"):
Expand Down
Loading

0 comments on commit e5460f5

Please sign in to comment.