5

Python 3.6.3, Django 2.0.3

I'm new to django, but I'm trying to make a pretty simple site where someone can trigger a few tasks that were previously just misc, stand-alone python scripts. Unfortunately those tasks can take a pretty long time. I want to be able to display the output from those tasks in the middle of the following template (where {{ stream }} is) so the user has some meaningful feedback.

{% load pagePieces %}

{% page_header %}

<div class="container">

    <div class="row">
        <a class="btn" href="/"><i class="fa fa-chevron-left"></i> Home</a>
    </div>

    <div class="row row-header"><h1>{{ operation }}</h1></div>

    <div class="row row-content">
        {{ stream }}
    </div>
</div>

{% page_footer %}

In my view file I've tried a few different things, but here's about where I'm at now (this is somewhat simplified. I took out some error handling and changed some names):

def myaction(request):
    output_template = loader.get_template('myapp/process_output.html')
    return StreamingHttpResponse(
        output_template.render({
            'operation': 'That long running task', 
            "stream": streaming_wrapper()
        })
    )

def streaming_wrapper():
    output_template = loader.get_template('myapp/process_output.html')
    x = yield from a_module.long_running_task()
    yield output_template.render({
        'operation': 'That long running task', 
        "stream": x
    })

This does stream the output from long_running_task(), but doesn't load the rest of the template until after it's done. At other points, I've gotten the output to stream after the template, but never in the middle, which looks bad because my template has a header and a footer.

I'm not sure how to make this work, and I'm not sure if the answer is in my views or perhaps doing something more complicated with my template.

(I'm also aware this is similar to these 2 questions, but neither of them have satisfactory answers and are years old.

Django: return a StreamingHttpResponse on an existing html page

Django StreamingHttpResponse into a Template)

ashja99
  • 378
  • 5
  • 13

1 Answers1

0

Try to implement the steps as per this blog of Miguel Grinberg - https://blog.miguelgrinberg.com/post/video-streaming-with-flask/page/8 (This is for for Flask). But you have to follow the same steps and also need to modify the template as per the need. These steps worked for me.

Dharman
  • 30,962
  • 25
  • 85
  • 135