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

Seamless interoperability with jupytext #131

Open
st-- opened this issue Jan 18, 2021 · 8 comments
Open

Seamless interoperability with jupytext #131

st-- opened this issue Jan 18, 2021 · 8 comments

Comments

@st--
Copy link
Contributor

st-- commented Jan 18, 2021

I've been writing some examples as jupyter notebooks, saved using jupytext as .jl files. I find it helpful to write equations in the jupyter notebook to get instant rendering - but unfortunately I can't directly get this and use Literate.jl because of the discrepancy between using $$ $$ or ```math ``` for display maths, and the extra -- coding: utf-8 -- at the top of the .jl file that doesn't get stripped away. Would you be willing to consider including something like a `jupytext::Bool`` flag that would handle these minor discrepancies and allow seamless use of the same file both for editing in jupyter and for turning into documentation? If you're up for this in general, I'd be happy to discuss how this could look & contribute a PR.

@fredrikekre
Copy link
Owner

Can you post an example of the jupytext output?

@st--
Copy link
Contributor Author

st-- commented Jan 19, 2021

https://github.com/JuliaGaussianProcesses/KernelFunctions.jl/blob/2d84c74da733ab74665c141b0698da01a08cd1f2/examples/gaussianprocesspriors.jl and https://github.com/JuliaGaussianProcesses/KernelFunctions.jl/blob/2d84c74da733ab74665c141b0698da01a08cd1f2/examples/kernel_ridge_regression.jl though I already made some changes to get it to play along a bit better. Notable differences are that Literate.jl exclusively uses # at the line start as "not a code example" and you have to use ## to actually get a comment, whereas jupytext does splitting into cells (and assigning as markdown/code) first. Jupytext also has #- and #+ to denote larger code cells with blank lines inbetween (though there is a slight format variant that does away with this).

@st--
Copy link
Contributor Author

st-- commented Jan 19, 2021

The rendered (Literate.jl/Documenter.jl) output is here: https://juliagaussianprocesses.github.io/KernelFunctions.jl/previews/PR234/examples/gaussianprocesspriors/ and https://juliagaussianprocesses.github.io/KernelFunctions.jl/previews/PR234/examples/kernel_ridge_regression/ (I'm not sure why the first one doesn't actually render the figures, whereas it works fine for the second one 🤔)

@fredrikekre
Copy link
Owner

Thanks. It is strange that some math work and other don't.

@fredrikekre
Copy link
Owner

To get rid of the # -*- coding: utf-8 -*- header you can use a preprocess function, see https://fredrikekre.github.io/Literate.jl/v2/customprocessing/. Something like

function remove_header(str)
    str = replace(str, r"^(.*coding: utf-8.*)$"m => ""; count=1)
end

should work I think.

@st--
Copy link
Contributor Author

st-- commented Jan 19, 2021

Handling ```math ... ``` vs $$ ... $$ still needs extra parsing though, likewise for cell splitting / #- ... #+?

@terasakisatoshi
Copy link

To get rid of the # -*- coding: utf-8 -*- header you can use a preprocess function, see https://fredrikekre.github.io/Literate.jl/v2/customprocessing/. Something like

function remove_header(str)
    str = replace(str, r"^(.*coding: utf-8.*)$"m => ""; count=1)
end

should work I think.

Thank you. This workaround could get rid off ut8-.... 👍

@stevengj
Copy link

stevengj commented Jun 10, 2021

Note that it is also easy to parse Jupyter notebooks and output .jl files yourself, because they are just JSON.

See this discourse post for example — replace text/plain with text/markdown in the output line to output Markdown code for the comments rather than converting to plain text.

Would it be possible to support this directly in Literate.jl, e.g. allowing one to use Jupyter notebooks directly as the input format?

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

4 participants