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日

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答