Joy 宇 2023-02-02 18:16 采纳率: 77.8%
浏览 69
已结题

从 html 激活/执行 python 脚本(通过 url 而不是 submit 按钮)

现在我有 python 脚本可以提取 mysql 数据并传递给Myshop html

  • .py 脚本
import mysql.connector
import webbrowser
import time
import pymysql
from flask import Flask,render_template

app = Flask(__name__)

mydb = mysql.connector.connect(
  host="196.168.101.141",
  user="root",
  password="password123", 
  database="cool_db",  
  auth_plugin='mysql_native_password'
)
      # assume customer looking for product ID 001 item        
mycursor = mydb.cursor()
mycursor.execute("SELECT P_TITLE,P_PRICE  FROM webpage WHERE P_ID = '001'")                       
                                         
myresult = mycursor.fetchall()

print(myresult)    # does get the result I want 

# send result to index.html
@app.route('/')
def index():
    return render_template("Myshop.html", myresult = myresult)

if __name__ == "__main__":
    app.run(debug=True)
  • Myshop.html
<!DOCTYPE html>
<html>
    <body>
      <p> this is {{myresult}}</p>

    </body>
</html>
  • 预期流程的简图

enter image description here

我之后会把 .py 和 Myshop.html 都放到 WinSCP上(online),放到线上
问题:如何藉由输入像http://192.168.0.206:8080/english/MyShop.html?ProductID=001这样的url来触发.py脚本

该网页可以用我的脚本显示不同的内容


我想使用 url 来传递值(http: ......product=001)并激活我的 .py 脚本来刷新 Myshop.html

补充,我的问题为
 
问题:如何通过输入像http://192.168.0.206:8080/english/MyShop.html?ProductID=001 这样的url来触发.py脚本,任何人都可以输入url http://......ProductID=001 , http://......ProductID=007 加载新页面
 
我目前直接启动 .py 脚本 是 "图片中的任务顺序”中间的步骤,我希望的流程是从第一步到最后一步

enter image description here

  • 写回答

3条回答 默认 最新

  • 扛狼1427 2023-02-03 09:54
    关注
    
    from flask import Flask,render_template,request   // 修改点
    
    
    @app.route('/')
      def index():
          mydb = mysql.connector.connect(
            host="196.168.101.141",
            user="root",
            password="password123", 
            database="cool_db",  
            auth_plugin='mysql_native_password'
         )
          # assume customer looking for product ID 001 item        
        mycursor = mydb.cursor()
        mycursor.execute("SELECT P_TITLE,P_PRICE  FROM webpage WHERE P_ID = "+request.args["ProductID"])         // 修改点              
                                             
        myresult = mycursor.fetchall()
    
        print(myresult)    # does get the result I want 
        return render_template("Myshop.html", myresult = myresult)
    

    http://192.168.0.206:8080/english/MyShop.html?ProductID=001 或者 http://192.168.0.206:8080/english/MyShop.html?ProductID=002 等就可以动态更改

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 2月16日
  • 已采纳回答 2月8日
  • 创建了问题 2月2日

悬赏问题

  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元