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

cell magics do not work with empty lines after #2447 #3178

Closed
juliantaylor opened this issue Apr 15, 2013 · 7 comments
Closed

cell magics do not work with empty lines after #2447 #3178

juliantaylor opened this issue Apr 15, 2013 · 7 comments
Assignees
Milestone

Comments

@juliantaylor
Copy link
Contributor

a file cell magic looking like this:

%%file /tmp/test.py
line1

line3

will give an error with the current 1.0dev git head but it worked fine with 0.13.2

NameError: line3 does not exist

removing the empty line avoids the issue, but that is not really acceptable for readability

Also the docstring says for the notebook -f is implied but it does not say what -f does.

@takluyver
Copy link
Member

Hmm, that sounds like a consequence of #2447. I'll look into it.

-f is force, i.e. don't prompt before overwriting the file. I agree that it should be documented.

@ghost ghost assigned takluyver Apr 15, 2013
minrk added a commit to minrk/ipython that referenced this issue Apr 15, 2013
there was a leftover mention of `-f`, which doesn't actually exist.

see ipython#3178
@minrk
Copy link
Member

minrk commented Apr 15, 2013

-f was removed, but not from the docstring. That part should be fixed by #3182.

@grsr
Copy link

grsr commented Apr 18, 2013

This seems to be a general problem with cell magics, the perl magic also doesn't work with newlines in the cell:

%%perl

my $foo = 'bar';
print $foo;

produces:

File "<ipython-input-18-2874bd4e8290>", line 2
    my $foo = 'bar';
       ^
SyntaxError: invalid syntax

while a version without a newline works as expected:

%%perl
my $foo = 'bar';
print $foo;

produces:

bar

@takluyver
Copy link
Member

Yep, it will affect all cell magics. If you do %hist -t after hitting it, you should see why - it only catches the cell magic code until a blank line, then treats the rest of the cell as regular Python code. I know the general shape of how to fix this, I'm just thinking about how it should fit into our framework.

@ellisonbg
Copy link
Member

But I thought that algorithm for detecting cell magics was dead simple. If The cell begins with double percent the rest of the cel is handled as a cell magic, right?

Sent from my iPhone

On Apr 18, 2013, at 1:43 PM, Thomas Kluyver notifications@github.com wrote:

Yep, it will affect all cell magics. If you do %hist -t after hitting it, you should see why - it only catches the cell magic code until a blank line, then treats the rest of the cell as regular Python code. I know the general shape of how to fix this, I'm just thinking about how it should fit into our framework.


Reply to this email directly or view it on GitHub.

@takluyver
Copy link
Member

Yep, but for the line-based frontends, we decide when a cell magic is complete and ready to execute by looking for a blank line. That also lets you use cell magics in a .ipy script. The code path for 'is this block of input complete' needs to be slightly different from the code path for 'run this complete cell'.

At present, my thinking is to have an attribute of input transformers that indicates whether they're used for completion testing, cell transformation, or both (the default, and what most will use). Then the rule will be that if your input transformer doesn't do both, it must be one of a pair that do both together. Once I've worked out the right API for this, I'll put together a pull request for it.

@takluyver
Copy link
Member

See PR #3203

minrk added a commit that referenced this issue Apr 21, 2013
Separate InputSplitter for transforming whole cells

This allows cell magics to include blank lines once again (closes #3178).

InteractiveShell now has two input splitters:

- `input_splitter` is used to determine when a block of input is complete, so you can use a cell magic in the terminal by leaving a blank line at the end.
- `input_transformer_manager` is used to transform whole cells, so cell magics affect the whole of their cell.

I removed the similar concept of 'input modes' for InputSplitter, because it was bound to get conflated, and it made the code rather hard to follow.
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014
there was a leftover mention of `-f`, which doesn't actually exist.

see ipython#3178
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014
* input_splitter is used to detect when a cell is complete
* input_transformer_manager transforms completed cells

Closes ipythongh-3178
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014
Separate InputSplitter for transforming whole cells

This allows cell magics to include blank lines once again (closes ipython#3178).

InteractiveShell now has two input splitters:

- `input_splitter` is used to determine when a block of input is complete, so you can use a cell magic in the terminal by leaving a blank line at the end.
- `input_transformer_manager` is used to transform whole cells, so cell magics affect the whole of their cell.

I removed the similar concept of 'input modes' for InputSplitter, because it was bound to get conflated, and it made the code rather hard to follow.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants