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

How to make Cadabra accept/incorporate the code prepared in plain text, for example in 'example.txt' file?

I'm not asking for the reading one line after another (https://cadabra.science/notebooks/ref_import.html) as inside I have not only definitions of variables... Probably I should expect something like:

 with open("Cadabra1.txt") as file:
     content = file.read()
content;

I have a Mathematica procedure generating thousands of txt files, each having properly designed code, which separately copied/pasted into opened Cadabra notebook allows me to run it in Cadabra. I need something to change this list of commands in each txt file into true Cadabra cnb files...

Naturally, I have tried to mimic inside of the Cadabra JSON but something goes wrong.

in General questions by (320 points)

2 Answers

+2 votes

If you have a text file Cadabra1.cdb with Cadabra code, you can read that in using

 from Cadabra1 import *

The file needs to be in your PYTHONPATH (or in the current directory, which Cadabra adds to PYTHONPATH on startup).

Pull the latest Cadabra version from github to fix an error which led to suppression of all output after such an import statement.

by (82.5k points)

I'm trying to use the above functionality to be able to import a very long expression from a text file. However, it seems to fail and I cannot see what I'm doing wrong.

As a simple example, my Cadabra1.cnb file looks like

#!/usr/local/bin/cadabra2
test := 5;

Now I go to a separate cadabra file and run

from Cadabra1 import *

No error is reported, so it seems to have found the file, but "test" is not recognized as an object.

Strange, that definitely works for me.

Oops! I had the wrong file extension (cnb vs cdb). Thanks, Kasper.

0 votes

Finally, I have sorted out things by generating the txt files with a list of commands appended by the line:

 #!/usr/local/bin/cadabra2

(therefore what would be a result of using in terminal "cadabra2cadabra example.cnb example.txt").

The last step for me was just running in a terminal:

cadabra2 file.txt
by (320 points)
...