Welcome, guest | Sign In | My Account | Store | Cart

This is a simple way to use the text interface to Mathematica and get the advantages of the editing capabilities of the GNU readline. The Python script makes use of the builtin function raw_input(), which in Linux uses GNU readline.

Python, 14 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import os,sys
f1=os.popen('math ', 'w')
f1.flush()
while 1:
  sys.stdout.write('')
  try:
    line=raw_input()
    f1.writelines(line+'\n')
    f1.flush()
  except KeyboardInterrupt:
    f1.close()
    break
sys.stdout.write('\n')
sys.exit()

This works on an xterm under the Linux operating system. To use it, give the code a name e.g. "wolfram.py", and start the Python intepreter in the xterm. Type

from wolfram import * That's it. The arrow keys should now functional with the up and down invoking the history function.

The initial position of the cursor may be incorrect. If anyone knows a more solid way achieve GNU readline editing, I would be pleased to hear about it. Also, it would nice if the whole thing could be started directly from the command line of the xterm, i.e.

python wolfram.py

4 comments

Michele Simionato 18 years, 10 months ago  # | flag

rlwrap. I think you are looking for rlwrap:

http://freshmeat.net/projects/rlwrap/

(never used it, however)

        M.S.
Mark Alford 18 years, 9 months ago  # | flag

Mathematica with readline input. Thanks for the clever idea! Wolfram Inc should have supplied line-editing themselves long ago.

Under Unix you can invoke your python-wrapped mathematica via a single command as follows.

Copy your Wolfram.py to a directory in your path such as /usr/local/bin, and put the following script (call it "mathcom", say) in the same directory:

#!/bin/csh
setenv PYTHONPATH /usr/local/bin
python -i -c "import Wolfram"

Typing "mathcom" gets you a nice line-editable mathematica command line interface.

David Lambert 15 years, 2 months ago  # | flag

I run the shell in an emacs buffer. This gives me full emacs capability with mathematica---and with all shell commands. To the dismay of my system managers I submit emacs to the batch queue to get interactive use of our batch-only computers.

Mark Alford 12 years, 3 months ago  # | flag

I have now tried Michele Simionato's suggestion above of using rlwrap. It works perfectly: just invoke mathematica with rlwrap math and you get command line editing and history. rlwrap is available as a package for Fedora and Debian Linux and perhaps other distributions as well.

Created by George Bertsch on Mon, 17 Jan 2005 (PSF)
Python recipes (4591)
George Bertsch's recipes (1)

Required Modules

Other Information and Tasks