陈大厨¶ 2023-02-06 16:29 采纳率: 0%
浏览 149
已结题

如何从 .html url 启动/激活 .py 文件(sql debug) --> 现在只有两回答

  • .py
    (可以获取sql数据并传递给index_test_0203.html)
import mysql.connector
import webbrowser
import time
import pymysql
from flask import Flask,render_template,request 
 
app = Flask(__name__)
 
mydb = mysql.connector.connect(
  host="196.168.1.141",
  user="Main_root",
  password="password_123", 
  database="database_db",  
  auth_plugin='mysql_native_password'
)
              
mycursor = mydb.cursor()
 
# below one can work that exectue pyscript with fixed P_ID = 'en_1-01'
# mycursor.execute("SELECT P_TITLE,P_DESC  FROM webpage WHERE P_ID = 'en_1-01'")  
 
# this one won't work, want to get text from .html
mycursor.execute("SELECT P_TITLE,P_DESC  FROM webpage WHERE P_ID = "+request.args["P_ID"])                     
                                         
myresult = mycursor.fetchall()
 
print(myresult)    
 
@app.route('/')
def index():
    return render_template("index_test_0203.html", myresult = myresult)
 
if __name__ == "__main__":
    app.run(debug=True)
  • index_test_0203.html
    (可以通过 flask 从 url 获取文本到 html document.write(p) )
    但少了将该文本 text 作为特定的 P_ID ,pass 给 .py mysql进行搜索,并激活/执行 .py 脚本
<!DOCTYPE html>
<html>
    <script type="text/javascript">
        const queryString = window.location.search;
        const urlParams = new URLSearchParams(queryString);
        for (const p of urlParams) {
            document.write(p+'
');
        }
        </script>
    <body>
      <p> this is {{myresult}}</p>
 
    </body>
</html>

我会将 .py 和 .html 都放在云服务器上

主要问题:
如何从 .html url 启动/激活 .py 文件,例如 http://192.168.0.206:8080/english/index_test_0203.html?P_ID=en-1-01
 
 
有必要的话,请看以下补充资料, 里头有我做过的尝试
 


我的问题是: 从浏览器中输入 http://192.168.0.206:8080/english/MyShop.html?ProductID=001
该地址,从而运行你的py脚本,而不是自己手动调用py脚本将服务运行起来

ps. 我会把 .py 跟 .html 放到WinSCP云端伺服器上hold着,别人浏览原网址http://192.168.0.206:8080/english/MyShop.html显示的是主页, 然后他们输入的http://192.168.0.206:8080/english/MyShop.html?ProductID=en-1-01 会呈现同个 .html + 同 <header> , <footer>, 中間文的内容是.py 的产物(从mysql抓ProductID=en-1-01的资料)


悬赏资料: 目前卡的技术问题集,实际上是一个问题
https://docs.google.com/document/d/1IO2F8H5q8MZDIyTxPHWLsatjNV6UCSrDkd83SbZSNLg/edit?usp=sharing

文章以及连结请一定要看看,已经写很详尽了,更能follow 我的问题

  • 写回答

4条回答 默认 最新

  • sake321 2023-02-06 17:20
    关注

    要从 .html URL 启动/激活 .py 文件,可以使用 Flask 框架。可以在 .py 文件中定义一个 Flask 应用,在 HTML 文件中通过 URL 获取参数,再通过 Flask 路由将该参数传递给 .py 文件,在 .py 文件中使用该参数执行搜索。您可以使用 Flask 的 render_template 函数将搜索结果呈现到 HTML 文件中。

    以下是一个示例:

    .py 文件:

    from flask import Flask,render_template,request 
    import mysql.connector
     
    app = Flask(__name__)
     
    mydb = mysql.connector.connect(
      host="196.168.1.141",
      user="Main_root",
      password="password_123", 
      database="database_db",  
      auth_plugin='mysql_native_password'
    )
                  
    mycursor = mydb.cursor()
     
    @app.route('/')
    def index():
        mycursor.execute("SELECT P_TITLE,P_DESC  FROM webpage WHERE P_ID = "+request.args["P_ID"])                     
        myresult = mycursor.fetchall()
        return render_template("index_test_0203.html", myresult = myresult)
     
    if __name__ == "__main__":
        app.run(debug=True)
    

    HTML 文件:

    
    <!DOCTYPE html>
    <html>
        <script type="text/javascript">
            const queryString = window.location.search;
            const urlParams = new URLSearchParams(queryString);
            const P_ID = urlParams.get("P_ID");
            window.location.href = "/?P_ID=" + P_ID;
        </script>
        <body>
          <p> this is {{myresult}}</p>
        </body>
    </html>
    

    请确保将此代码部署在云服务器上并运行 Flask 应用,然后访问 HTML 页面时将参数作为 URL 参数提供。

    评论

报告相同问题?

问题事件

  • 系统已结题 2月14日
  • 修改了问题 2月9日
  • 修改了问题 2月6日
  • 修改了问题 2月6日
  • 展开全部