-1

python file pass value to html with flask

  • error

Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.`

import mysql.connector
# from mysql import 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'
)
              
mycursor = mydb.cursor()
mycursor.execute("SELECT P_TITLE,P_DESC  FROM webpage WHERE P_ID = 'en_1-01'")                       
                                         
myresult = mycursor.fetchall()

print(myresult)    # does get the result I want 

# but below function not send "myresult" to html successfully
@app.route('/')
def index():
    return render_template("index.html", myresult = myresult)

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

    </body>
</html>

I already read through the discussion how to pass data to html page using flask? and also other tutorial but not see how to solve, therefore I need a hand thanks

error:jinja2.exceptions.TemplateNotFound: index.html enter image description here

enter image description here

3 Answers3

1

You are getting this error because there is no index.html file present in the templates folder. In flask, you need to create a folder named templates in the same location where your app.py file is located. Inside that templates folder, you need to create the index.html file.

So, your directory structure should look like:

|- firstflaskapp.py
|- templates/
|   |- index.html

I am sure this will resolve your error.

The full directory structure followed used in flask is:

|- app.py
|- templates/
|   |- index.html
|   |- login.html
|   |- ...
|- static/
|   |- css/
|   |   |- main.css
|   |- images/
|   |   |- logo.png
|   |- ...
|- requirements.txt
  • app.py is the main Flask application file
  • templates/ directory contains HTML templates for the application
  • static/ directory contains static files like CSS, JS, images, etc.
  • requirements.txt is a text file containing the list of python libraries by the application.
Shubham Garg
  • 459
  • 3
  • 10
1
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.101.141",
  user="root",
  password="password123", 
  database="cool_db",  
  auth_plugin='mysql_native_password'
)    
mycursor = mydb.cursor()

# send result to index.html
@app.route('/')
def index():
    mycursor.execute("SELECT P_TITLE,P_PRICE  FROM webpage WHERE P_ID = '%s'",(request.args.get("ProductID"),))
    myresult = mycursor.fetchall()
    return render_template("Myshop.html", myresult = myresult)

if __name__ == "__main__":
    app.run(debug=True)
7HR4IZ3
  • 21
  • 3
  • still get error though, I post the screen shot here, https://imgur.com/a/5xR7eW7 – 暫時時 Feb 02 '23 at 09:00
  • Change: mycursor.execute("SELECT P_TITLE,P_PRICE FROM webpage WHERE P_ID = '%s'",(request.args.get("ProductID"),)) to: mycursor.execute("SELECT P_TITLE,P_PRICE FROM webpage WHERE P_ID = %s",(request.args.get("ProductID"),)) I removed the quotes around %s – 7HR4IZ3 Feb 03 '23 at 18:39
1

you dont enter the Myshop.html in the url if your using flask. You go to the / route and it will render the Myshop.html.

If you want to pass data from the get request like in your example

http://192.168.0.206:8080/?ProductID=001

You can access that data via the request

@app.route('/')
def index():
    # if key doesn't exist, returns None
    myresult = request.args.get('ProductID')

    return render_template("Myshop.html", myresult = myresult)

This tutorial explains it in more detail: https://www.digitalocean.com/community/tutorials/processing-incoming-request-data-in-flask

Cpt_Freeze
  • 71
  • 5
  • humm, my question is **Question: how to trigger .py script by inputing the url like `http://192.168.0.206:8080/english/MyShop.html?ProductID=001`** , that anyone can type the url `http://......ProductID=001` , `http://.......ProductID=007` to load the new page; seems for now, we still need to execute pyscript which is middle step of the "task order in pic", I want the flow is from first step to the last step – 暫時時 Feb 02 '23 at 09:11
  • I'm not sure I understand your question? This isn't a full working example. To make it functional you should place the database query in a seperate function. And call that function with the ProductID data you take out of the request. So something like `myresult = my_db_query(request.args.get('ProductID'))` where `my_db_query()` is this new function – Cpt_Freeze Feb 02 '23 at 10:28