[404错误,从html按钮运行python脚本|托管的Google App Engine网站

问题描述 投票:1回答:1

我有一个html页面custom.html和一个python脚本test.py(均在下面截图)。 html页面上只有一个按钮,该按钮应触发python脚本以打印一行(在我的下一个python脚本中,我希望它做更多的事情,但这是我的起点)。

在Chrome的开发人员工具下,单击按钮后,我收到由jQuery引发的GET 404错误。非常感谢通过我的html按钮成功激活python脚本的任何建议。

enter image description hereenter image description here

我的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>

编辑:我正在将代码添加到Google App Engine处理URL调用和导入Flask所需的main.py脚本中。

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route("/")
def index():
    return render_template('index.html')

@app.route("/learn.html")
def learn():
    return render_template('learn.html')

@app.route("/custom.html")
def custom():
    return render_template('custom.html')

if __name__ == "__main__":
    app.run()

编辑@Dustin Ingram的答案后,编辑2:

这是我的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>

  <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>

而且我做了一个简单的test.py只是为了测试单击html按钮以激活python脚本的能力

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开发者工具屏幕截图。

enter image description hereenter image description here

python html ajax google-app-engine google-cloud-platform
1个回答
1
投票

您无法通过这样的AJAX请求调用Python脚本。您需要调用与Python Web应用程序的端点相对应的URL。

例如,在前端:

$.ajax({
  url: "/do-something",
  context: document.body
})

然后在后端有一条对应的路由:

@app.route("/do-something")
def do_something():
    print("Successful line print")
    return 'OK'

有关在App Engine上开始使用Python网络应用程序的详细信息,请参见https://cloud.google.com/appengine/docs/standard/python3/quickstart。>>

编辑:

这正是我经过测试和确认的作品:

app.yaml

runtime: python37

requirements.txt

Flask==1.1.2

main.py

from flask import Flask, render_template

app = Flask(__name__)


@app.route('/custom.html')
def custom():
    return render_template('custom.html')

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

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8080, debug=True)

templates/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>

  <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>

您可以看到它在这里工作:https://stackoverflow-61195723.uc.r.appspot.com/custom.html

© www.soinside.com 2019 - 2024. All rights reserved.