I need a HTML webpage in my Django app to load and show the continous output of a script in a scrollable box. Is this possible?
I\'m presently using a subprocess
You could simplify your task using StreamingHttpResponse and Popen:
def test_iterator():
from subprocess import Popen, PIPE, CalledProcessError
with Popen(['ping', 'localhost'], stdout=PIPE, bufsize=1, universal_newlines=True) as p:
for line in p.stdout:
yield(line + '
') # process line here
if p.returncode != 0:
raise CalledProcessError(p.returncode, p.args)
def busy_view(request):
from django.http import StreamingHttpResponse
return StreamingHttpResponse(test_iterator())
StreamingHttpResponse
expects an iterator as its parameter. An iterator function is one which has a yield
expression (or a generator expression), and its return value is a generator object (an iterator).
In this example, I simply echo the ping command to prove it works.
Substitute ['ping', 'localhost']
by a list (it has to be a list if you pass parameters to the command - in this case, localhost
). Your original ['python', script]
should work.
If you want to know more about generators, I would recommend Trey Hunner's talk, and also strongly that you read chapter 14 of Fluent Python book. Both are amazing sources.
Disclaimer:
Performance considerations
Django is designed for short-lived requests. Streaming responses will tie a worker process for the entire duration of the response. This may result in poor performance.
Generally speaking, you should perform expensive tasks outside of the request-response cycle, rather than resorting to a streamed response.