mwouts/jupytext

Convert Jupyter notebooks to Hydrogen / VScode / Spyder compatible scripts, and back

Closed this issue ยท 6 comments

Many editors recognize code cells in python scripts as blocks that start with # %%, including

  • Hydrogen
  • VS code
  • Spyder.

I found no specific marker for markdown () or raw cell yet. So let's be inventive and imagine a few specifications for that format: any cell starts with the above prefix. An optional cell type (among: code, markdown, raw) can be specified. An optional cell name can be stated (it cannot be any of code, markdown, raw). Then, if required, we add the cell metadata, in JSON format, like in the following:

# %% optional_cell_type optional_cell_name {"metadata_key1": "value1", "key2": "value2"}
import pandas as pd
pd.DataFrame({'A':[5]}).plot(kind='bar')

# %% markdown
# This is a markdown cell

# %%
# # This is a commented code cell
# import pandas

Hydrogen and VS code execute python code with Jupyter kernels, so there's no need to escape Jupyter magics here.

This is implemented in the latest release candidate for Jupytext! Install the rc with

pip install jupytext==0.7.0rc0

Corresponding documentation is the v0.7.0 branch. Please comment here if it works, if it doesn't, if that's easy to use or not...

Following suggestions by the Spyder team, I will change the cell header to

# %% Optional text [cell type] {optional JSON metadata}

with cell type either absent (code), or [markdown], or [raw].

Available in v0.7.0 now! Please try it.

The code from spyder


#%%
def func1(x):
    # implement func1 (see the example function in chapter 1)
    if type(x) is list:
        x = [(i-1)**2 for i in x]
    if type(x) is tuple:
        x= (x-1)**2
    return x

def der_func1(x):
    # the derivative of func1
    if type(x) is list:
        x = [2*(i-1) for i in x]
    if type(x) is tuple:
        x= 2*(x-1)
  
    return x

is rendered as
2018-10-4-15-46-04

Is it supossed behaviour? (two cells rather than one, + visible percent signs)

Another thing: Maybe is good to have an exemple of percent cells in the demo?

Well, yes. You should add a least a second cell... this is briefly documented in the next README. Let me explain a bit more...

When no format information is given, jupytext classifies percent scripts as such if they contain at least two explicit cells. What's happening here is that jupytext opens your script as a standard Python script, using the light format.

Note that if you generate the percent script using Jupytext, a YAML header with an explicit information on the file format will be added, and Jupyter will open it with the right format.

@danieltomasz , thanks for your example. I just changed the format parser to make sure your example will open as a 'percent' script. The identification of spyder scripts now requires just one cell in the script, that's a simpler criterion!