[IPython-User] Fortran cell magic

Matthias BUSSONNIER bussonniermatthias@gmail....
Tue Sep 10 02:19:42 CDT 2013

Le 10 sept. 2013 à 02:13, Martín Gaitán a écrit :

> Hi Matthias, thanks for you feedback 
> Did you know wether or not CodeMirror support Fortran highlighting. 
> If so it should be pretty easy to have the notebook switch to fortran mode if it sees the %%fortran
> at the start of cell.
> Cool. How can I do that? Any working example? 

For mosts things that would be a single line to add/ monkey patch: 


,'magic_fortran'      :{'reg':[/^%%fortran/]}

Unfortunately, it seem code mirror does not support fortran (yet)

> You can also add your magic into the ipython cookbook. And don't forget that if yo unmake it a python package it can be installed via PIP.
> sure, it's added to the to-do, but I would like to polish it a bit before :) 

That's your choice.

> I would also suggest to get closer to f2py and see if they would like to integrate this into their own repo, so that importing f2py into ipython
> would almost automagically load fortran magic.  
> See (Break out language magics into their own repos) https://github.com/ipython/ipython/issues/3803 for more info.
> What's the idea behind this? F2py is rarely used programatically (and its API is a bit rough, btw) and there is no need to import anything related from the f2py package to use a module compiled through it. 
> So in the context of an interactive session, do "import numpy.f2py"  would be the same effort than "%load_ext fortranmagic" and, imho, less explicit to our purpose.  what do you think about?

Well living in the same repo might not always make sense, but in case like %%R/rpy2 %%cython/cython, the magic is just a higher level wrapper to API. 
So it might make sense to have it together.  I don't really use fortran (anymore) so I'm not in a good place to judge, this was jus t a proposition. 
I might have.

From a end user point of view, you have either the : 
 - to use fortran with python, the best is IPython with fortran magic.
 - you have to  install fortran magic.
 - but fortran magic use f2py 
 - once done if you have the correct f2py version installed it works

 - to use fortran with python : install f2py
 - oh, and by the way if you are using ipython you can "Just" do load ext numpy.f2py and you have a fortran magic.

it feels for me exactly the same as %sympyprinting and panda html dataframe option.
they are part of the package, and if you are using IPython you "just" have to enable them.

(I guess a package can even try to detect if it is in IPython at import time and register magic automagically
even if explicit is better than implicit.)

> PS: Should this kind of message go to ipython-dev list instead of this one?

Sure, fernando did a much better explanation here if you are interested:


Little long but worth reading.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/ipython-user/attachments/20130910/74f68047/attachment.html 

More information about the IPython-User mailing list