Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask - Display database from python to html

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():
    import pymysql
    import re

    host='localhost'
    user = 'root'
    password = ''
    db = 'skripsi'

    try:
        con = pymysql.connect(host=host,user=user,password=password,db=db, use_unicode=True, charset='utf8')
        print('+=========================+')
        print('|  CONNECTED TO DATABASE  |')
        print('+=========================+')
     except Exception as e:
        sys.exit('error',e)

     cur = con.cursor()
     cur.execute("SELECT * FROM dataset")
     data = cur.fetchall()

     for row in data:
         id_berita = row[0]
         judul = row[1]
         isi = row[2]
         print('===============================================')
         print('BERITA KE', id_berita)
         print('Judul :', judul)
         print('Isi   :', isi)
         print('===============================================')

return render_template('home.html')

This is the result

enter image description here

This is berita.html. I want to display inside div class = output

<body>
<center>
<header style="padding-top: 10px; font-family: Calibri; font-size: 40pt;">WELCOME!</header><br>
<div class="nav">
  <a href="/home">Home
  <a href="/berita">Berita
  <a href="/preprocessing">Pre-Processing
  <a href="/feature">Fitur Ekstraksi
  <a href="/knn">KNN
</div>
<div class="output">
</div>

like image 538
David Herlianto Avatar asked Aug 08 '17 02:08

David Herlianto


People also ask

How do you display Python output on a Flask HTML page?

Create a simple HTML page to display text. create a route ”/” and return home. html from the function. Then run your api.py file and click on the link that it provides after running.

How do you display a database table in HTML using Python?

import mysql. connector import webbrowser conn = mysql. connector. connect(user='root', password='', host='localhost',database='company') if conn: print ("Connected Successfully") else: print ("Connection Not Established") select_employee = """SELECT * FROM employee""" cursor = conn.


2 Answers

render_template allows you to pass variables to html, and jinja2 help you to manipulate it. You only need to format your query result and send it within render_template

Example

app.py

@app.route('/test')
def test_route():
    user_details = {
        'name': 'John',
        'email': '[email protected]'
    }

    return render_template('test.html', user=user_details)

test.html

<!DOCTYPE html>
<html>
    <head>
        <title>test</title>
    </head>
    <body>
        <!-- use {{}} to access the render_template vars-->
        <p>{{user.name}}</p>
        <p>{{user.email}}</p>
    </body>
</html>

to make the most of jinja2, take a look at his Documentation

like image 141
DobleL Avatar answered Sep 21 '22 05:09

DobleL


You can pass your data using render_template() like this:

cur = con.cursor()
cur.execute("SELECT * FROM dataset")
data = cur.fetchall()
render_template('template.html', data=data)

Then in your template, iterate over the rows, for example you could render table rows for each row:

{% for item in data %}
<tr>
    <td>{{item[0]}}</td>
    <td>{{item[1]}}</td>
    ...
</tr>
{% endfor %}
like image 29
bgse Avatar answered Sep 18 '22 05:09

bgse