-
Notifications
You must be signed in to change notification settings - Fork 292
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
%%writefile magic or similar for IRkernel? #244
Comments
Sorry, magics are a feature of IPython, the Python kernel, not Jupyter itself, and we have no plans to implement something similar for IRkernel. However, if you want to write code in a notebook and then convert it to an R script, you can use nbconvert to convert the whole notebook. There should be an option in the notebook menu (File, Download as), or you can run at a command line |
Hmm. I think this will inhibit the use of IRkernel for rapid code prototyping. My best workaround at the moment is to use the con <- file("/path/file.R")
writeLines(' [target cell] ',con) then merging the three together and executing. This is little faster than copying and pasting into an editor. I'm familiar with the Download as and nbconvert routes, but I often don't want all of the cells in a resulting script. The intent is to document how I authored a given script--i.e., striving for Jupyter's core tenet of literate programming. |
What would it take to implement this by capturing the More specifically, if the python kernel has magics, what would it take to add them ti the R kernel? |
It’s less about it being hard and more about it being unnecessary, this is easy enough, no? cat('
You only have to escape typewriter quotes \'
Multiple lines are fine
', file = 'thing.txt') |
@flying-sheep -- Sorry I want to have an R notebook and do
or
|
I think using reticulate or |
|
The magics system is part of IPython, not Jupyter. It's up to the people working on each kernel whether they want to support some kind of extra convenience syntax as well as the programming language the kernel is for. Please don't approach this with an expectation that every kernel should reimplement magics. That said, if people do want to, cell magics ( |
@takluyver -- exactly. And,I think that is why this issue is in IRkernel, not Jupyter. At least, I would think that IRkernel would be the R equivalent of IPython. If magics were going to be implemented for R, it would be here, right? |
Yep, this is where it would go. But I want to set expectations correctly: a kernel is an execution backend for a programming language, with no expectation that it extends that language. Please don't show up to kernel maintainers (largely @flying-sheep now for IRkernel; I haven't touched it for years) and expect them to 'just' provide IPython features. Approach this as a new feature you are hoping to convince someone else is worth implementing, maintaining, and dealing with bug reports on, not something that the project is self-evidently missing. Of course, like with any open source project, you can offer to help implement & maintain it, which means it's less work for the existing maintainers. But reviewing PRs takes time & energy too, and it's hard to be sure that a new contributor will stick around for maintenance, so you're still asking something of the maintainers. |
Gotcha. The first thing would be to look at the code for the %% magics in the IPython kernel and see how it is implemented there. |
After a quick look, the magics are in This suggests to me that down the road a more modular architecture would be that IPython would be generalized to a notebook library (perhaps Jupyter itself) with hooks for any kernel. Then the syntax of magics would be the same for all kernels, and magic could be used for any kernel. The notebook driver (for now IPython) would be kernel agnostic. Is something like this on anyone's radar, or is it worth a feature request to Jupyter or IPython? |
There have been some efforts to generalise kernel features - see metakernel. But AFAIK you have to build the kernel in Python to use that, whereas Jupyter as a whole is designed so that kernels can be implemented in any language. You could propose some extra layer between the frontend and the kernel which would intercept & handle certain code cells, but doing that as an official Jupyter feature would be a pretty big step. |
I would love to have magics in IRkernel! It seems that at least the xeus-based R kernel alternative intends to implement those, though they are not active recently. I extensively reviewed the magics adoption across Jupyter kernels here. About workarounds - probably none will be as good as having dedicated magics, but maybe you could consider using the IJulia solution of showing the workaround when user tries to use magic? |
The
%%writefile
magic in Jupyter is a major component of my workflow: I trial code in the notebook, then write out the final version to.py
for future use. (I don't anticipate opening a notebook every time I want to run a particular module.) I'm missing the convenience of this magic in the IRkernel. Any pointers/plans for future development?The text was updated successfully, but these errors were encountered: