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)
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
我现在研究到的结果是 ajax 或 flask 可能会有所帮助,如果有其他解法请告诉我
你可以试一下
加js,打开指定程序
看你使用的是Flask,轻量级的框架使用非常简洁,关于你所描述的问题,想通过页面访问获得数据,使用Ajax即可,在html加载js代码。Flask服务启动后,直接在浏览器中输入请求的地址参数,Ajax会直接传给后端服务解析,并渲染html后返回,从而达到实际效果。
因為我對這一題的理想/目標是看是修改.HTML還是 .PY,能做到
从 .html url 启动/激活 .py 文件,例如 http://192.168.0.206:8080/english/index_test_0203.html?P_ID=en-1-01
我目前就是差这临门一脚,还望诸位专家给些帮助