瓶子功能不会产生结果 [英] flask sub function not yielding results

查看:127
本文介绍了瓶子功能不会产生结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆代码(1300行),正在工作正常,我正在试图将瓶放入图片。为了做到这一点,我试图用flask.Response在我的方法中调用一个函数,调用我的类中的另一个方法。



这里是测试代码,重新创建我的问题。

pre $ #!/ usr / bin / env python

import flask

class TestClass(object):
$ b $ app = flask.Flask(__ name__)
def __init __(self):
pass

def worker(self):
yield'print test\\\
'

@app ('/')
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 只是一个带有提交按钮的文本框。



我遇到的问题是一旦你点击提交按钮,请求就成功了,但是页面是空的,在python命令行或者浏览器中没有错误,我希望发生的是用纯文本打印测试用换行符显示。



任何帮助将不胜感激。我试图避免完全重写我的所有代码。有了这个理解,我将不得不在我的代码中用'yield'命令替换'print'。解析方案

你嵌套的 test_method_sub_function()函数不返回任何东西;它只是简单地创建生成器(通过调用生成器函数),然后退出。



它应该至少返回 tc.worker()调用:

  def test_method_sub_function():
return tc.worker()

此时路线起作用。你可以跳过这个嵌套函数,直接使用 tc.worker()

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

注意:虽然你使用 Flask 对象作为类属性恰好工作,你应该把它放在一个类中。将应用程序对象和路线保留在类之外:

  import flask 
$ b $ class TestClass(object):
def worker(self):
yield'print test\\\
'

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)


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.

解决方案

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)

这篇关于瓶子功能不会产生结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆