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
Refactoring of the magics system and implementation of cell magics #1732
Conversation
What I think is important is that from the user's perspective there can be cell and line magics with the same name. We don't want to have to have separate In terms of the implementation, I think you should do whatever makes it easiest to accomplish this with minimal complexity and code bloat - even if it doesn't follow the IPEP. If you feel like you don't have time to support cell magics with the same name right now, then let's come up with a design that will allow us to add that in later without breaking the API we put in place now. |
@ellisonbg, sorry for not being clear enough: there's nothing to worry user-side, it's just that in order to implement a magic that can be used both as @line_cell_magic
def f(self, line, cell=None):
if cell is None:
do_line_stuff(line)
else:
do_line_and_cell_stuff(line, cell) instead of being able to use separate implementations for the line-only and cell-only parts. So I don't think it's a big deal. I'll add this new decorator. It's just that I'm finding I need to have a single namespace, so that a magic name X maps to only one function, whether it's line-only, cell-only or line-and-cell. The management of truly disjoint line and cell namespaces was proving to be an ever-growing tangle of complexity. Does that make sense? |
Yes, this makes sense. While it is a bit of a pain for people writing On Tue, May 15, 2012 at 10:14 PM, Fernando Perez
Brian E. Granger |
PR #1630 now touches the parallelmagic extension (silent keyword had to be added to run_cell), which will obviously conflict with this. The necessary change was quite tiny, but I included a fair amount of cleanup since that file gets neglected and had gone very stale (docstrings were simply wrong, and not updated since Twisted days, and the run_code override is entirely unnecessary). Should I rollback the cleanup, so resolving conflicts is easier, and doesn't add to the scale of this guy? |
Hey guys (esp. @ellisonbg & @minrk), this has proved to be a fair bit more time-consuming than I was hoping for, but there's now light at the end of the tunnel. At this point, I'm getting the full test suite to pass again (modulo an occasional zmq failure that I only see intermittently and never in isolation, and which is unrelated to magics, so I hope it will get fixed elsewhere). Note that I still don't actually have the real I'll now focus on getting the actual cell syntax working. I think it should go comparatively quickly, considering that all the infrastructure is now in place. But I've learned to be cautious with this beast now, so no promises :) I'll work on that tomorrow. |
On Tue, May 22, 2012 at 12:29 AM, Min RK
Sure, I'd appreciate that so we can knock this monster in one shot. |
Mmh, I wonder why I got @minrk's reply via email but it doesn't show up here on the website. Weird. |
@fperez - I see my note just fine (I posted it on here shortly before your ping, it's not a reply to it). |
Okay, parallelmagic changes in #1630 are now restricted to adding silent kwarg in two places. I will open a separate PR totally rewriting that file after both of these are merged. |
@fperez, OK do you think we should start to review your branch now or is it not quite ready? I have been reviewing the mergekernel branch and am going to focus on code review. My branch is done until I can rebase on top of your two branches and finish a few things. |
@ellisonbg, go for it. There will be a few more commits with the actual implementation of the cell magics that I'm working on now, but those should be few. If you want to be able to see those in isolation, we can also make them a separate PR later today or tomorrow, in case you go through this in full. At least now this branch works in full and has what I think is the final architecture for magics, modulo missing actual |
OK I will start on the review... On Tue, May 22, 2012 at 11:54 AM, Fernando Perez
Brian E. Granger |
So I just wrote a simple https://gist.github.com/2779717 But I am running into a problem. Currently, the return value of a magic is discarded. This means there is no way of getting the result of a computation done in a cell magic back to the user. I think this is something we are going to run into a lot as magics get more complicated and interesting. Any thoughts on getting the return value back to the user? This could also show up if a magic function returns something that has an interesting representation that displayhook/pyout should pick up. |
Here is another version of a https://gist.github.com/2779875 I think this one is more useful. |
I have noticed a common mistake that is not handled very well currently. I keep forgetting to put in the extra |
Could you try pulling again? I've just made a bunch of changes while |
/home/tsuser/parent/child | ||
""" | ||
|
||
#bkms = self.shell.persist.get("bookmarks",{}) |
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.
Why is this line here? It looks like a leftover from some refactoring?
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.
Yup, good catch. Fixed locally, will push later today. Thanks!
I noticed the general "try/except" clause at couple more places -- I think it should be made explicit what exact exception(s) you are trying to catch. |
@ellisonbg, I think I'm done with all the items you'd noted. I'm going to look into the py3 stuff now that @takluyver reported, and we should be good to go. |
Refactored the test code a bit for better reuse of common functionality.
OK @takluyver, as best I can tell, I'm also in good shape with Python 3 now, I've applied the compat fix you indicated. Thanks for that review! |
%magic is our main interactive explanation of the magic system, so it's a good place to explain the system.
I've just completed the documentation and added |
Great, merge away! Sent from my iPad On May 26, 2012, at 9:41 PM, Fernando Perezreply@reply.github.com wrote:
|
I've also added |
Added detailed description to the docs, as well as comprehensive examples of magic creation with the new APIs.
OK, I've added a bunch of documentation to the actual manual, with detailed examples. Last round of tests, and this goes in... |
Test results for commit 0876e92 (can't merge cleanly)
Not available for testing: |
Mmh, I'm not sure why test_pr said above it wouldn't merge cleanly, because it does. Things are looking pretty solid on both 2 and 3, so I'll go ahead and merge now. This is blocking too much stuff, we can (and will) continue to polish on master. |
Refactoring of the magics system and implementation of cell magics. This PR completely refactors the magic system, finally moving the magic objects to standalone, independent objects instead of being the mixin class we'd had since the beginning of IPython. Now, a separate base class is provided in IPython.core.magic.Magics that users can subclass to create their own magics. Decorators are also provided to create magics from simple functions without the need for object orientation. All builtin magics now exist in a few subclasses that group together related functionality, and the new IPython.core.magics package has been created to organize this into smaller files. This cleanup was the last major piece of deep refactoring needed from the original 2001 codebase. Secondly, this PR introduces a new type of magic function, prefixed with `%%` instead of `%`, which operates at the cell level. A cell magic receives two arguments: the line it is called on (like a line magic) and the body of the cell below it. Cell magics are most natural in the notebook, but they also work in the terminal and qt console, with the usual approach of using a blank line to signal cell termination. This PR closes #1611, or IPEP 1, where the design had been discussed.
test_pr examines the 'mergeable' field when it queries the Github API for the pull request. If we see it making a mistake again, I'll check that - it's possible that Github is misreporting it after a rebase, for instance. Kudos for getting this done. Sorry I didn't have more time to look into it, but I'll do a bit of manual Python 3 testing now. |
One small flaw I've noticed: if you start the Qt console, and type |
Everything I've tried with it in Python 3.2 works perfectly. |
On Sun, May 27, 2012 at 11:40 AM, Thomas Kluyver <
I've seen that sometime, and for me it seems that gihub api result is
|
On 27 May 2012 13:00, Bussonnier Matthias
Ah, that could well be it. We'll keep an eye out for it, and if that |
@takluyver, thanks for catching the completion problem. Opened now as #1767. |
Great job! |
Refactoring of the magics system and implementation of cell magics. This PR completely refactors the magic system, finally moving the magic objects to standalone, independent objects instead of being the mixin class we'd had since the beginning of IPython. Now, a separate base class is provided in IPython.core.magic.Magics that users can subclass to create their own magics. Decorators are also provided to create magics from simple functions without the need for object orientation. All builtin magics now exist in a few subclasses that group together related functionality, and the new IPython.core.magics package has been created to organize this into smaller files. This cleanup was the last major piece of deep refactoring needed from the original 2001 codebase. Secondly, this PR introduces a new type of magic function, prefixed with `%%` instead of `%`, which operates at the cell level. A cell magic receives two arguments: the line it is called on (like a line magic) and the body of the cell below it. Cell magics are most natural in the notebook, but they also work in the terminal and qt console, with the usual approach of using a blank line to signal cell termination. This PR closes ipython#1611, or IPEP 1, where the design had been discussed.
Restore loadpy to load. closes ipython#1783, just the part of ipython#1606 eaten by ipython#1732, where some code was accidentally removed.
This IS now ready to merge!
Implement cell-level magics, with support in all frontends. In all text consoles the behavior is similar to that for other interactive code: one blank line signals the end of the cell. In the notebook, such limitation doesn't exist, of course.
The final version is slightly different from the original IPEP (#1611): we do not allow separate implementations of line and cell magics with the same name. Instead, a single function with signature
def f(line, cell=None)
(withself
if done as a method) will have to be called and distinguish whether the call is line-magic or cell-magic based on whethercell
isNone
or a string.All tests now pass on my machine, so this is good to go for full review.