Skip to content

Commit

Permalink
Support Sage notebooks (#732)
Browse files Browse the repository at this point in the history
* WIP Add support for Sage
(language_info is missing in sample notebook)

* Add sage notebook including language_info

* Sage notebooks have .py in language info but still we want .sage as the associated extension

* Add the text representations of the sample Sage notebook

* Update CHANGELOG.md

* Fix the test on the auto ext in the context of Sage notebooks

Co-authored-by: Lars Franke <frcl@mailbox.org>
mwouts and frcl authored Feb 4, 2021
1 parent 7cabad8 commit 518701e
Showing 12 changed files with 109 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
-------------------
1 change: 1 addition & 0 deletions docs/languages.md
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions jupytext/formats.py
Original file line number Diff line number Diff line change
@@ -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", {}
1 change: 1 addition & 0 deletions jupytext/languages.py
Original file line number Diff line number Diff line change
@@ -57,6 +57,7 @@
".sos": {"language": "sos", "comment": "#"},
".java": {"language": "java", "comment": "//"},
".groovy": {"language": "groovy", "comment": "//"},
".sage": {"language": "sage", "comment": "#"},
}

_COMMENT_CHARS = [
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
@@ -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


0 comments on commit 518701e

Please sign in to comment.