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

Add support for line breaks in jupyter #27834

Merged
merged 5 commits into from Apr 23, 2019

Conversation

lc0
Copy link
Contributor

@lc0 lc0 commented Apr 14, 2019

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

@lc0 lc0 marked this pull request as ready for review April 14, 2019 20:19
@lc0
Copy link
Contributor Author

lc0 commented Apr 14, 2019

cc @martinwicke

@rthadur rthadur requested a review from ymodak April 15, 2019 00:54
@rthadur rthadur self-assigned this Apr 15, 2019
@rthadur rthadur added this to Assigned Reviewer in PR Queue via automation Apr 15, 2019
@rthadur rthadur added the size:S CL Change Size: Small label Apr 15, 2019
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
Copy link
Member

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

Copy link
Contributor Author

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 🙂

Copy link
Contributor Author

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

Copy link
Member

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?

Copy link
Contributor Author

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

PR Queue automation moved this from Assigned Reviewer to Reviewer Requested Changes Apr 17, 2019

def check_line_split(code_line):
r"""
Checks if a line was splitted with `\`.
Copy link
Member

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
Copy link
Member

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'):
Copy link
Member

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
Copy link
Member

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?

@martinwicke
Copy link
Member

I still don't understand the logic behind commenting out the continued lines.

Is that encoding ever removed?

@lc0
Copy link
Contributor Author

lc0 commented Apr 17, 2019

@martinwicke right, it's removed once I update from updated code back to notebook. Just some lines below at

cell["source"] = "\n".join(new_code).replace("###!!!", "").replace(

PR Queue automation moved this from Reviewer Requested Changes to Approved by Reviewer Apr 18, 2019
@martinwicke
Copy link
Member

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.

@rthadur rthadur added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Apr 18, 2019
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Apr 18, 2019
@rthadur
Copy link
Contributor

rthadur commented Apr 18, 2019

@lc0 can you please check build failures.

@lc0
Copy link
Contributor Author

lc0 commented Apr 18, 2019

@rthadur I am not sure it's related to my changes

ERROR: /tmpfs/src/github/tensorflow/tensorflow/compiler/xla/BUILD:887:1: name 'cc_header_only_library' is not defined
ERROR: in target '//tensorflow/compiler/xla/service:friends', no such label '//tensorflow/compiler/xla:friends': Target '//tensorflow/compiler/xla:friends' contains an error and its package is in error
ERROR: /tmpfs/src/github/tensorflow/tensorflow/compiler/xla/service/BUILD:30:1: Target '//tensorflow/compiler/xla:xla_data_proto_py_genproto' contains an error and its package is in error and referenced by '//tensorflow/compiler/xla/service:hlo_proto_py_genproto'
ERROR: /tmpfs/src/github/tensorflow/tensorflow/core/protobuf/tpu/BUILD:61:1: Target '//tensorflow/compiler/xla:xla_data_proto_py_genproto' contains an error and its package is in error and referenced by '//tensorflow/core/protobuf/tpu:compilation_result_proto_py_genproto'
ERROR: /tmpfs/src/github/tensorflow/tensorflow/core/protobuf/tpu/BUILD:61:1: Target '//tensorflow/compiler/xla:xla_data_proto_py' contains an error and its package is in error and referenced by '//tensorflow/core/protobuf/tpu:compilation_result_proto_py'
ERROR: Analysis of target '//tensorflow/tools/pip_package:build_pip_package' failed; build aborted: Analysis failed

@lc0
Copy link
Contributor Author

lc0 commented Apr 18, 2019

seems like there are some issues with pilling xla as dependencies, must be coming from a different place cc @rthadur

@lc0
Copy link
Contributor Author

lc0 commented Apr 20, 2019

Would you mind to try to re-run the tests? XLA fails have nothing to do with my changes.

@dynamicwebpaige dynamicwebpaige added the kokoro:force-run Tests on submitted change label Apr 22, 2019
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Apr 22, 2019
@tensorflow-copybara tensorflow-copybara merged commit 850d385 into tensorflow:master Apr 23, 2019
PR Queue automation moved this from Approved by Reviewer to Merged Apr 23, 2019
tensorflow-copybara pushed a commit that referenced this pull request Apr 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes ready to pull PR ready for merge process size:S CL Change Size: Small
Projects
PR Queue
  
Merged
Development

Successfully merging this pull request may close these issues.

handle line splits with \
7 participants