`yield` throughout parsing in flask

901 Views Asked by At

First, I use flask.

Second, I yield values like the code below.

def upload_file():
    if request.method == 'POST':
        f = request.files['file']
        def generate():
            yield '<pre>'
            yield "Processing...\n"
            file = CheckFile(f.filename, f.read())
            file.parse()
            for note in file.validate():
                if note.oddity_type == Note.ERROR:
                    yield '<span style="color: red;">%s</span>' % str(note)
                if note.type == Note.WARNING:
                    yield '<span style="color: orange;">%s</span>' % str(note)
                if note.type == Note.INFO:
                    yield '<span style="color: blue;">%s</span>' % str(note)
                if note.type == Note.FATAL:
                    yield '<span style="color: red;">%s</span>' % str(note)
            yield '</pre>'
        return Response(stream_with_context(generate()))
    if request.method == 'GET':
        return app.send_static_file('index.html')

So after the __init__ inCheckFileis completed, Ireturnvalues withyield` to the response.

What I want to do is to be able to yield values in __init__ and in file.parse().

0

There are 0 best solutions below