Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.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
404从html按钮运行python脚本时出错|谷歌应用引擎网站托管_Python_Html_Ajax_Google App Engine_Google Cloud Platform - Fatal编程技术网

404从html按钮运行python脚本时出错|谷歌应用引擎网站托管

404从html按钮运行python脚本时出错|谷歌应用引擎网站托管,python,html,ajax,google-app-engine,google-cloud-platform,Python,Html,Ajax,Google App Engine,Google Cloud Platform,我有一个html页面custom.html和一个python脚本test.py(都在下面截图)。html页面只有一个按钮可以触发python脚本打印一行(在我的下一个python脚本中,我希望它能做得更多,但这是我的出发点) 在Chrome的开发者工具下,单击按钮后,我收到一个GET 404错误,由Jquery发起。非常感谢关于从我的html按钮成功激活python脚本的任何建议 我的test.py脚本非常简单 print("Successful line print") 这是我的cust

我有一个html页面custom.html和一个python脚本test.py(都在下面截图)。html页面只有一个按钮可以触发python脚本打印一行(在我的下一个python脚本中,我希望它能做得更多,但这是我的出发点)

在Chrome的开发者工具下,单击按钮后,我收到一个GET 404错误,由Jquery发起。非常感谢关于从我的html按钮成功激活python脚本的任何建议

我的test.py脚本非常简单

print("Successful line print")
这是我的custom.html文档

<!DOCTYPE html>
  <html lang="en" dir="ltr">

  <head>
    <meta charset="utf-8">
    <title>Page Title</title>
    <link rel="stylesheet" type="text/css" href="../static/css/style2.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>


  </head>
  <pre>

  </pre>

  <body>
    <div>
      <div>
        <button style="text-align: center; margin-bottom: 150px" class="search-btn" type="button" value=" Run Script " onclick="goPython()">Trigger Python
          <script>
            function goPython() {
              $.ajax({
                url: "../folder/test.py",
                context: document.body
              }).done(function() {
                alert('finished python script');;
              });
            }
          </script>
        </button>
      </div>
    </div>
  </body>

  </html>
编辑2,尝试@Dustin Ingram的答案后:

下面是my custom.html的新代码

from flask import Flask

app = Flask(__name__)

@app.route("/trigger-python")
def print_something():
    print("Successful line print")
    return 'OK'

print_something()

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8080, debug=True)
在修复了AJAX中调用的URL之后,我仍然在单击按钮时遇到相同的404错误。下面是一个更新的Chrome开发者工具屏幕截图


您不能通过这样的AJAX请求调用Python脚本。您需要调用对应于Python web应用程序端点的URL

例如,在前端:

$.ajax({
url:“/做点什么”,
上下文:document.body
})
然后在后端有一个对应的路由:

@app.route(“/do something”)
定义做某事():
打印(“成功行打印”)
返回“OK”
有关在App Engine上开始使用Python web应用程序的详细信息,请参阅

编辑:以下是我测试和确认的工作原理:

app.yaml

Flask==1.1.2
requirements.txt

<!DOCTYPE html>
  <html lang="en" dir="ltr">

  <head>
    <meta charset="utf-8">
    <title>Page Title</title>
    <link rel="stylesheet" type="text/css" href="../static/css/style2.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>


  </head>

  <body>
    <div>
      <div>
        <button style="text-align: center; margin-bottom: 150px" class="search-btn" type="button" value=" Run Script " onclick="goPython()">Click Here
          <script>
            function goPython() {
              $.ajax({
                url: "/trigger-python",
                context: document.body
              }).done(function() {
                alert('finished python script');;
              });
            }
          </script>
        </button>
      </div>
    </div>
  </body>

  </html>
main.py

从烧瓶导入烧瓶,渲染\u模板
app=烧瓶(名称)
@app.route(“/custom.html”)
def custom():
返回呈现模板('custom.html')
@app.route(“/trigger python”)
def print_something():
打印(“成功行打印”)
返回“OK”
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app.run(host='127.0.0.1',port=8080,debug=True)
templates/custom.html


页面标题
点击这里
函数goPython(){
$.ajax({
url:“/trigger python”,
上下文:document.body
}).done(函数(){
警报('finished python script');;
});
}

您可以看到它在这里工作:

谢谢您的建议,我已将代码添加到main.py,该脚本位于我的app.yaml脚本、static和templates子目录旁,用于调用网站各个页面的URL。您是否建议在单击按钮时需要一个单独的URL,并且python脚本链接到该单独的URL,就像“索引”、“学习”和“自定义”都有自己的URL一样?这就是我发现使用AJAX实现此目的的原因,但假设它会更安全,因为我正在运行Google App Engine:。那么这对我的案子来说是不正确的?@Henrik我已经更新了我的答案以添加更多细节。太好了,谢谢你的更新。我会在早上的第一件事就是把这件事告诉你,我整个上午都在实现你的答案,但仍然会遇到同样的问题(我已经用新代码更新了我原来的帖子,底部有错误)。我确信这是一个实现错误,我遗漏了一些小的东西,但查找问题只会让我找到我已经访问过的相同站点。你有什么建议吗?
runtime: python37
Flask==1.1.2
<!DOCTYPE html>
  <html lang="en" dir="ltr">

  <head>
    <meta charset="utf-8">
    <title>Page Title</title>
    <link rel="stylesheet" type="text/css" href="../static/css/style2.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>


  </head>

  <body>
    <div>
      <div>
        <button style="text-align: center; margin-bottom: 150px" class="search-btn" type="button" value=" Run Script " onclick="goPython()">Click Here
          <script>
            function goPython() {
              $.ajax({
                url: "/trigger-python",
                context: document.body
              }).done(function() {
                alert('finished python script');;
              });
            }
          </script>
        </button>
      </div>
    </div>
  </body>

  </html>