I have code like this to retrieve data from database and I want to display it in html.
This is app.py
@app.route(\'/news\')
def news():
id| name | email | phone | 1 | Eltac | eltac@gmail.com | +99421112 |
You can do something like this:
app_name.py
from flask import Flask, render_template
import mysql.connector
mydatabase = mysql.connector.connect(
host = 'localhost(or any other host)', user = 'name_of_user',
passwd = 'db_password', database = 'database_name')
mycursor = mydatabase.cursor()
#There you can add home page and others. It is completely depends on you
@app.route('/example.html')
def example():
mycursor.execute('SELECT * FROM user_info')
data = mycursor.fetchall()
return render_template('example.html', output_data = data)
In the above code we use fetchall() method that's why ID is also included automatically
(Header html tag and others are ignored. I write only inside of body) example.html
--snip--
ID
Name
Email
Phone
{% for row in output_data %} <-- Using these '{%' and '%}' we can write our python code -->
{{row[0]}}
{{row[1]}}
{{row[2]}}
{{row[3]}}
{% endfor %} <-- Because it is flask framework there would be other keywords like 'endfor' -->
--snip--
And finally you get expected result