Skip to content

An extension for IPython/Jupyter that helps to use Fortran in your interactive session.

License

Notifications You must be signed in to change notification settings

mgaitan/fortran_magic

Repository files navigation

Fortran magic

PyPI CI PyPI - Downloads

Compile and import symbols from a cell with Fortran code, using f2py.

Attention: I am looking for collaborators to maintain this project. If you are interested, please open an issue (or PRs) with your proposals for improvements and volunteer to be a maintainer.

The contents of the cell are written to a .f90 file in the directory IPYTHONDIR/fortran using a filename with the hash of the code. This file is then compiled. The resulting module is imported and all of its symbols are injected into the user's namespace.

Install

You can install or upgrade via uv:

uv add fortran-magic

Or via pip:

pip install -U fortran-magic

Or run directly

uv run --with=fortran-magic ipython

Basic usage

Once it's installed, you can load it with %load_ext fortranmagic. Then put your Fortran code in a cell started with the cell magic %%fortran. For example:

In[1]: %load_ext fortranmagic

In[2]: %%fortran

       subroutine f1(x, y, z)
            real, intent(in) :: x,y
            real, intent(out) :: z

            z = sin(x+y)

       end subroutine f1

Every symbol is automatically imported. So the subroutine f1 is already available in your python session as a function:

In[3]:  f1(1.0, 2.1415)
Out[3]: 9.26574066397734e-05

Tests

Run tests with:

uv run --group dev pytest

See the documentation for further details.

About

An extension for IPython/Jupyter that helps to use Fortran in your interactive session.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 8