Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Sage notebooks #732

Merged
merged 6 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Jupytext ChangeLog
1.10.1 (2021-02-??)
-------------------

**Added**
- Sage notebooks are supported. They can be converted to `.sage` and `.md` files and back. Thanks to Lars Franke for suggesting this! ([#727](https://github.com/mwouts/jupytext/issues/727))


1.10.0 (2021-02-04)
-------------------
Expand Down
1 change: 1 addition & 0 deletions docs/languages.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Jupytext works with notebooks in any of the following languages:
- R
- Robot Framework
- Rust/Evxcr
- Sage
- Scala
- Scheme
- Script of Script
Expand Down
5 changes: 5 additions & 0 deletions jupytext/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,11 @@ def auto_ext_from_metadata(metadata):
"""Script extension from notebook metadata"""
auto_ext = metadata.get("language_info", {}).get("file_extension")

# Sage notebooks have ".py" as the associated extension in "language_info",
# so we change it to ".sage" in that case, see #727
if auto_ext == ".py" and metadata.get("kernelspec", {}).get("language") == "sage":
auto_ext = ".sage"

if auto_ext is None:
language = metadata.get("kernelspec", {}).get("language") or metadata.get(
"jupytext", {}
Expand Down
1 change: 1 addition & 0 deletions jupytext/languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
".sos": {"language": "sos", "comment": "#"},
".java": {"language": "java", "comment": "//"},
".groovy": {"language": "groovy", "comment": "//"},
".sage": {"language": "sage", "comment": "#"},
}

_COMMENT_CHARS = [
Expand Down
34 changes: 34 additions & 0 deletions tests/notebooks/ipynb_sage/sage_print_hello.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Hello world\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "SageMath 9.2",
"language": "sage",
"name": "sagemath"
},
"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.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
11 changes: 11 additions & 0 deletions tests/notebooks/mirror/ipynb_to_Rmd/sage_print_hello.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
jupyter:
kernelspec:
display_name: SageMath 9.2
language: sage
name: sagemath
---

```{sage}
print("Hello world")
```
10 changes: 10 additions & 0 deletions tests/notebooks/mirror/ipynb_to_hydrogen/sage_print_hello.sage
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# ---
# jupyter:
# kernelspec:
# display_name: SageMath 9.2
# language: sage
# name: sagemath
# ---

# %%
print("Hello world")
11 changes: 11 additions & 0 deletions tests/notebooks/mirror/ipynb_to_md/sage_print_hello.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
jupyter:
kernelspec:
display_name: SageMath 9.2
language: sage
name: sagemath
---

```sage
print("Hello world")
```
10 changes: 10 additions & 0 deletions tests/notebooks/mirror/ipynb_to_myst/sage_print_hello.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
kernelspec:
display_name: SageMath 9.2
language: sage
name: sagemath
---

```{code-cell} ipython3
print("Hello world")
```
10 changes: 10 additions & 0 deletions tests/notebooks/mirror/ipynb_to_percent/sage_print_hello.sage
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# ---
# jupyter:
# kernelspec:
# display_name: SageMath 9.2
# language: sage
# name: sagemath
# ---

# %%
print("Hello world")
9 changes: 9 additions & 0 deletions tests/notebooks/mirror/ipynb_to_script/sage_print_hello.sage
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ---
# jupyter:
# kernelspec:
# display_name: SageMath 9.2
# language: sage
# name: sagemath
# ---

print("Hello world")
4 changes: 4 additions & 0 deletions tests/test_auto_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def test_auto_from_kernelspecs_works(nb_file):
elif expected_ext == ".fs":
expected_ext = ".fsx"
auto_ext = auto_ext_from_metadata(nb.metadata)
if auto_ext == ".sage":
pytest.xfail(
"Sage notebooks have Python in their language_info metadata, see #727"
)
assert auto_ext == expected_ext


Expand Down