Skip to content

Commit

Permalink
Ignore cell language when exporting to hydrogen format #179
Browse files Browse the repository at this point in the history
Hydrogen allows cell magics just like in the Jupyter Notebook. There's no need to comment out these cells.
  • Loading branch information
mwouts committed Feb 16, 2019
1 parent 6b54975 commit 213d106
Show file tree
Hide file tree
Showing 16 changed files with 377 additions and 0 deletions.
1 change: 1 addition & 0 deletions jupytext/cell_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ def cell_to_text(self):
class HydrogenCellExporter(DoublePercentCellExporter): # pylint: disable=W0223
"""A class that can represent a notebook cell as a Hydrogen script (#59)"""
default_comment_magics = False
parse_cell_language = False


class SphinxGalleryCellExporter(BaseCellExporter): # pylint: disable=W0223
Expand Down
26 changes: 26 additions & 0 deletions tests/notebooks/hydrogen/hydrogen_latex_html_R_magics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# %%
1 + 1

# %%
import pandas as pd
pd.Series({'A':5, 'B':2}).plot()

# %%
%matplotlib inline
pd.Series({'A':5, 'B':2}).plot(figsize=(3,2))

# %%
%%html
<p><a href="https://github.com/mwouts/jupytext", style="color: rgb(0,0,255)">Jupytext</a> on GitHub</p>

# %%
%load_ext rpy2.ipython

# %%
%%R -w 400 -h 200
library(ggplot2)
ggplot(data=data.frame(x=c('A', 'B'), y=c(5, 2)), aes(x,weight=y)) + geom_bar()

# %%
%%latex
$\frac{\pi}{2}$
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# ---
# jupyter:
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---

# %%
1 + 1


# %% [markdown]
# A markdown cell
# And below, the cell for function f has non trivial cell metadata. And the next cell as well.

# %% {"attributes": {"classes": [], "id": "", "n": "10"}}
def f(x):
return x


# %% {"attributes": {"classes": [], "id": "", "n": "10"}}
f(5)

# %% [markdown]
# More text

# %%
2 + 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# ---
# jupyter:
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---

# %% [markdown]
# ##################################################################
# This is a notebook that contains many hash signs.
# Hopefully its python representation is not recognized as a Sphinx Gallery script...
# ##################################################################

# %%
some = 1
code = 2
some+code

##################################################################
# A comment
##################################################################
# Another comment

# %% [markdown]
# ##################################################################
# This is a notebook that contains many hash signs.
# Hopefully its python representation is not recognized as a Sphinx Gallery script...
# ##################################################################
30 changes: 30 additions & 0 deletions tests/notebooks/mirror/ipynb_to_hydrogen/Notebook_with_R_magic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# ---
# jupyter:
# kernelspec:
# display_name: Python 2
# language: python
# name: python2
# ---

# %% [markdown]
# # A notebook with R cells
#
# This notebook shows the use of R cells to generate plots

# %%
%load_ext rpy2.ipython

# %%
%%R
suppressMessages(require(tidyverse))

# %%
%%R
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color=Species)) + geom_point()

# %% [markdown]
# The default plot dimensions are not good for us, so we use the -w and -h parameters in %%R magic to set the plot size

# %%
%%R -w 400 -h 240
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color=Species)) + geom_point()
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# ---
# jupyter:
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---

# %%
%%time

print('asdf')

# %% [markdown]
# Thanks for jupytext!

# %%
15 changes: 15 additions & 0 deletions tests/notebooks/mirror/ipynb_to_hydrogen/frozen_cell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# ---
# jupyter:
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---

# %%
# This is an unfrozen cell. Works as usual.
print("I'm a regular cell so I run and print!")

# %% {"deletable": false, "editable": false, "run_control": {"frozen": true}}
# # This is an frozen cell
# print("I'm frozen so Im not executed :(")
29 changes: 29 additions & 0 deletions tests/notebooks/mirror/ipynb_to_hydrogen/jupyter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# ---
# jupyter:
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---

# %% [markdown]
# # Jupyter notebook
#
# This notebook is a simple jupyter notebook. It only has markdown and code cells. And it does not contain consecutive markdown cells. We start with an addition:

# %%
a = 1
b = 2
a + b

# %% [markdown]
# Now we return a few tuples

# %%
a, b

# %%
a, b, a+b

# %% [markdown]
# And this is already the end of the notebook
25 changes: 25 additions & 0 deletions tests/notebooks/mirror/ipynb_to_hydrogen/jupyter_again.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ---
# jupyter:
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---

# %%
c = '''
title: "Quick test"
output:
ioslides_presentation:
widescreen: true
smaller: true
editor_options:
chunk_output_type console
'''

# %%
import yaml
print(yaml.dump(yaml.load(c)))

# %%
?next
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# ---
# jupyter:
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---

# %%
1+2+3

# %% [raw]
# This is a raw cell

# %% [markdown]
# This is a markdown cell
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# ---
# title: "Quick test"
# output:
# ioslides_presentation:
# widescreen: true
# smaller: true
# editor_options:
# chunk_output_type console
# jupyter:
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---

# %%
1+2+3

# %%
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ---
# jupyter:
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---

# %%
24 changes: 24 additions & 0 deletions tests/notebooks/mirror/ipynb_to_hydrogen/nteract_with_parameter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# ---
# jupyter:
# kernel_info:
# name: python3
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---

# %% {"outputHidden": false, "inputHidden": false, "tags": ["parameters"]}
param = 4

# %% {"outputHidden": false, "inputHidden": false}
import pandas as pd

# %% {"outputHidden": false, "inputHidden": false}
df = pd.DataFrame({'A': [1, 2], 'B': [3 + param, 4]},
index=pd.Index(['x0', 'x1'], name='x'))
df

# %% {"outputHidden": false, "inputHidden": false}
%matplotlib inline
df.plot(kind='bar')
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# ---
# jupyter:
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---

# %% [markdown] {"slideshow": {"slide_type": "slide"}}
# A markdown cell

# %% {"slideshow": {"slide_type": ""}}
1+1

# %% [markdown] {"cell_style": "center", "slideshow": {"slide_type": "fragment"}}
# Markdown cell two
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"1 + 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"pd.Series({'A':5, 'B':2}).plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"pd.Series({'A':5, 'B':2}).plot(figsize=(3,2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%html\n",
"<p><a href=\"https://github.com/mwouts/jupytext\", style=\"color: rgb(0,0,255)\">Jupytext</a> on GitHub</p>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%load_ext rpy2.ipython"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%R -w 400 -h 200\n",
"library(ggplot2)\n",
"ggplot(data=data.frame(x=c('A', 'B'), y=c(5, 2)), aes(x,weight=y)) + geom_bar()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%latex\n",
"$\\frac{\\pi}{2}$"
]
}
],
"metadata": {
"jupytext": {
"cell_metadata_filter": "-all",
"main_language": "python",
"notebook_metadata_filter": "-all"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 213d106

Please sign in to comment.