Mathics icon indicating copy to clipboard operation
Mathics copied to clipboard

Run command not supported

Open sn6uv opened this issue 11 years ago • 6 comments

The ability to launch external commands, would be a nice addition

For example:

In[1]:= !python -c "print(1+1)"
2

This requires implementing the Run command [1] and adding some a special input rule to the frontend.

[1] http://reference.wolfram.com/mathematica/ref/Run.html

sn6uv avatar Nov 12 '13 00:11 sn6uv

@sn6uv This looks very interesting.

Do you think I could handle this?

mrshu avatar Nov 12 '13 21:11 mrshu

@mrshu, yes I think so.

I'm not completely sure what will be required to get this working cross-platform, but if you use the subprocess module it should be okay.

As always, let me know if you need help.

sn6uv avatar Nov 12 '13 21:11 sn6uv

Thanks!

A few unsorted notes

  • subprocess should be pretty cross-platfrom, at least in my tests it did not have any trouble running commands on Windows, Linux and OS X.
  • although I have read the Mathematica help page for this command I still do not have a clear idea of how it should work. Could you please provide me with some example use cases?
  • If I was to implement this command where in the code do you think I should start? I haven't seen mathics in a while -- what would be an appropriate file for this kind of function?
  • It seems that there will have to be an option to disable this function when not running the server locally.

mrshu avatar Nov 12 '13 22:11 mrshu

  • Seeing as this functionality isn't really related to what's currently implemented, I'd say it makes sense to put this into a new file. Perhaps mathics/builtin/command.py or mathics/builtin/external.py`.
  • Yes, there's the ENABLE_FILES_MODULE setting in mathics/settings.py, which controls which modules are imported in mathics/builtin/__init__.py. Maybe we should rename this if it now controls more than just the file module.
  • Sure, here's an example:
In[1]:= Run["pwd"]
/home/angus

Out[1]= 0

In[2]:= Run["du", "-sh"]    (* Cancelled because it takes to long *)
^C
Out[2]= 2

In[3]:= Run["cat", "myexample.py"]
#!/usr/bin/python2
print 1 + 1

Out[3]= 0

In[4]:= Run["python2", "myexample.py"]
2

Out[4]= 0

In[5]:= Run["ipython2"]   (* Launches an interactive program *)
Python 2.7.5 (default, Sep  6 2013, 09:55:21) 
Type "copyright", "credits" or "license" for more information.

IPython 1.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: 1+4
Out[1]: 5

In [2]: print("abc")
abc

In [3]: 
Do you really want to exit ([y]/n)? 

Out[5]= 0

sn6uv avatar Nov 13 '13 07:11 sn6uv

This is implemented in mathicsscript since 1.1.2. See https://github.com/Mathics3/mathicsscript/releases/tag/1.1.2

rocky avatar Apr 05 '21 14:04 rocky

@rocky @mmatera already implemented in Mathics CLI too.

TiagoCavalcante avatar Nov 09 '21 23:11 TiagoCavalcante