Welcome to Cadabra Q&A, where you can ask questions and receive answers from other members of the community.
+2 votes

Cadabra 1.x has an include command "<<", which I used to build libraries of declarations (indices, fields, etc.) and substitution rules that I could then include in a number of other files. What is the Cadabra 2 syntax for this? (Apologies if this is answered already somewhere...)

I am also vaguely aware of how python implements modules via the import command, but this would seem to only let me bring in procedures (modules) from .py files, not a long list of declarations.


in General questions by (510 points)

I played with this some more and I can see how to use modules to import a entire python file, provided it is proper python code, but this fails with Cadabra syntax as noted by Kasper in https://cadabra.science/qa/593.

1 Answer

+2 votes

Dominic has figured out how to do this with the standard Python import functionality. I have just pushed some experimental code to github which does this. With this you can import notebooks into other notebooks. For example, if you have a library.cnb notebook containing a cell


{m, n}::Indices;
ex:=A_{m n};

you can import this expression and the property declaration into another notebook doing

from library import *

This will auto-convert your library.cnb into a library.py file which is then imported.

As I said: experimental. Any feedback welcome.

by (66.2k points)

Thanks! I'll play around with it and let you know anything interesting I uncover.

It seems doesn't work in Jupyter notebook:

I have ttt.cnb:

{m, n}::Indices;
ex:=A_{m n};

and I write :

from ttt import *

it says:

RuntimeError: * Line 1, Column 2 Missing '}' or object member name

At: cadabra2_defaults.py(54): find_spec (906): _find_spec (963): _find_and_load_unlocked (983): _find_and_load (2):

Jupyter notebooks are not Cadabra notebooks (giving them the extension .cnb makes no difference to that). And you cannot import Jupyter notebooks into other Jupyter notebooks. So at the moment this does not work, unless your ttt.cnb really is a Cadabra notebook, in which case it should work.

If you are absolutely sure your ttt.cnb is a Cadabra notebook (made with the Cadabra GUI, not with the Jupyter GUI which saves as Jupyter notebooks), then please report again and I'll have a look.

The current version on github (and on Cadabra Cloud) can now import Jupyter notebooks as well (assuming those have been generated by Cadabra). So if you have one notebook ttt.ipynb made on Cadabra Cloud, and you want to import that into another one, you can do

from ttt import *

and it will work just as if it had been a Cadabra notebook.

As a side-effect, you can now also load notebooks created with Cadabra Cloud (.ipynb) into the normal desktop Cadabra.