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

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

Closed
mwouts opened this issue Sep 6, 2018 · 6 comments

Comments

@mwouts
Copy link
Owner

mwouts commented Sep 6, 2018

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.

@mwouts
Copy link
Owner Author

mwouts commented Sep 22, 2018

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...

@mwouts
Copy link
Owner Author

mwouts commented Sep 23, 2018

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].

@mwouts
Copy link
Owner Author

mwouts commented Sep 23, 2018

Available in v0.7.0 now! Please try it.

@danieltomasz
Copy link

danieltomasz commented Oct 4, 2018

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?

@mwouts
Copy link
Owner Author

mwouts commented Oct 4, 2018

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.

@mwouts
Copy link
Owner Author

mwouts commented Oct 7, 2018

@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!

mwouts added a commit that referenced this issue Dec 14, 2018
New hydrogen format for scripts, which derives from percent. In that format Jupyter magic commands are not commented (#59, #126, #132).
mwouts added a commit that referenced this issue Jan 14, 2019
New hydrogen format for scripts, which derives from percent. In that format Jupyter magic commands are not commented (#59, #126, #132).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants