在Javascript中调用Python函数,读取静态文件(Flask)

时间:2015-03-30 16:33:43

标签: javascript python json flask

我正在创建基于用户选择的json文件的d3可视化,如下所示 -

d3.json("/data", function(error, graph)

这是views.py

中的应用路径
    @app.route("/data") #the javascript will call this
def data():
        return(userInput.get_data()) #returns the return value of given function

在表单中,我有一个userInput类,其中包含一个下拉列表。此列表由json文件的名称组成。 json_fileCrew是用户选择的json文件的名称。

json_fileCrew = SelectField(u"Filename", choices=[(f, f) for f in filenamesCrew])

在这个课程中,我有get_data函数:

def get_data(json_fileCrew):
        return send_from_directory ("/project/myproject/app/static/Crews" , json_fileCrew)

所以,当javascript调用/ data时它应该返回json_fileCrew,但是我收到了这个错误

File "\project\myproject\app\views.py", line
34, in data
    return(userInput.get_data()) #returns the return value of given function
TypeError: get_data() missing 1 required positional argument: 'json_fileCrew'

这是完整的追溯 -

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\flask\app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python34\lib\site-packages\flask\app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "C:\Python34\lib\site-packages\flask\app.py", line 1403, in handle_except
ion
    reraise(exc_type, exc_value, tb)
  File "C:\Python34\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Python34\lib\site-packages\flask\app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python34\lib\site-packages\flask\app.py", line 1477, in full_dispatch
_request
    rv = self.handle_user_exception(e)
  File "C:\Python34\lib\site-packages\flask\app.py", line 1381, in handle_user_e
xception
    reraise(exc_type, exc_value, tb)
  File "C:\Python34\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Python34\lib\site-packages\flask\app.py", line 1475, in full_dispatch
_request
    rv = self.dispatch_request()
  File "C:\Python34\lib\site-packages\flask\app.py", line 1461, in dispatch_requ
est
    return self.view_functions[rule.endpoint](**req.view_args)
  File "\project\myproject\app\views.py", line
34, in data
    return(userInput.get_data()) #returns the return value of given function
TypeError: get_data() missing 1 required positional argument: 'json_fileCrew'

1 个答案:

答案 0 :(得分:2)

你有一个方法

def get_data(json_fileCrew)

但是您没有使用必需的参数

来调用它
return(userInput.get_data())

正如错误告诉你的那样

TypeError: get_data() missing 1 required positional argument: 'json_fileCrew'