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 line breaks in jupyter #27834
Add support for line breaks in jupyter #27834
Conversation
6787ed1
to
8fb20dd
Compare
cc @martinwicke |
for line_idx, code_line in enumerate(cell_lines): | ||
|
||
# Sometimes, jupyter has more than python code | ||
# Idea is to comment these lines, for upgrade time | ||
if code_line.startswith("%") or code_line.startswith("!") \ | ||
or code_line.startswith("?"): | ||
if skip_magic(code_line, ['%', '!', '?']) or is_line_split: | ||
# Found a special character, need to "encode" | ||
code_line = "###!!!" + code_line |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand how this works...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea behind is just to make it work for cases like
!gcloud ml-engine models create ${MODEL} \
--regions us-central1
So I just check if the "magic" code line ends with , so I also need to comment the next line, since it's not python 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also prepared a couple of test case, that I am planning to add somewhere, so it's testable - > https://colab.research.google.com/drive/1fCcmg8ZcbnR5dnG3KNl96QKN7u0FrQL-#scrollTo=jrxeKRCwogn6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, would be good to have test cases for this stuff with the code here.
I see the logic to skip the next line, but what does the ###!!!
do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just a way to encode these sections with unique commenting prefix, so I can strip them after pasta.
###!!!
it's just "unique pattern" that is less likely to be used by somebody else. Otherwise I would remove something, that I did not want to remove
|
||
def check_line_split(code_line): | ||
r""" | ||
Checks if a line was splitted with `\`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
splitted -> split
Checks if the cell has magic, that is not python-based. | ||
|
||
>>> skip_magic('!ls -laF', ['%', '!', '?']) | ||
True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add an args, returns section, and put the brief description on the same line as the opening """? (also below)
True | ||
""" | ||
|
||
if code_line.endswith('\\\n'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this catch lines with trailing whitespace (which I think is allowed after \
)?
for line_idx, code_line in enumerate(cell_lines): | ||
|
||
# Sometimes, jupyter has more than python code | ||
# Idea is to comment these lines, for upgrade time | ||
if code_line.startswith("%") or code_line.startswith("!") \ | ||
or code_line.startswith("?"): | ||
if skip_magic(code_line, ['%', '!', '?']) or is_line_split: | ||
# Found a special character, need to "encode" | ||
code_line = "###!!!" + code_line |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, would be good to have test cases for this stuff with the code here.
I see the logic to skip the next line, but what does the ###!!!
do?
I still don't understand the logic behind commenting out the continued lines. Is that encoding ever removed? |
@martinwicke right, it's removed once I update from updated code back to notebook. Just some lines below at
|
Got it. Test cases would be nice, maybe just a test notebook that has cells containing stuff like this that you can run this on to make sure it works. |
@lc0 can you please check build failures. |
@rthadur I am not sure it's related to my changes
|
seems like there are some issues with pilling xla as dependencies, must be coming from a different place cc @rthadur |
Would you mind to try to re-run the tests? XLA fails have nothing to do with my changes. |
PiperOrigin-RevId: 244888116
jupyter cells could have a single line magic, but that splits a line with
\
.This PR adds support for such cases.
Closes lc0/tf2up#31