1

I have an XML file that is updated every 5 minutes (data.xml). I also have a Flask Webserver (app.py) running on the same platform. This webserver hosts a HTML5 page (index.html). What is the most efficient way of displaying information from the XML page on the HTML (index.html) page in set locations and having it update every 5 minutes and when the page is refreshed? I have been advised to look at AJAX and jQuery but as I have no experience with these they seem slightly confusing. I need some help to find the most effecient solution. Fairly new to programming. Thank you in advance.

Data.xml:

    <?xml version='1.0' encoding='utf-8'?>
    <root>
      <coin>
        <trader variable="GLDAG_MAPLE">Gold.co.uk</trader>
        <metal>Silver</metal>
        <type>Maple</type>
        <price>£31.2</price>
      </coin>
    </root>

Data2.xml: data.xml converted to a dictionary

<root><coin><trader variable="GLDAG_MAPLE">Gold.co.uk</trader><metal>Silver</metal><type>Maple</type><price>£31.56</price></coin><coin><trader variable="GLDAG_BRITANNIA">Gold.co.uk</trader><metal>Silver</metal><type>Britannia</type><price>£32.4</price></coin><coin><trader variable="GLDAG_PHILHARMONIC">Gold.co.uk</trader><metal>Silver</metal><type>Philharmonic</type><price>£32.76</price></coin><coin><trader variable="BBPAG_MAPLE">Bullion By Post</trader><metal>Silver</metal><type>Maple</type><price>£27.12</price></coin><coin><trader variable="BBPAG_BRITANNIA">Bullion By Post</trader><metal>Silver</metal><type>Britannia</type><price>£23.88</price></coin><coin><trader variable="BBPAG_PHILHARMONIC">Bullion By Post</trader><metal>Silver</metal><type>Philharmonic</type><price>£26.88</price></coin></root>

App.py:

from flask import Flask, flash, redirect, render_template, request, session, abort, url_for
app = Flask(__name__)

# ./Home Script + Portfolio Page:
@app.route("/")
@app.route("/index")
def index():
    return render_template('index.html')

Index.html:

<td> Price from XML file needs to be presented here! </td>
arthem
  • 141
  • 3
  • 13

1 Answers1

0

For the case of periodic updates, it is best to use the likes of jQuery and Ajax. The following is the process that I would follow to solve your problem.

  1. Convert your xml file to a dict, so that it can be easily accessed in the template. See convert-an-xml-string-to-a-dictionar

  2. Use Ajax and Js (you can jQuery if you wish) to request an update at a given time interval. See the following examples update-and-render-a-value-from-flask-periodically and dynamically-update-html

  3. You then simply render the values in your template in the standard way.

This way, when your file updates every 5 minutes it can then be updated dynamically to the template.

Do let me know if you have any questions.

AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35
  • So my XML file containing the dictionary (data2.xml) is now setup. Using the answer from (https://stackoverflow.com/questions/15721679/update-and-render-a-value-from-flask-periodically) how do I connect data from the dictionary to the javascript file and the python file that has the flask script? – arthem Jun 11 '20 at 10:32
  • No not yet. Having issues with the jQuery AJAX script. Its not really working for me as I dont know how to use jQuery. Could you help write the script? I'm pretty certain that I can do the Python and HTML side of things. – arthem Jun 11 '20 at 16:37
  • It's rather simple -jQuery is merely a javascript library. Have a look at both the examples from the point `2` . Do also have a look at the documentation of `jQuery` if you are stuck - that's the only way to learn. If you are still stuck, consider posting the problem as a newer question and @ me here so I can have a look into it. – AzyCrw4282 Jun 11 '20 at 16:44