-
Notifications
You must be signed in to change notification settings - Fork 392
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
add support for parsing raw multiline strings #836
Conversation
Hi @jimustafa , thanks for your pull request! Don't you think that we should add a test to make sure that a round trip on a text notebook with a raw string preserves the raw string? Also for those like me who didn't know about raw strings, I see that they are documented at https://docs.python.org/3/reference/lexical_analysis.html |
Greetings @mwouts. Thanks for the feedback. Yes, it would be good to have a test for this case. Are you suggesting a check that py->ipynb->py is idempotent? |
Hello @jimustafa , yes I think we should test the round trips, both Now I have another question for you. Could you please provide more information about what made you consider using raw strings? I am inclined to think that you author notebooks in a text editor and that text editor gave you trouble with some strings? Is that correct? Could you please provide an example or such a string? I am asking as I think in |
Yes, I have been writing regular Python scripts with a text editor and converting them to notebooks using # %% [markdown]
R"""
## Description
This notebook computes the blackbody spectrum.
$$
B_\lambda(\lambda,T)=\frac{2hc^2}{\lambda^5}\frac{1}{e^{hc/(\lambda k_\mathrm{B}T)}-1}
$$
""" I think the current approach works well; it just needs to be generalized a bit to allow for the opening triple quotes to be prefixed with an "r" or "R" to indicate a raw string. It may be the case that a raw string is the most suitable for encoding Markdown cells, it certainly seems appropriate for writing a Markdown cell in a Python script. Escape characters aside, it would be nice to support the raw string format as acceptable Python syntax. |
Hi @jimustafa , sorry I was busy for a while. Thanks for the example, do you mind if I reuse it into a test? What I propose is the following:
The impact on existing text notebooks should be minor (only cells with \ will be affected), and this change will turn the py file into a more standard one. In terms of round trip, ipynb->py->ipynb will be completely stable, however py -> ipynb -> py will turn raw strings into plain strings if these strings do not contain |
All good, thanks for getting back to the discussion. Yes, please feel free to use the snippet. Regarding your proposal, I would not change how # %% [markdown]
R"""
# Test
""" and yields (partially) "cells": [
{
"cell_type": "markdown",
"id": "d8046188",
"metadata": {
"cell_marker": "R\"\"\"\n,\n\"\"\""
},
"source": [
"# Test"
]
}
] Then the # %% [markdown]
# # Test So, it seems that the |
Hi @jimustafa , I have added a few tests & commits on top of yours at #850 , would you like to give a try to that version? You can install it with
(feel free to remove |
Looks like #850 works! Thanks! Now, should we start thinking about raw f-strings 😉 Not even sure if that should be expected to work... Well, maybe not interpolation, but, even without interpolation, it would be a valid triple-quoted string. For example: # %% [markdown]
Rf"""
A $\LaTeX$ expression
""" |
Thank you @jimustafa for giving it a try! Ha ha I like the f-string suggestion... This reminds me of #498 and of the python-markdown extension However I don't think it is a good idea to add the |
These changes allow for multiline strings containing Markdown to be prefixed with "r" or "R" to indicate that they are raw strings. This can help avoid noisy syntax highlighting for escape characters, especially when writing LaTeX, which uses a lot of backslashes.