5
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ Take a look at RunscheduledTask. Related/potential duplicate of mathematica.stackexchange.com/q/47263/131. $\endgroup$ – Yves Klett Dec 27 '15 at 3:58
  • 3
    $\begingroup$ 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. $\endgroup$ – m_goldberg Dec 27 '15 at 3:58
  • $\begingroup$ StartProcess might be worth a look. $\endgroup$ – george2079 Dec 27 '15 at 4:12
  • 1
    $\begingroup$ 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? $\endgroup$ – Szabolcs Dec 27 '15 at 12:01
  • $\begingroup$ 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 $\endgroup$ – user35053 Dec 30 '15 at 23:35
6
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – user35053 Dec 30 '15 at 23:41
  • $\begingroup$ @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. $\endgroup$ – LouisB Dec 31 '15 at 0:55
  • $\begingroup$ the delaySeconds works! its just what I needed! Thank you~ $\endgroup$ – user35053 Dec 31 '15 at 1:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.