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

Calling Python function in Javascript, reading static files (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 class,其中包含一个下拉列表。此列表包含 json 个文件的名称。 json_fileCrew 是用户选择的 json 文件的名称。

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

在这个 class 中,我有 get_data 函数:

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

所以,当 javascript 调用 /data 时,它应该 return 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'

你有办法

def get_data(json_fileCrew)

但您没有使用所需的参数调用它

return(userInput.get_data())

正如错误告诉你的那样

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