6

I am running this script:

script = Import["!python  C:\\...python script", "String"];

but I need to run it a few times as it outputs data I need. I run it every time I change something so I would like to keep it running continuously or give me a 'live' feed of changes. I am trying to use Dynamic, but I am not sure how I can do it.

Edit:

My program is just a stream as I am using a sensor as a camera and the Python script that I run gives me the measurements that I need. The measurements change if I make the surroundings darker or lighter. Basically I'm taking pictures but I would like to be at a constant rate so it would work as a live-feed camera. Whenever I run the script it gives me different measurements so that is why I need to constantly run it but doing it manually seems like a hassle. I would like to keep running it without me doing it. Now I am using RunScheduleTask but it seems to be slow. Is there another way around this?

Karsten7
  • 27,448
  • 5
  • 73
  • 134
user35053
  • 127
  • 1
  • 5
  • Take a look at RunscheduledTask. Related/potential duplicate of http://mathematica.stackexchange.com/q/47263/131. – Yves Klett Dec 27 '15 at 03:58
  • 3
    I think we need more information to help you. Do you want your python script to run at a fixed interval? Do you want it to run when you click on a button? Or do you want some other event to trigger it? Pleas do not answer these questions in a comment -- edit your question to add information about triggering the script. – m_goldberg Dec 27 '15 at 03:58
  • StartProcess might be worth a look. – george2079 Dec 27 '15 at 04:12
  • 1
    I'm not sure I understand. "I run it every time I change something" ... <- What do you change? The Python script? Do you want Mathematica to detect when the Python script has changed and re-run it automatically? – Szabolcs Dec 27 '15 at 12:01
  • So my program is just a stream as I am using a sensor as a camera and the python script that I run gives me the measurements that I need. The measurements change if I make the surroundings darker or lighter. Basically I'm taking pictures but I would like to be at a constant rate so it would work as a live-feed camera. @Szabolcs – user35053 Dec 30 '15 at 23:35
  • @m_goldberg ... – user35053 Dec 30 '15 at 23:35
  • What's described in your last comment is quite different form what I understood from your original post. It would be good to edit (i.e. rewrite) your original post and explain more clearly what you need. – Szabolcs Dec 30 '15 at 23:59

1 Answers1

6

Have you considered using SetDelayed (:=) to define script? Here is an example that is coded for Windows:

linPy = "'import datetime; print datetime.datetime.now()'";
winPy = "\"import datetime; print datetime.datetime.now()\"";
cmd = "!python -c " <> winPy ;
script := Import[cmd, "String"]
Linux users would use the "linPy" expression instead of "winPy". With the above definitions, script is updated whenever it is referenced. For example, execute the following command a few times and you will see an updated value each time:

First@StringTake[script, {18 ;;}]

Of course, you would substitute your own python command.

For a continuously updated display you could use something like the following:

Dynamic[x]
continueDisplay = True;
Button["Stop", continueDisplay = False]
While[continueDisplay,
 x = First@StringTake[script, {18 ;;}];
 ]

The While-loop will be terminated when you press the "stop" button. The problem with continuous update is that you can't keep working in the notebook. Using the RunScheduledTask is a better solution:

Dynamic[x]
delaySeconds = 2;
task = RunScheduledTask[x = First@StringTake[script, {18 ;;}], delaySeconds];
The scheduled task waits 2 seconds, runs the Python script and updates the displayed value of x. It allows you to keep working in the same notebook. To terminate the scheduled task, evaluate RemoveScheduledTask[task];
LouisB
  • 12,528
  • 1
  • 21
  • 31
  • Yes every time I run my script I get different values but I want it so I don't have to run it manually each time. – user35053 Dec 30 '15 at 23:41
  • @user35053 I have added two display methods. One is a continuous update method and the other is an intermittent update. Thanks for mentioning the RunScheduledTask function in your post. – LouisB Dec 31 '15 at 00:55
  • the delaySeconds works! its just what I needed! Thank you~ – user35053 Dec 31 '15 at 01:00