flask sub function not yielding results
Question: I have a bunch of code (1300 lines) that is working correctly and I am trying
to incorporate flask into the picture. In order to do this, I an trying to use
flask.Response to call a function within my method, that calls another method
in my class.

Here is test code that re-creates my problem.

    
    
    #!/usr/bin/env python
    
    import flask
    
    class TestClass(object):
    
        app = flask.Flask(__name__)
        def __init__(self):
            pass
    
        def worker(self):
            yield 'print test\n'
    
        @app.route('/')
        def test_method_get_stuff():
            return flask.render_template('index.html')
    
        @app.route('/', methods=['POST'])
        def test_method_post_stuff():
            def test_method_sub_function():
                tc.worker()
            return flask.Response(test_method_sub_function(),mimetype= 'text/plain')
    
    
    tc = TestClass()
    tc.app.run(debug=True)
    

`index.html` just has a text box with a submit button.

The issue I have is once you click the submit button, the request goes through
sucessfully but the page is blank with no errors in the python command line or
in the browser, and what I expect to happen is to show in plain text "print
test" with a newline.'

Any assistance would be appreciated. I am trying to avoid completely re-
writing all my code. With the understanding that i will have to replace
'print' with 'yield' commands in my code.


Answer: Your nested `test_method_sub_function()` function doesn't return anything; it
simply creates the generator (by calling a generator function), then exits.

It should at the very least _return_ the `tc.worker()` call:

    
    
    def test_method_sub_function():
        return tc.worker()
    

at which point the route works. You may as well skip this nested function
however and use `tc.worker()` _directly_ :

    
    
    @app.route('/', methods=['POST'])
    def test_method_post_stuff():
        return flask.Response(tc.worker(), mimetype='text/plain')
    

One note: although your use of the `Flask` object as a class attribute happens
to work, you should to put it in a class. Leave the `app` object and routes
outside of the class:

    
    
    import flask
    
    class TestClass(object):    
        def worker(self):
            yield 'print test\n'
    
    tc = TestClass()
    app = flask.Flask(__name__)
    
    @app.route('/')
    def test_method_get_stuff():
        return flask.render_template('index.html')
    
    @app.route('/', methods=['POST'])
    def test_method_post_stuff():
        return flask.Response(tc.worker(), mimetype='text/plain')
    
    app.run(debug=True)
    

