Have to restart apache server to display data from SQL Server in Flask

151 views Asked by At

I have setup a Flask app on a VPS and everything seems to work fine; When i restart apache everything works fine and i got this: html table

if I reload the browser page the data disappear and only remains the table header; if I want to see again the data I have to restart the apache server, and this is the big problem that I can't figure out.

my data stricture is the following:

|----FlaskApp
|---------FlaskApp
|         +----main.html
|--------------static
|--------------templates
|              +----elenco.py
|              +----__init__.py

What I try to do is to connect to a SQL Server DB with _mssql, query the database and display the results in a html table. This is the content of the files:

init.py

from flask import Flask, render_template
import _mssql
from elenco  import Elenco_chiamate


Chiamate = Elenco_chiamate()


app = Flask(__name__)

@app.route('/')
def homepage():
    return render_template("main.html", chiamate_html = Chiamate)

if __name__ == "__main__":
    app.run()

elenco.py

    import _mssql

def Elenco_chiamate():
    conn = _mssql.connect(server='xxxxxxx', user='xxxxx', password='xxxxx')
    conn.execute_row('SELECT TOP 100 * FROM ASSET_VIEW')
    return conn 
    conn.close()

main.html

 <!DOCTYPE html>
<html lang="it">
<head>
    <meta charset="utf-8">
    <title>Python Programming Tutorials</title>
    <script
    src="https://code.jquery.com/jquery-3.1.1.min.js"
    integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
    crossorigin="anonymous"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>

<body>
    <table class="table table-striped table-bordered table-hover table-condensed">
        <tr>
        <th>
            INVENTARIO
        </th>
        <th>
            SERIALE
        </th>
        <th>
            APPARECCHIATURA
        </th>
        </tr>
    {% for row in chiamate_html %}
        <tr>
        <td>{{row['NUM']}}</td>
        <td>{{row['N_SERI']}}</td>
        <td>{{row['NOM']}}</td>
        </tr>
           {% endfor %}

    </table>
 </body>

</html>
1

There are 1 answers

2
Daniel Roseman On BEST ANSWER

You are calling Elenco_chiamate() at module level, so it will only be called once - when the module is first imported.

Call it inside the view function instead.