Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 烧瓶中的传递线_Python_String_Flask_Parameter Passing - Fatal编程技术网

Python 烧瓶中的传递线

Python 烧瓶中的传递线,python,string,flask,parameter-passing,Python,String,Flask,Parameter Passing,我有以下问题,似乎找不到答案,即使我认为这是相当直截了当的 我试图用Python/flask将字符串从一个函数传递到另一个函数。 没有烧瓶,它可以工作并打印我的文本 [...] def sample(args): with open(os.path.join(args.save_dir, 'config.pkl'), 'rb') as f: saved_args = cPickle.load(f) with open(os.path.join(args.save_

我有以下问题,似乎找不到答案,即使我认为这是相当直截了当的

我试图用Python/flask将字符串从一个函数传递到另一个函数。 没有烧瓶,它可以工作并打印我的文本

[...]
def sample(args):
    with open(os.path.join(args.save_dir, 'config.pkl'), 'rb') as f:
        saved_args = cPickle.load(f)
    with open(os.path.join(args.save_dir, 'chars_vocab.pkl'), 'rb') as f:
        chars, vocab = cPickle.load(f)
    model = Model(saved_args, training=False)
    with tf.Session() as sess:
        tf.global_variables_initializer().run()
        saver = tf.train.Saver(tf.global_variables())
        ckpt = tf.train.get_checkpoint_state(args.save_dir)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
            text = model.sample(sess, chars, vocab, args.n, args.prime,
                               args.sample).encode('utf-8')
            printer(text)

def printer(text):    
    print(text)
这很好,可以打印我的字符串。 如果我尝试实现flask,我会在127.0.0.1:5000/文本上得到一个内部伺服错误

def sample(args):
    with open(os.path.join(args.save_dir, 'config.pkl'), 'rb') as f:
        saved_args = cPickle.load(f)
    with open(os.path.join(args.save_dir, 'chars_vocab.pkl'), 'rb') as f:
        chars, vocab = cPickle.load(f)
    model = Model(saved_args, training=False)
    with tf.Session() as sess:
        tf.global_variables_initializer().run()
        saver = tf.train.Saver(tf.global_variables())
        ckpt = tf.train.get_checkpoint_state(args.save_dir)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
            text = model.sample(sess, chars, vocab, args.n, args.prime,
                               args.sample).encode('utf-8')
            printer(text)

@app.route('/text')
def printer(text):    
    return(text)
如果我只是尝试在不传递字符串的情况下显示一些文本,那么效果很好

我是python和flask的新手,非常感谢您的帮助

这是终端的回溯:

 [2017-12-02 15:47:06,244] ERROR in app: Exception on /text [GET]
Traceback (most recent call last):
  File "/Users/marcel/tensorflow/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/marcel/tensorflow/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/marcel/tensorflow/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/marcel/tensorflow/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/Users/marcel/tensorflow/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/marcel/tensorflow/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
TypeError: printer() missing 1 required positional argument: 'text'

查看运行flask应用程序的终端的控制台输出。 它应该具有要调试的详细信息的堆栈跟踪, 可能以这样一行结尾:

问题是用
@app.route
修饰的函数调用时没有参数,但您定义的
打印机
函数需要参数。您需要使函数无参数:

@app.route('/text')
def printer():    
    # ...
您需要更改它的实现,以便它返回一个有效的
响应
对象。
查看示例。

查看运行flask应用程序的终端的控制台输出。 它应该具有要调试的详细信息的堆栈跟踪, 可能以这样一行结尾:

问题是用
@app.route
修饰的函数调用时没有参数,但您定义的
打印机
函数需要参数。您需要使函数无参数:

@app.route('/text')
def printer():    
    # ...
您需要更改它的实现,以便它返回一个有效的
响应
对象。
有关示例,请参见。

能否发布整个回溯(作为对您的问题的编辑)。谢谢您能否发布整个回溯(作为对您问题的编辑)。谢谢是的,你是对的,这就是确切的错误。我正在查看响应对象。非常感谢。是的,你是对的,这就是确切的错误。我正在查看响应对象。非常感谢。