# Import of commands from txt file to cnb

+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;

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.

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 (65.1k 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.

 #!/usr/local/bin/cadabra2
cadabra2 file.txt