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