【问题标题】:Calling Python function in Javascript, reading static files (Flask) [closed]在Javascript中调用Python函数,读取静态文件(Flask)[关闭]
【发布时间】:2015-03-30 16:33:43
【问题描述】:

我正在根据用户选择的 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 -

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'

【问题讨论】:

  • 嗯,错误回溯很明显:您调用的函数get_data 没有所需的参数json_fileCrew
  • 有什么问题?您的 .get_data() 函数是使用参数定义的,但您的代码未传递任何参数。

标签: javascript python json flask


【解决方案1】:

你有方法

def get_data(json_fileCrew)

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

return(userInput.get_data())

正如错误告诉你的那样

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

【讨论】:

  • 谢谢,我花了这么多时间看错误的东西,我没有发现答案就在我面前!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
相关资源
最近更新 更多