我有以下问题,似乎无法找到答案,即使我认为它相当直接......
我正在尝试将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)
这很好用并打印我的字符串。 如果我尝试实现烧瓶,我会在127.0.0.1:5000/text
上收到内部服务器错误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'
答案 0 :(得分:2)
查看运行flask应用程序的终端的控制台输出。 它应该有堆栈跟踪以及要调试的细节, 可能以这样的一行结束:
TypeError: printer() missing 1 required positional argument: 'text'
问题是用@app.route
修饰的函数在没有参数的情况下调用,但是你定义的printer
函数需要一个。您需要使函数无参数:
@app.route('/text')
def printer():
# ...
您需要更改其实现,以便返回有效的Response
对象。
有关示例,请参阅documentation。